sethgrid / multibar

Display multiple progress bars in Go (golang).
MIT License
377 stars 27 forks source link

`apt` style output #17

Open fenollp opened 5 years ago

fenollp commented 5 years ago

Hi! I'm trying to get an output of logs with a progress bar sticking on the bottom of the window. I've been adapting the example to try that:

diff --git a/demo/main.go b/demo/main.go
index d032073..739ae15 100644
--- a/demo/main.go
+++ b/demo/main.go
@@ -15,7 +15,8 @@ func main() {

        // some arbitrary totals for our  progress bars
        // in practice, these could be file sizes or similar
        mediumTotal := 200
        // 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
@@ -29,11 +30,11 @@ func main() {
        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...")

@@ -49,55 +50,59 @@ func main() {

        */
        wg := &sync.WaitGroup{}
        wg.Add(61)
        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)
                        if i % 10 == 0 {
                                // progressBars.Println("It is best to use the print wrappers to keep output synced up.")
                                progressBars.Bars[0].Prepend += fmt.Sprintf("#%v: wef wef wef wef wef wf wef\n", 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

but output looks nothing at all like apt upgrade:

0 4s mb master (22.0.2) ∀ go run demo/main.go
Below are many progress bars.
It is best to use the print wrappers to keep output synced up.
We can switch back to normal fmt after our progress bars are done.

1st#0: wef wef wef wef wef wf wef
#10: wef wef wef wef wef wf wef-------------------------------------------------------------------------------------------------------------------------------------------------------------] 136ms
#20: wef wef wef wef wef wf wef-------------------------------------------------------------------------------------------------------------------------------------------------------------] 288ms
#30: wef wef wef wef wef wf wef===>---------------------------------------------------------------------------------------------------------------------------------------------------------] 439ms
#40: wef wef wef wef wef wf wef============>------------------------------------------------------------------------------------------------------------------------------------------------] 591ms
All Bars Completewef wef wf wef=====================>---------------------------------------------------------------------------------------------------------------------------------------] 742ms
0 4s mb master (22.0.2) ∀ f wef==============================>------------------------------------------------------------------------------------------------------------------------------] 894ms
#70: wef wef wef wef wef wf wef=======================================>---------------------------------------------------------------------------------------------------------------------] 1s5ms
#80: wef wef wef wef wef wf wef================================================>------------------------------------------------------------------------------------------------------------] 1s
#90: wef wef wef wef wef wf wef=========================================================>---------------------------------------------------------------------------------------------------] 1s
#100: wef wef wef wef wef wf wef=================================================================>------------------------------------------------------------------------------------------] 1s
#110: wef wef wef wef wef wf wef==========================================================================>---------------------------------------------------------------------------------] 1s
#120: wef wef wef wef wef wf wef===================================================================================>------------------------------------------------------------------------] 1s
#130: wef wef wef wef wef wf wef============================================================================================>---------------------------------------------------------------] 1s
#140: wef wef wef wef wef wf wef=====================================================================================================>------------------------------------------------------] 2s
#150: wef wef wef wef wef wf wef==============================================================================================================>---------------------------------------------] 2s
#160: wef wef wef wef wef wf wef=======================================================================================================================>------------------------------------] 2s
#170: wef wef wef wef wef wf wef================================================================================================================================>---------------------------] 2s
#180: wef wef wef wef wef wf wef=========================================================================================================================================>------------------] 2s
#190: wef wef wef wef wef wf wef===================================================================================================================================================>--------] 2s
#200: wef wef wef wef wef wf wef===========================================================================================================================================================] 3ss
 100% [=====================================================================================================================================================================================] 3s

Any ideas?

sethgrid commented 5 years ago

Hi @fenollp , This project is not actively maintained - it was more of a demo to see how someone could do loading bars similar to how they might accomplish it in Docker. I'm swamped, and don't have the time to dig in, sorry. Without digging into your code, if I were you, I would see if the example runs as you expect. If yes, then try to structure your usage in the same way. The key is that the calls to the bar progress update happen concurrently (ie, in a goroutine) while the printing of the bars happens in your "root" code (probably main).