-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprogression_helper.go
93 lines (79 loc) · 2.38 KB
/
progression_helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package styledconsole
import (
"fmt"
"math"
"os"
"strconv"
"strings"
"time"
"golang.org/x/term"
)
var progressBarLength int = 19
var progressStarted bool = false
var progressTotalSteps int
var progressDone int
var lastPrintAdvancement int
var lastPrintTime time.Time
// ProgressStart starts a progress bar of a given duration
func ProgressStart(totalSteps int) {
if progressStarted || totalSteps < 0 {
return
}
fmt.Printf(" %s", buildProgressBar(0, totalSteps))
if !term.IsTerminal(int(os.Stdout.Fd())) {
fmt.Print("\n")
}
progressStarted = true
progressTotalSteps = totalSteps
progressDone = 0
lastPrintAdvancement = 0
lastPrintTime = time.Now()
}
// ProgressAdvance advances the current progress bar of a given stepCount. If there is no progressBar it does nothing
func ProgressAdvance(stepCount int) {
if !progressStarted || stepCount == 0 {
return
}
if progressDone+stepCount < progressTotalSteps {
progressDone += stepCount
if term.IsTerminal(int(os.Stdout.Fd())) {
if time.Since(lastPrintTime) > 1e8 {
// Do not refresh faster than 10fps, to avoid stdout-induced lag
fmt.Printf("\033[1000D %s", buildProgressBar(progressDone, progressTotalSteps))
lastPrintTime = time.Now()
}
} else if (progressDone - lastPrintAdvancement) >= int(float64(progressTotalSteps)*0.05) {
fmt.Printf(" %s\n", buildProgressBar(progressDone, progressTotalSteps))
lastPrintAdvancement = progressDone
}
} else {
progressDone = progressTotalSteps
ProgressFinish()
}
}
// ProgressFinish finishes the current progress bar. If there is no progressBar it does nothing
func ProgressFinish() {
if !progressStarted {
return
}
if term.IsTerminal(int(os.Stdout.Fd())) {
fmt.Printf("\033[1000D %s\n", buildProgressBar(progressTotalSteps, progressTotalSteps))
} else {
fmt.Printf(" %s\n", buildProgressBar(progressTotalSteps, progressTotalSteps))
}
progressStarted = false
}
func buildProgressBar(done int, total int) string {
advancementRatio := float64(done) / float64(total)
advancement := int(math.Round(advancementRatio * float64(progressBarLength)))
maxDigits := int(math.Ceil(math.Log10(float64(total + 1))))
paddedDone := fmt.Sprintf("%-"+strconv.Itoa(maxDigits)+"d", done)
return fmt.Sprintf(
"%s/%d [%s>%s] %3d%%",
paddedDone,
total,
strings.Repeat("=", advancement),
strings.Repeat("-", progressBarLength-advancement),
int(advancementRatio*100),
)
}