summaryrefslogtreecommitdiffstats
path: root/dependencies/pkg/mod/github.com/vbauerster/mpb/v6@v6.0.4/internal
diff options
context:
space:
mode:
Diffstat (limited to 'dependencies/pkg/mod/github.com/vbauerster/mpb/v6@v6.0.4/internal')
-rw-r--r--dependencies/pkg/mod/github.com/vbauerster/mpb/v6@v6.0.4/internal/percentage.go19
-rw-r--r--dependencies/pkg/mod/github.com/vbauerster/mpb/v6@v6.0.4/internal/percentage_test.go71
-rw-r--r--dependencies/pkg/mod/github.com/vbauerster/mpb/v6@v6.0.4/internal/predicate.go6
-rw-r--r--dependencies/pkg/mod/github.com/vbauerster/mpb/v6@v6.0.4/internal/width.go10
4 files changed, 106 insertions, 0 deletions
diff --git a/dependencies/pkg/mod/github.com/vbauerster/mpb/v6@v6.0.4/internal/percentage.go b/dependencies/pkg/mod/github.com/vbauerster/mpb/v6@v6.0.4/internal/percentage.go
new file mode 100644
index 0000000..a8ef8be
--- /dev/null
+++ b/dependencies/pkg/mod/github.com/vbauerster/mpb/v6@v6.0.4/internal/percentage.go
@@ -0,0 +1,19 @@
+package internal
+
+import "math"
+
+// Percentage is a helper function, to calculate percentage.
+func Percentage(total, current int64, width int) float64 {
+ if total <= 0 {
+ return 0
+ }
+ if current >= total {
+ return float64(width)
+ }
+ return float64(int64(width)*current) / float64(total)
+}
+
+// PercentageRound same as Percentage but with math.Round.
+func PercentageRound(total, current int64, width int) float64 {
+ return math.Round(Percentage(total, current, width))
+}
diff --git a/dependencies/pkg/mod/github.com/vbauerster/mpb/v6@v6.0.4/internal/percentage_test.go b/dependencies/pkg/mod/github.com/vbauerster/mpb/v6@v6.0.4/internal/percentage_test.go
new file mode 100644
index 0000000..6d5410d
--- /dev/null
+++ b/dependencies/pkg/mod/github.com/vbauerster/mpb/v6@v6.0.4/internal/percentage_test.go
@@ -0,0 +1,71 @@
+package internal
+
+import "testing"
+
+func TestPercentage(t *testing.T) {
+ // key is barWidth
+ testSuite := map[int][]struct {
+ name string
+ total int64
+ current int64
+ expected int64
+ }{
+ 100: {
+ {"t,c,e{-1,-1,0}", -1, -1, 0},
+ {"t,c,e{0,-1,0}", 0, -1, 0},
+ {"t,c,e{0,0,0}", 0, 0, 0},
+ {"t,c,e{0,1,0}", 0, 1, 0},
+ {"t,c,e{100,0,0}", 100, 0, 0},
+ {"t,c,e{100,10,10}", 100, 10, 10},
+ {"t,c,e{100,15,15}", 100, 15, 15},
+ {"t,c,e{100,50,50}", 100, 50, 50},
+ {"t,c,e{100,99,99}", 100, 99, 99},
+ {"t,c,e{100,100,100}", 100, 100, 100},
+ {"t,c,e{100,101,101}", 100, 101, 100},
+ {"t,c,e{120,0,0}", 120, 0, 0},
+ {"t,c,e{120,10,8}", 120, 10, 8},
+ {"t,c,e{120,15,13}", 120, 15, 13},
+ {"t,c,e{120,50,42}", 120, 50, 42},
+ {"t,c,e{120,60,50}", 120, 60, 50},
+ {"t,c,e{120,99,83}", 120, 99, 83},
+ {"t,c,e{120,101,84}", 120, 101, 84},
+ {"t,c,e{120,118,98}", 120, 118, 98},
+ {"t,c,e{120,119,99}", 120, 119, 99},
+ {"t,c,e{120,120,100}", 120, 120, 100},
+ {"t,c,e{120,121,101}", 120, 121, 100},
+ },
+ 80: {
+ {"t,c,e{-1,-1,0}", -1, -1, 0},
+ {"t,c,e{0,-1,0}", 0, -1, 0},
+ {"t,c,e{0,0,0}", 0, 0, 0},
+ {"t,c,e{0,1,0}", 0, 1, 0},
+ {"t,c,e{100,0,0}", 100, 0, 0},
+ {"t,c,e{100,10,8}", 100, 10, 8},
+ {"t,c,e{100,15,12}", 100, 15, 12},
+ {"t,c,e{100,50,40}", 100, 50, 40},
+ {"t,c,e{100,99,79}", 100, 99, 79},
+ {"t,c,e{100,100,80}", 100, 100, 80},
+ {"t,c,e{100,101,81}", 100, 101, 80},
+ {"t,c,e{120,0,0}", 120, 0, 0},
+ {"t,c,e{120,10,7}", 120, 10, 7},
+ {"t,c,e{120,15,10}", 120, 15, 10},
+ {"t,c,e{120,50,33}", 120, 50, 33},
+ {"t,c,e{120,60,40}", 120, 60, 40},
+ {"t,c,e{120,99,66}", 120, 99, 66},
+ {"t,c,e{120,101,67}", 120, 101, 67},
+ {"t,c,e{120,118,79}", 120, 118, 79},
+ {"t,c,e{120,119,79}", 120, 119, 79},
+ {"t,c,e{120,120,80}", 120, 120, 80},
+ {"t,c,e{120,121,81}", 120, 121, 80},
+ },
+ }
+
+ for width, cases := range testSuite {
+ for _, tc := range cases {
+ got := int64(PercentageRound(tc.total, tc.current, width))
+ if got != tc.expected {
+ t.Errorf("width %d; %s: Expected: %d, got: %d\n", width, tc.name, tc.expected, got)
+ }
+ }
+ }
+}
diff --git a/dependencies/pkg/mod/github.com/vbauerster/mpb/v6@v6.0.4/internal/predicate.go b/dependencies/pkg/mod/github.com/vbauerster/mpb/v6@v6.0.4/internal/predicate.go
new file mode 100644
index 0000000..1e4dd24
--- /dev/null
+++ b/dependencies/pkg/mod/github.com/vbauerster/mpb/v6@v6.0.4/internal/predicate.go
@@ -0,0 +1,6 @@
+package internal
+
+// Predicate helper for internal use.
+func Predicate(pick bool) func() bool {
+ return func() bool { return pick }
+}
diff --git a/dependencies/pkg/mod/github.com/vbauerster/mpb/v6@v6.0.4/internal/width.go b/dependencies/pkg/mod/github.com/vbauerster/mpb/v6@v6.0.4/internal/width.go
new file mode 100644
index 0000000..216320f
--- /dev/null
+++ b/dependencies/pkg/mod/github.com/vbauerster/mpb/v6@v6.0.4/internal/width.go
@@ -0,0 +1,10 @@
+package internal
+
+// CheckRequestedWidth checks that requested width doesn't overflow
+// available width
+func CheckRequestedWidth(requested, available int) int {
+ if requested <= 0 || requested >= available {
+ return available
+ }
+ return requested
+}