Skip to content

Commit

Permalink
Merge pull request #170 from treuherz/complete-state
Browse files Browse the repository at this point in the history
Add more properties to State
  • Loading branch information
schollz committed Feb 24, 2024
2 parents 2232dc9 + b59683a commit a61e639
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 a61e639

Please sign in to comment.