Skip to content

Commit a177c83

Browse files
authored
Print only status bars that have been activated (#351)
This fixes #348 by changing the behaviour of the `progressWithStatusBarsTTY` to only print status bars that have been updated/reset. It removes the placeholder label "Starting worker..." and simply doesn't display status bars that don't have a value yet.
1 parent c7922ae commit a177c83

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

cmd/src/campaign_progress_printer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (p *campaignProgressPrinter) initProgressBar(statuses []*campaigns.TaskStat
4545

4646
statusBars := make([]*output.StatusBar, 0, numStatusBars)
4747
for i := 0; i < numStatusBars; i++ {
48-
statusBars = append(statusBars, output.NewStatusBarWithLabel("Starting worker..."))
48+
statusBars = append(statusBars, output.NewStatusBar())
4949
}
5050

5151
p.progress = p.out.ProgressWithStatusBars([]output.ProgressBar{{

internal/output/progress_with_status_bars_tty.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ func newProgressWithStatusBarsTTY(bars []*ProgressBar, statusBars []*StatusBar,
5454
type progressWithStatusBarsTTY struct {
5555
*progressTTY
5656

57-
statusBars []*StatusBar
58-
statusBarLabelWidth int
57+
statusBars []*StatusBar
58+
statusBarLabelWidth int
59+
numPrintedStatusBars int
5960
}
6061

6162
func (p *progressWithStatusBarsTTY) Close() { p.Destroy() }
@@ -83,7 +84,7 @@ func (p *progressWithStatusBarsTTY) Complete() {
8384
defer p.o.lock.Unlock()
8485

8586
// +1 because of the line between progress and status bars
86-
for i := 0; i < len(p.statusBars)+1; i += 1 {
87+
for i := 0; i < p.numPrintedStatusBars+1; i += 1 {
8788
p.o.moveUp(1)
8889
p.o.clearCurrentLine()
8990
}
@@ -98,7 +99,7 @@ func (p *progressWithStatusBarsTTY) Complete() {
9899
}
99100

100101
func (p *progressWithStatusBarsTTY) lines() int {
101-
return len(p.bars) + len(p.statusBars) + 1
102+
return len(p.bars) + p.numPrintedStatusBars + 1
102103
}
103104

104105
func (p *progressWithStatusBarsTTY) SetLabel(i int, label string) {
@@ -162,12 +163,18 @@ func (p *progressWithStatusBarsTTY) draw() {
162163

163164
}
164165

166+
p.numPrintedStatusBars = 0
165167
for i, statusBar := range p.statusBars {
166168
if statusBar == nil {
167169
continue
168170
}
171+
if !statusBar.initialized {
172+
continue
173+
}
174+
169175
last := i == len(p.statusBars)-1
170176
p.writeStatusBar(last, statusBar)
177+
p.numPrintedStatusBars += 1
171178
}
172179
}
173180

internal/output/status_bar.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ type StatusBar struct {
1111
format string
1212
args []interface{}
1313

14-
startedAt time.Time
15-
finishedAt time.Time
14+
initialized bool
15+
startedAt time.Time
16+
finishedAt time.Time
1617
}
1718

1819
// Completef sets the StatusBar to completed and updates its text.
@@ -25,6 +26,7 @@ func (sb *StatusBar) Completef(format string, args ...interface{}) {
2526

2627
// Resetf sets the status of the StatusBar to incomplete and updates its label and text.
2728
func (sb *StatusBar) Resetf(label, format string, args ...interface{}) {
29+
sb.initialized = true
2830
sb.completed = false
2931
sb.label = label
3032
sb.format = format
@@ -34,6 +36,7 @@ func (sb *StatusBar) Resetf(label, format string, args ...interface{}) {
3436

3537
// Updatef updates the StatusBar's text.
3638
func (sb *StatusBar) Updatef(format string, args ...interface{}) {
39+
sb.initialized = true
3740
sb.format = format
3841
sb.args = args
3942
}
@@ -52,3 +55,5 @@ func (sb *StatusBar) runtime() time.Duration {
5255
func NewStatusBarWithLabel(label string) *StatusBar {
5356
return &StatusBar{label: label, startedAt: time.Now()}
5457
}
58+
59+
func NewStatusBar() *StatusBar { return &StatusBar{} }

0 commit comments

Comments
 (0)