diff options
| author | Rose Hogenson <rosehogenson@posteo.net> | 2025-04-05 14:24:07 -0700 |
|---|---|---|
| committer | Rose Hogenson <rosehogenson@posteo.net> | 2025-04-05 14:24:07 -0700 |
| commit | 049211d719574b9af8453b044d56625fa4af39af (patch) | |
| tree | b4b3c473afc0242b5f69a7e3ffbe65d99ec27bbd | |
| parent | 08f8aa62eeaa2ad0ed9a3f299e471c7b2fe98ee3 (diff) | |
| download | progress-bar-049211d719574b9af8453b044d56625fa4af39af.tar.zst | |
Fix bugs
| -rw-r--r-- | cmd/progress-bar/progress-bar.go | 9 | ||||
| -rw-r--r-- | progress.go | 9 |
2 files changed, 13 insertions, 5 deletions
diff --git a/cmd/progress-bar/progress-bar.go b/cmd/progress-bar/progress-bar.go index 65079ef..25e6216 100644 --- a/cmd/progress-bar/progress-bar.go +++ b/cmd/progress-bar/progress-bar.go @@ -10,7 +10,7 @@ import ( "strconv" "time" - "gitlab.com/rhogenson/progress-bar/progress" + "gitlab.com/rhogenson/progress-bar" ) var n = flag.Duration("n", 2*time.Minute, "polling interval") @@ -21,12 +21,13 @@ func run() error { return errors.New("usage error") } maxStr := args[0] - maxVal, err := strconv.Atoi(maxStr) + maxInt, err := strconv.Atoi(maxStr) if err != nil { return fmt.Errorf("max: %s", err) } + maxFloat := float64(maxInt) cmd := args[1:] - bar := progress.New(maxVal) + bar := new(progress.Bar) for i := 0; ; i++ { outputBytes, err := exec.Command(cmd[0], cmd[1:]...).Output() if err != nil { @@ -38,7 +39,7 @@ func run() error { fmt.Println(err) continue } - bar.Set(output) + bar.Set(float64(output) / maxFloat) bar.Print() time.Sleep(*n) } diff --git a/progress.go b/progress.go index da7cef0..ddc810a 100644 --- a/progress.go +++ b/progress.go @@ -5,7 +5,7 @@ // import ( // time" // -// "gitlab.com/rhogenson/progress-bar/progress" +// "gitlab.com/rhogenson/progress-bar" // ) // // func main() { @@ -43,6 +43,9 @@ type Bar struct { // Set sets the current value to val. val must be between 0 and 1, inclusive. func (b *Bar) Set(val float64) { + if !(0 <= val && val <= 1) { + panic(fmt.Sprintf("progress.Bar.Set: value must be between 0 and 1 (got %f)", val)) + } if b.measurements.Len() == measurements { b.measurements.PopFront() } @@ -51,6 +54,10 @@ func (b *Bar) Set(val float64) { // Print shows the progress bar on standard error. func (b *Bar) Print() { + if b.measurements.Len() == 0 { + return + } + first := b.measurements.Get(0) last := b.measurements.Get(b.measurements.Len() - 1) deltaT := last.t.Sub(first.t) |
