Skip to content

Commit

Permalink
Merge branch 'kevin-hanselman-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
cheggaaa committed Mar 19, 2021
2 parents 2553c89 + db60cce commit aaf4f83
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
28 changes: 13 additions & 15 deletions v3/element.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,36 +231,34 @@ var ElementBar ElementFunc = func(state *State, args ...string) string {
return p.buf.String()
}

func elapsedTime(state *State) time.Duration {
return state.Time().Sub(state.StartTime()).Truncate(time.Millisecond)
}

// ElementRemainingTime calculates remaining time based on speed (EWMA)
// Optionally can take one or two string arguments.
// First string will be used as value for format time duration string, default is "%s".
// Second string will be used when bar finished and value indicates elapsed time, default is "%s"
// Third string will be used when value not available, default is "?"
// In template use as follows: {{rtime .}} or {{rtime . "%s remain"}} or {{rtime . "%s remain" "%s total" "???"}}
var ElementRemainingTime ElementFunc = func(state *State, args ...string) string {
var rts string
if state.IsFinished() {
return fmt.Sprintf(argsHelper(args).getOr(1, "%s"), elapsedTime(state))
}
sp := getSpeedObj(state).value(state)
if !state.IsFinished() {
if sp > 0 {
remain := float64(state.Total() - state.Value())
remainDur := time.Duration(remain/sp) * time.Second
rts = remainDur.String()
} else {
return argsHelper(args).getOr(2, "?")
}
} else {
rts = state.Time().Truncate(time.Second).Sub(state.StartTime().Truncate(time.Second)).String()
return fmt.Sprintf(argsHelper(args).getOr(1, "%s"), rts)
if sp > 0 {
remain := float64(state.Total() - state.Value())
remainDur := time.Duration(remain/sp) * time.Second
return fmt.Sprintf(argsHelper(args).getOr(0, "%s"), remainDur)
}
return fmt.Sprintf(argsHelper(args).getOr(0, "%s"), rts)
return argsHelper(args).getOr(2, "?")
}

// ElementElapsedTime shows elapsed time
// Optionally cat take one argument - it's format for time string.
// In template use as follows: {{etime .}} or {{etime . "%s elapsed"}}
var ElementElapsedTime ElementFunc = func(state *State, args ...string) string {
etm := state.Time().Truncate(time.Second).Sub(state.StartTime().Truncate(time.Second))
return fmt.Sprintf(argsHelper(args).getOr(0, "%s"), etm.String())
return fmt.Sprintf(argsHelper(args).getOr(0, "%s"), elapsedTime(state))
}

// ElementString get value from bar by given key and print them
Expand Down
6 changes: 6 additions & 0 deletions v3/pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,12 @@ func (pb *ProgressBar) SetTotal(value int64) *ProgressBar {
return pb
}

// AddTotal adds to the total bar value
func (pb *ProgressBar) AddTotal(value int64) *ProgressBar {
atomic.AddInt64(&pb.total, value)
return pb
}

// SetCurrent sets the current bar value
func (pb *ProgressBar) SetCurrent(value int64) *ProgressBar {
atomic.StoreInt64(&pb.current, value)
Expand Down
15 changes: 15 additions & 0 deletions v3/pb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,21 @@ func TestPBMaxWidth(t *testing.T) {
}
}

func TestAddTotal(t *testing.T) {
bar := new(ProgressBar)
bar.SetTotal(0)
bar.AddTotal(50)
got := bar.Total()
if got != 50 {
t.Errorf("bar.Total() = %v, want %v", got, 50)
}
bar.AddTotal(-10)
got = bar.Total()
if got != 40 {
t.Errorf("bar.Total() = %v, want %v", got, 40)
}
}

func TestPBTemplate(t *testing.T) {
bar := new(ProgressBar)
result := bar.SetTotal(100).SetCurrent(50).SetWidth(40).String()
Expand Down

0 comments on commit aaf4f83

Please sign in to comment.