From b59683aea751d2302db56bf285be31c8a20f0380 Mon Sep 17 00:00:00 2001 From: Eli Treuherz Date: Wed, 10 Jan 2024 17:18:35 +0000 Subject: [PATCH] Add more properties to State 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. --- progressbar.go | 9 +++++++++ progressbar_test.go | 6 ++++++ 2 files changed, 15 insertions(+) diff --git a/progressbar.go b/progressbar.go index 1cd3f37..fb0ba90 100644 --- a/progressbar.go +++ b/progressbar.go @@ -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 { @@ -704,6 +707,11 @@ 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() @@ -711,6 +719,7 @@ func (p *ProgressBar) State() State { 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 } diff --git a/progressbar_test.go b/progressbar_test.go index 88acd78..ce780e8 100644 --- a/progressbar_test.go +++ b/progressbar_test.go @@ -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) }