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

[pull] main from schollz:main #11

Merged
merged 8 commits into from
May 20, 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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db
github.com/rivo/uniseg v0.4.7
github.com/stretchr/testify v1.3.0
golang.org/x/term v0.17.0
golang.org/x/term v0.20.0
)

go 1.13
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.17.0 h1:mkTF7LCd6WGJNL3K1Ad7kwxNfYAW6a8a8QqtMblp/4U=
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.20.0 h1:VnkxpohqXaOBYJtBmEppKUG6mXpi+4O6purfc2+sMhw=
golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY=
22 changes: 14 additions & 8 deletions progressbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ func NewOptions64(max int64, options ...Option) *ProgressBar {
width: 40,
max: max,
throttleDuration: 0 * time.Nanosecond,
elapsedTime: true,
elapsedTime: max == -1,
predictTime: true,
spinnerType: 9,
invisible: false,
Expand Down Expand Up @@ -494,10 +494,7 @@ func (p *ProgressBar) Reset() {

// Finish will fill the bar to full
func (p *ProgressBar) Finish() error {
p.lock.Lock()
p.state.currentNum = p.config.max
p.lock.Unlock()
return p.Add(0)
return p.Set64(p.config.max)
}

// Exit will exit the bar to keep current state
Expand Down Expand Up @@ -615,11 +612,17 @@ func New64(max int64) *ProgressBar {

// GetMax returns the max of a bar
func (p *ProgressBar) GetMax() int {
p.lock.Lock()
defer p.lock.Unlock()

return int(p.config.max)
}

// GetMax64 returns the current max
func (p *ProgressBar) GetMax64() int64 {
p.lock.Lock()
defer p.lock.Unlock()

return p.config.max
}

Expand All @@ -635,13 +638,17 @@ func (p *ProgressBar) ChangeMax(newMax int) {
// but takes in a int64
// to avoid casting
func (p *ProgressBar) ChangeMax64(newMax int64) {
p.lock.Lock()

p.config.max = newMax

if p.config.showBytes {
p.config.maxHumanized, p.config.maxHumanizedSuffix = humanizeBytes(float64(p.config.max),
p.config.useIECUnits)
}

p.lock.Unlock() // so p.Add can lock

p.Add(0) // re-render
}

Expand Down Expand Up @@ -840,7 +847,7 @@ func renderProgressBar(c config, s *state) (int, error) {
}
rightBrac = rightBracNum.String()
fallthrough
case c.elapsedTime:
case c.elapsedTime || c.showElapsedTimeOnFinish:
leftBrac = (time.Duration(time.Since(s.startTime).Seconds()) * time.Second).String()
}

Expand Down Expand Up @@ -944,8 +951,7 @@ func renderProgressBar(c config, s *state) (int, error) {
strings.Repeat(c.theme.SaucerPadding, repeatAmount),
c.theme.BarEnd,
sb.String())

if s.currentPercent == 100 && c.showElapsedTimeOnFinish {
if (s.currentPercent == 100 && c.showElapsedTimeOnFinish) || c.elapsedTime {
str = fmt.Sprintf("%s [%s]", str, leftBrac)
}

Expand Down
49 changes: 41 additions & 8 deletions progressbar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,12 @@ func ExampleOptionClearOnFinish() {
}

func ExampleProgressBar_Finish() {
bar := NewOptions(100, OptionSetWidth(10))
bar := NewOptions(100, OptionSetWidth(10), OptionShowCount(), OptionShowBytes(true), OptionShowIts())
bar.Reset()
time.Sleep(1 * time.Second)
bar.Finish()
// Output:
// 100% |██████████|
// 100% |██████████| (100/100 B, 100 B/s, 100 it/s)
}

func Example_xOutOfY() {
Expand Down Expand Up @@ -464,6 +466,37 @@ func TestOptionSetElapsedTime_spinner(t *testing.T) {
}
}

func TestOptionSetElapsedTime(t *testing.T) {
buf := strings.Builder{}
bar := NewOptions(
10,
OptionSetElapsedTime(false),
OptionSetPredictTime(false),
OptionSetWidth(10),
OptionSetWriter(&buf),
)

_ = bar.Add(2)
result := strings.TrimSpace(buf.String())
expect := "20% |██ |"

if result != expect {
t.Errorf("Render miss-match\nResult: '%s'\nExpect: '%s'\n%+v", result, expect, bar)
}

bar.Reset()
bar.config.elapsedTime = true
buf.Reset()

_ = bar.Add(7)
result = strings.TrimSpace(buf.String())
expect = "70% |███████ | [0s]"

if result != expect {
t.Errorf("Render miss-match\nResult: '%s'\nExpect: '%s'\n%+v", result, expect, bar)
}
}

func TestShowElapsedTimeOnFinish(t *testing.T) {
buf := strings.Builder{}
bar := NewOptions(10,
Expand Down Expand Up @@ -739,9 +772,9 @@ func TestOptionFullWidth(t *testing.T) {
{ // 4
[]Option{OptionSetPredictTime(false)},
"" +
"\r 10% |██████ | " +
"\r \r" +
"\r 100% |████████████████████████████████████████████████████████████████| ",
"\r 10% |██████ | " +
"\r \r" +
"\r 100% |█████████████████████████████████████████████████████████████████████| ",
},
{ // 5
[]Option{OptionSetPredictTime(false), OptionShowElapsedTimeOnFinish()},
Expand Down Expand Up @@ -795,9 +828,9 @@ func TestOptionFullWidth(t *testing.T) {
{ // 12
[]Option{OptionShowIts(), OptionShowCount(), OptionSetPredictTime(false)},
"" +
"\r 10% |████ | (10/100, 10 it/s) " +
"\r \r" +
"\r 100% |██████████████████████████████████████████████| (100/100, 50 it/s) ",
"\r 10% |████ | (10/100, 10 it/s) " +
"\r \r" +
"\r 100% |███████████████████████████████████████████████████| (100/100, 50 it/s) ",
},
{ // 13
[]Option{OptionShowIts(), OptionShowCount(), OptionSetPredictTime(false), OptionShowElapsedTimeOnFinish()},
Expand Down
Loading