Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding color to one bar on abort adds colors to others in barExtenderRev example #125

Open
mmellin opened this issue Mar 21, 2023 · 1 comment

Comments

@mmellin
Copy link

mmellin commented Mar 21, 2023

I would like to add some color to the rendered bars, and I like the example barExtenderRev for my purposes. My purpose is to add red color to only the failed task bar, and expect the color of everything else to stay the same. What I am seeing is that when the task is aborted, other tasks and both bars change color to red as well.

I am trying to follow along with the middleware but the workflow is a bit convoluted. Any guidance would be appreciated.

$ go run _examples/barExtenderRev/main.go 
✓  Taksk 00: Done
✓  Taksk 01: Done
✗  Taksk 02: FAILED  <===== I want to color only this line Red
   Taksk 03

current:   [-----------------------------------------------------------------------------------------------------]  0 %
TOTAL(2/4) [==================================>------------------------------------------------------------------] 35 %

What I am seeing:

$ go run _examples/barExtenderRev/main.go 
✓  Taksk 00: Done    <===== RED
✓  Taksk 01: Done
✗  Taksk 02: FAILED <===== RED 
   Taksk 03 <====== Also RED

current:   [-----------------------------------------------------------------------------------------------------]  0 %
TOTAL(2/4) [==================================>------------------------------------------------------------------] 35 % <======== BOTH bars are red

My code changes for v8:

$ git diff _examples/barExtenderRev/main.go
diff --git a/_examples/barExtenderRev/main.go b/_examples/barExtenderRev/main.go
index d0544c1..eeea6b5 100644
--- a/_examples/barExtenderRev/main.go
+++ b/_examples/barExtenderRev/main.go
@@ -7,6 +7,7 @@ import (
        "sync/atomic"
        "time"
 
+       "github.com/fatih/color"
        "github.com/vbauerster/mpb/v8"
        "github.com/vbauerster/mpb/v8/decor"
 )
@@ -70,19 +71,31 @@ func main() {
 
        tb.SetTotal(total, false)
 
-       for _, t := range tasks {
+       aborting := false
+       for i, t := range tasks {
 
-       p.Wait()
+       // p.Wait()
 }
 
 func middleware(base mpb.BarFiller, id uint32) mpb.BarFiller {
        var done bool
+       red := color.New(color.FgRed).SprintFunc()
        fn := func(w io.Writer, st decor.Statistics) error {
                if !done {
                        cur := atomic.LoadUint32(&curTask) == id
@@ -91,12 +104,18 @@ func middleware(base mpb.BarFiller, id uint32) mpb.BarFiller {
                                return err
                        }
                        if !st.Completed {
-                               _, err := fmt.Fprintf(w, "=> Taksk %02d\n", id)
-                               return err
+                               if st.Aborted {
+                                       _, err := fmt.Fprintf(w, red("✗  Taksk %02d: FAILED\n"), id)
+                                       return err
+                               } else {
+                                       _, err := fmt.Fprintf(w, "=> Taksk %02d\n", id)
+                                       return err
+                               }
                        }
                        done = cur
                }
-               _, err := fmt.Fprintf(w, "   Taksk %02d: Done!\n", id)
+
+               _, err := fmt.Fprintf(w, "✓  Taksk %02d: Done\n", id)
                return err
        }
        if base == nil {
@@ -132,3 +151,9 @@ func complete(tb *mpb.Bar, t *task) {
        }
        bar.Wait()
 }
+
+func abort(tb *mpb.Bar, t *task) {
+       bar := t.bar
+       bar.Abort(false)
+       bar.Wait()
+}
(END)
@vbauerster
Copy link
Owner

This sounds like closing escape sequence is not applied.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants