aboutsummaryrefslogtreecommitdiff
path: root/progress.go
diff options
context:
space:
mode:
Diffstat (limited to 'progress.go')
-rw-r--r--progress.go40
1 files changed, 40 insertions, 0 deletions
diff --git a/progress.go b/progress.go
new file mode 100644
index 0000000..51effbe
--- /dev/null
+++ b/progress.go
@@ -0,0 +1,40 @@
+package main
+
+import (
+ "context"
+ "fmt"
+ "strings"
+ "time"
+)
+
+/*
+__________..........__________..........__________..........__________..........
+Directory does not contain SHA256.sig. Continue without verification? [no] yes
+Installing xshare58.tgz 100% |********************......| 4358 KB 00:12 ETA
+Work 100% |**********************....| 25m0s ETA
+*/
+
+func progress(current, max time.Duration) string {
+ width := time.Duration(40)
+ n := width * current / max
+ done := strings.Repeat("*", int(n))
+ left := strings.Repeat(".", int(width-n))
+ return fmt.Sprintf("|%v%v| %3d%%", done, left, 100*current/max)
+}
+
+func count(ctx context.Context, s string, d time.Duration) {
+ ctx, _ = context.WithTimeout(ctx, d)
+ start := time.Now()
+ defer fmt.Print("\n")
+ ticker := time.NewTicker(time.Second)
+ defer ticker.Stop()
+ for t := range ticker.C {
+ select {
+ case <-ctx.Done():
+ fmt.Printf("\r%-20s %5s %v", s, d, progress(d, d))
+ return
+ default:
+ fmt.Printf("\r%-20s %5s %v", s, d, progress(t.Sub(start), d))
+ }
+ }
+}