Giter VIP home page Giter VIP logo

progress_bar's Introduction

progress_bar

Go Progress Bar

Go Go Report Card

Features

progress_bar creates a single customizable progress bar for Linux terminal.

Installation

go get -u github.com/ermanimer/progress_bar

Functions

DefaultProgressBar(totalValue float64) *ProgressBar

Creates a progress bar with default parameters and given total value.

Parameter Description
totalValue Total value of progress bar

Default parameters:

Default Parameter Value
Default Schema [{bar}][{percent}][{current}/{total}][Elapsed: {elapsed}s Remaining:{remaining}s]
Default Filled Character #
Default Blank Character .
Default Length 50

NewProgressBar(output io.Writer, schema string, filledCharacter string, blankCharacter string, length float64, totalValue float64) *ProgressBar

Creates a progress bar with default parameters and given total value.

Parameter Description
output Output of progress bar
schema Schema of progress bar
filledCharacter Filled character of progress bar
blankCharacter Blank character of progress bar
length Length of progress bar
totalValue Total value of progress bar

Schema Variables:

Schema Variable Value
{bar} Bar of progress bar
{percent} Percentage of progress bar
{current} Current value of progress bar
{total} Total value of progress bar
{elapsed} Elapsed duration
{remaining} Estimated remaining duration

Methods

Start() error

Starts progress bar.

Stop() error

Stops progress bar.

Update(value float64) error

Updates progress bar with given value and stops progress bar is total value is reached.

Parameter Description
value Current value of progress bar

Usage

Default Progress Bar:

package main

import (
	"fmt"
	"time"

	"github.com/ermanimer/progress_bar"
)

func main() {
	//create new progress bar
	pb := progress_bar.DefaultProgressBar(100)
	//start
	err := pb.Start()
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	//update
	for value := 1; value <= 100; value++ {
		time.Sleep(20 * time.Millisecond)
		err := pb.Update(float64(value))
		if err != nil {
			fmt.Println(err.Error())
			break
		}
	}
}

Terminal Output: Default Terminal Output

New Progress Bar:

package main

import (
	"fmt"
	"os"
	"time"

	"github.com/ermanimer/progress_bar"
)

func main() {
	//create parameters
	output := os.Stdout
	schema := "({bar}) ({percent}) ({current} of {total} completed)"
	filledCharacter := "="
	blankCharacter := "-"
	var length float64 = 60
	var totalValue float64 = 80
	//create new progress bar
	pb := progress_bar.NewProgressBar(output, schema, filledCharacter, blankCharacter, length, totalValue)
	//start
	err := pb.Start()
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	//update
	for value := 1; value <= 80; value++ {
		time.Sleep(20 * time.Millisecond)
		err := pb.Update(float64(value))
		if err != nil {
			fmt.Println(err.Error())
			break
		}
	}
}

Terminal Output: New Terminal Output

New Progress Bar Colored With color:

package main

import (
	"fmt"
	"os"
	"time"

	"github.com/ermanimer/color/v2"
	"github.com/ermanimer/progress_bar"
)

func main() {
	//create parameters
	output := os.Stdout
	//create color functions
	orange := (&color.Color{Foreground: 172}).SprintFunction()
	grey := (&color.Color{Foreground: 246}).SprintFunction()
	//create colored schema
	bar := orange("{bar}")
	percent := grey("{percent}")
	schema := fmt.Sprintf("%s %s", bar, percent)
	filledCharacter := "โ–†"
	blankCharacter := " "
	var length float64 = 50
	var totalValue float64 = 80
	//create new progress bar
	pb := progress_bar.NewProgressBar(output, schema, filledCharacter, blankCharacter, length, totalValue)
	//start
	err := pb.Start()
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	//update
	for value := 1; value <= 80; value++ {
		time.Sleep(20 * time.Millisecond)
		err := pb.Update(float64(value))
		if err != nil {
			fmt.Println(err.Error())
			break
		}
	}
}

Terminal Output: New Terminal Output

References

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.