Skip to content

Commit

Permalink
Add more properties to State
Browse files Browse the repository at this point in the history
Add Current num (as an int not a float), maximum and description to the
State output, hopefully making it a more complete and useful description of the
current state of the bar.
  • Loading branch information
treuherz committed Jan 10, 2024
1 parent 27ddfcc commit b59683a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
9 changes: 9 additions & 0 deletions progressbar.go
Expand Up @@ -26,11 +26,14 @@ type ProgressBar struct {

// State is the basic properties of the bar
type State struct {
Max int64
CurrentNum int64
CurrentPercent float64
CurrentBytes float64
SecondsSince float64
SecondsLeft float64
KBsPerSecond float64
Description string
}

type state struct {
Expand Down Expand Up @@ -704,13 +707,19 @@ func (p *ProgressBar) State() State {
p.lock.Lock()
defer p.lock.Unlock()
s := State{}
s.CurrentNum = p.state.currentNum
s.Max = p.config.max
if p.config.ignoreLength {
s.Max = -1
}
s.CurrentPercent = float64(p.state.currentNum) / float64(p.config.max)
s.CurrentBytes = p.state.currentBytes
s.SecondsSince = time.Since(p.state.startTime).Seconds()
if p.state.currentNum > 0 {
s.SecondsLeft = s.SecondsSince / float64(p.state.currentNum) * (float64(p.config.max) - float64(p.state.currentNum))
}
s.KBsPerSecond = float64(p.state.currentBytes) / 1024.0 / s.SecondsSince
s.Description = p.config.description
return s
}

Expand Down
6 changes: 6 additions & 0 deletions progressbar_test.go
Expand Up @@ -491,6 +491,12 @@ func TestSpinnerState(t *testing.T) {
bar.Add(10)

state := bar.State()
if state.Max != -1 {
t.Errorf("Max mismatched gotMax %d wantMax %d", state.Max, -1)
}
if state.CurrentNum != 10 {
t.Errorf("Number mismatched gotNum %d wantNum %d", state.CurrentNum, 10)
}
if state.CurrentBytes != 10.0 {
t.Errorf("Number of bytes mismatched gotBytes %f wantBytes %f", state.CurrentBytes, 10.0)
}
Expand Down

0 comments on commit b59683a

Please sign in to comment.