Skip to content

Commit dcad234

Browse files
authored
Merge pull request #205 from mcamou/add-no-count-option
Fixes #138: Add OptionShowTotal
2 parents bb7c93d + 75ed67f commit dcad234

File tree

2 files changed

+84
-6
lines changed

2 files changed

+84
-6
lines changed

progressbar.go

+31-5
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ type config struct {
8383
showIterationsPerSecond bool
8484
showIterationsCount bool
8585

86+
// whether the progress bar should show the total bytes (e.g. 23/24 or 23/-, vs. just 23).
87+
showTotalBytes bool
88+
8689
// whether the progress bar should show elapsed time.
8790
// always enabled if predictTime is true.
8891
elapsedTime bool
@@ -309,6 +312,13 @@ func OptionShowElapsedTimeOnFinish() Option {
309312
}
310313
}
311314

315+
// OptionShowTotalBytes will keep the display of total bytes.
316+
func OptionShowTotalBytes(flag bool) Option {
317+
return func(p *ProgressBar) {
318+
p.config.showTotalBytes = flag
319+
}
320+
}
321+
312322
// OptionSetItsString sets what's displayed for iterations a second. The default is "it" which would display: "it/s"
313323
func OptionSetItsString(iterationString string) Option {
314324
return func(p *ProgressBar) {
@@ -403,6 +413,7 @@ func NewOptions64(max int64, options ...Option) *ProgressBar {
403413
spinnerType: 9,
404414
invisible: false,
405415
spinnerChangeInterval: 100 * time.Millisecond,
416+
showTotalBytes: true,
406417
},
407418
}
408419

@@ -482,6 +493,7 @@ func DefaultBytes(maxBytes int64, description ...string) *ProgressBar {
482493
OptionSetDescription(desc),
483494
OptionSetWriter(os.Stderr),
484495
OptionShowBytes(true),
496+
OptionShowTotalBytes(true),
485497
OptionSetWidth(10),
486498
OptionThrottle(65*time.Millisecond),
487499
OptionShowCount(),
@@ -508,6 +520,7 @@ func DefaultBytesSilent(maxBytes int64, description ...string) *ProgressBar {
508520
OptionSetDescription(desc),
509521
OptionSetWriter(io.Discard),
510522
OptionShowBytes(true),
523+
OptionShowTotalBytes(true),
511524
OptionSetWidth(10),
512525
OptionThrottle(65*time.Millisecond),
513526
OptionShowCount(),
@@ -528,6 +541,7 @@ func Default(max int64, description ...string) *ProgressBar {
528541
OptionSetDescription(desc),
529542
OptionSetWriter(os.Stderr),
530543
OptionSetWidth(10),
544+
OptionShowTotalBytes(true),
531545
OptionThrottle(65*time.Millisecond),
532546
OptionShowCount(),
533547
OptionShowIts(),
@@ -554,6 +568,7 @@ func DefaultSilent(max int64, description ...string) *ProgressBar {
554568
OptionSetDescription(desc),
555569
OptionSetWriter(io.Discard),
556570
OptionSetWidth(10),
571+
OptionShowTotalBytes(true),
557572
OptionThrottle(65*time.Millisecond),
558573
OptionShowCount(),
559574
OptionShowIts(),
@@ -1010,21 +1025,32 @@ func renderProgressBar(c config, s *state) (int, error) {
10101025
if c.showBytes {
10111026
currentHumanize, currentSuffix := humanizeBytes(s.currentBytes, c.useIECUnits)
10121027
if currentSuffix == c.maxHumanizedSuffix {
1013-
sb.WriteString(fmt.Sprintf("%s/%s%s",
1014-
currentHumanize, c.maxHumanized, c.maxHumanizedSuffix))
1015-
} else {
1028+
if c.showTotalBytes {
1029+
sb.WriteString(fmt.Sprintf("%s/%s%s",
1030+
currentHumanize, c.maxHumanized, c.maxHumanizedSuffix))
1031+
} else {
1032+
sb.WriteString(fmt.Sprintf("%s%s",
1033+
currentHumanize, c.maxHumanizedSuffix))
1034+
}
1035+
} else if c.showTotalBytes {
10161036
sb.WriteString(fmt.Sprintf("%s%s/%s%s",
10171037
currentHumanize, currentSuffix, c.maxHumanized, c.maxHumanizedSuffix))
1038+
} else {
1039+
sb.WriteString(fmt.Sprintf("%s%s", currentHumanize, currentSuffix))
10181040
}
1019-
} else {
1041+
} else if c.showTotalBytes {
10201042
sb.WriteString(fmt.Sprintf("%.0f/%d", s.currentBytes, c.max))
1043+
} else {
1044+
sb.WriteString(fmt.Sprintf("%.0f", s.currentBytes))
10211045
}
10221046
} else {
10231047
if c.showBytes {
10241048
currentHumanize, currentSuffix := humanizeBytes(s.currentBytes, c.useIECUnits)
10251049
sb.WriteString(fmt.Sprintf("%s%s", currentHumanize, currentSuffix))
1026-
} else {
1050+
} else if c.showTotalBytes {
10271051
sb.WriteString(fmt.Sprintf("%.0f/%s", s.currentBytes, "-"))
1052+
} else {
1053+
sb.WriteString(fmt.Sprintf("%.0f", s.currentBytes))
10281054
}
10291055
}
10301056
}

progressbar_test.go

+53-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"crypto/md5"
66
"encoding/hex"
77
"fmt"
8-
"github.com/chengxilo/virtualterm"
98
"io"
109
"log"
1110
"net/http"
@@ -15,6 +14,8 @@ import (
1514
"testing"
1615
"time"
1716

17+
"github.com/chengxilo/virtualterm"
18+
1819
"github.com/stretchr/testify/assert"
1920
)
2021

@@ -1063,3 +1064,54 @@ func TestOptionSetSpinnerChangeIntervalZero(t *testing.T) {
10631064
assert.Equal(t, expected[i], actuals[i])
10641065
}
10651066
}
1067+
1068+
func TestOptionShowTotalFalseDeterminate(t *testing.T) {
1069+
buf := strings.Builder{}
1070+
bar := NewOptions64(
1071+
100000000,
1072+
OptionShowBytes(true),
1073+
OptionShowCount(),
1074+
OptionSetWidth(10),
1075+
OptionShowTotalBytes(false),
1076+
OptionSetWriter(&buf),
1077+
)
1078+
1079+
bar.Add(10000)
1080+
if !strings.Contains(buf.String(), "10 kB, ") {
1081+
t.Errorf("wrong string: %s", buf.String())
1082+
}
1083+
}
1084+
1085+
func TestOptionShowTotalFalseIndeterminate(t *testing.T) {
1086+
buf := strings.Builder{}
1087+
bar := NewOptions(-1,
1088+
OptionSetWidth(10),
1089+
OptionSetDescription("indeterminate spinner"),
1090+
OptionShowIts(),
1091+
OptionShowCount(),
1092+
OptionSpinnerType(9),
1093+
OptionShowTotalBytes(false),
1094+
OptionSetWriter(&buf),
1095+
)
1096+
bar.Add(10)
1097+
if !strings.Contains(buf.String(), "10, ") {
1098+
t.Errorf("wrong string: %s", buf.String())
1099+
}
1100+
}
1101+
1102+
func TestOptionShowTotalTrueIndeterminate(t *testing.T) {
1103+
buf := strings.Builder{}
1104+
bar := NewOptions(-1,
1105+
OptionSetWidth(10),
1106+
OptionSetDescription("indeterminate spinner"),
1107+
OptionShowIts(),
1108+
OptionShowCount(),
1109+
OptionSpinnerType(9),
1110+
OptionShowTotalBytes(true),
1111+
OptionSetWriter(&buf),
1112+
)
1113+
bar.Add(10)
1114+
if !strings.Contains(buf.String(), "10/-, ") {
1115+
t.Errorf("wrong string: %s", buf.String())
1116+
}
1117+
}

0 commit comments

Comments
 (0)