• Stars
    star
    377
  • Rank 113,535 (Top 3 %)
  • Language
    Go
  • License
    MIT License
  • Created almost 10 years ago
  • Updated over 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Display multiple progress bars in Go (golang).

MultiBar

Display progress bars in Go

$ go run main.go
here we have a progress bar

some work  30% [====================>-------------------------------------------] 925ms
and here we have another progress bar
here we have a longer prepend string  25% [==========>--------------------------] 911ms
working...  19% [=============>-------------------------------------------------] 911ms

Display Options:

When you call .MakeBar(total int, prepend string), the returned progress bar has options that you can change to fit your needs. Here are the defaults:

- Width:           screen width - len(prepend) - 20
- Total:           total (passed in)
- Prepend:         prepend (passed in)
- LeftEnd:         '['
- RightEnd:        ']'
- Fill:            '='
- Head:            '>'
- Empty:           '-'
- ShowPercent:     true
- ShowTimeElapsed: true

Example usage:

example gif

(note, in the example gif, we are manually resetting the terminal mode. We NO LONGER have to, issue fixed)

You can run this with cd demo; go run main.go.

    package main

    import (
        "fmt"
        "sync"
        "time"

        "github.com/sethgrid/multibar"
    )

    func main() {
        // create the multibar container
        // this allows our bars to work together without stomping on one another
        progressBars, _ := multibar.New()

        // some arbitrary totals for our  progress bars
        // in practice, these could be file sizes or similar
        mediumTotal, smallTotal, largerTotal := 150, 100, 200

        // make some output for the screen
        // the MakeBar(total, prependString) returns a method that you can pass progress into
        progressBars.Println("Below are many progress bars.")
        progressBars.Println("It is best to use the print wrappers to keep output synced up.")
        progressBars.Println("We can switch back to normal fmt after our progress bars are done.\n")

        // we will update the progress down below in the mock work section with barProgress1(int)
        barProgress1 := progressBars.MakeBar(mediumTotal, "1st")

        progressBars.Println()
        progressBars.Println("We can separate bars with blocks of text, or have them grouped.\n")

        barProgress2 := progressBars.MakeBar(smallTotal, "2nd - with description:")
        barProgress3 := progressBars.MakeBar(largerTotal, "3rd")
        barProgress4 := progressBars.MakeBar(mediumTotal, "4th")
        barProgress5 := progressBars.MakeBar(smallTotal, "5th")
        barProgress6 := progressBars.MakeBar(largerTotal, "6th")

        progressBars.Println("And we can have blocks of text as we wait for progress bars to complete...")

        // listen in for changes on the progress bars
        // I should be able to move this into the constructor at some point
        go progressBars.Listen()

        /*

            *** mock work ***
            spawn some goroutines to do arbitrary work, updating their
            respective progress bars as they see fit

        */
        wg := &sync.WaitGroup{}
        wg.Add(6)
        go func() {
            // do something asyn that we can get updates upon
            // every time an update comes in, tell the bar to re-draw
            // this could be based on transferred bytes or similar
            for i := 0; i <= mediumTotal; i++ {
                barProgress1(i)
                time.Sleep(time.Millisecond * 15)
            }
            wg.Done()
        }()

        go func() {
            for i := 0; i <= smallTotal; i++ {
                barProgress2(i)
                time.Sleep(time.Millisecond * 25)
            }
            wg.Done()
        }()

        go func() {
            for i := 0; i <= largerTotal; i++ {
                barProgress3(i)
                time.Sleep(time.Millisecond * 12)
            }
            wg.Done()
        }()

        go func() {
            for i := 0; i <= mediumTotal; i++ {
                barProgress4(i)
                time.Sleep(time.Millisecond * 10)
            }
            wg.Done()
        }()
        go func() {
            for i := 0; i <= smallTotal; i++ {
                barProgress5(i)
                time.Sleep(time.Millisecond * 20)
            }
            wg.Done()
        }()
        go func() {
            for i := 0; i <= largerTotal; i++ {
                barProgress6(i)
                time.Sleep(time.Millisecond * 10)
            }
            wg.Done()
        }()
        wg.Wait()

        // continue doing other work
        fmt.Println("All Bars Complete")
    }

Idiosyncrasies

When you run tests, a lot of terminal cursor movement happens. This will cause the output to look all kinds of messed up. In most unix systems, clear or cmd+k should clear out the output.

License

See LICENSE.md

More Repositories

1

pester

Go (golang) http calls with retries and backoff
Go
644
star
2

gencurl

gencurl generates a curl command based on an http.Request to be used for logging and debugging
Go
161
star
3

curse

Basic terminal cursor manipulation
Go
49
star
4

fakettp

Command line http debugging proxy for faking http responses. Set response codes, headers, time to respond, and more
Go
48
star
5

Dapi

Dapi is an experiment in creating a database rest api geared towards MySQL in Go.
Go
5
star
6

httpsink

A simple http sink. Any endpoint that you call gets captured and the request data can be retrieved.
Go
5
star
7

memtest

MemTest is a terminal spot-the-difference game
Go
4
star
8

xeger

Xeger is an inverse regex generator. Pass in your regular expression pattern and get back a string that matches.
Go
3
star
9

giraffe

giraffe is an in-memory directional graph designed to model scaffolding knowledge relationships
Go
3
star
10

the_game

Playing around with a terminal based game (this is the server). see github.com/sethgrid/the_game_client
Go
2
star
11

multifilt

MultiFilt (and the cmd line tool mf) filter an input stream and drop lines that match lines in a filter file.
Go
2
star
12

MorseNode

Spaces missing in your Morse Code? Rebuild possible matching strings.
Python
2
star
13

countmyreps

Go
2
star
14

taskr

Log the tasks that you've done from the command line
Go
1
star
15

go_apid

Go
1
star
16

the_game_client

Playing around with a terminal based game (this is the client). see github.com/sethgrid/the_game
Go
1
star
17

interview_idea

An idea I am floating for interviews
Go
1
star
18

justify

playing around with justifying fixed width text
Go
1
star
19

exploit

An attempt at validating a curl | sh exploit
Go
1
star