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

Add more properties to State #170

Merged
merged 1 commit into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions progressbar.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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