forked from itchio/butler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
progress.go
153 lines (126 loc) · 2.81 KB
/
progress.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package comm
import (
"fmt"
"os"
"runtime"
"strings"
"time"
"github.com/itchio/butler/pb"
)
var bar *pb.ProgressBar
// ProgressTheme contains all the characters we need to show progress
type ProgressTheme struct {
BarStart string
BarEnd string
Current string
CurrentHalfTone string
Empty string
OpSign string
StatSign string
}
var themes = map[string]*ProgressTheme{
"unicode": {"▐", "▌", "▓", "▒", "░", "•", "✓"},
"ascii": {"|", "|", "#", "=", "-", ">", "<"},
"cp437": {"▐", "▌", "█", "▒", "░", "∙", "√"},
}
func (th *ProgressTheme) apply(bar *pb.ProgressBar) {
bar.BarStart = th.BarStart
bar.BarEnd = th.BarEnd
bar.Current = th.Current
bar.CurrentN = th.Current
bar.Empty = th.Empty
}
func getCharset() string {
if runtime.GOOS == "windows" && os.Getenv("OS") != "CYGWIN" {
return "cp437"
}
var utf8 = ".UTF-8"
if strings.Contains(os.Getenv("LC_ALL"), utf8) ||
os.Getenv("LC_CTYPE") == "UTF-8" ||
strings.Contains(os.Getenv("LANG"), utf8) {
return "unicode"
}
return "ascii"
}
var theme = themes[getCharset()]
// GetTheme returns the theme used to show progress
func GetTheme() *ProgressTheme {
return theme
}
const maxLabelLength = 40
// ProgressLabel sets the string printed next to the progress indicator
func ProgressLabel(label string) {
if bar == nil {
return
}
if len(label) > maxLabelLength {
label = fmt.Sprintf("...%s", label[len(label)-(maxLabelLength-3):])
}
bar.Postfix(label)
}
// StartProgress begins a period in which progress is regularly printed
func StartProgress() {
if bar != nil {
// Already in-progress
return
}
if settings.noProgress || settings.json {
// Don't want a bar, ever.
return
}
// shows percentages, to the 1/100th
bar = pb.New64(100 * 100)
bar.AlwaysUpdate = true
bar.RefreshRate = 250 * time.Millisecond
bar.ShowCounters = false
bar.ShowFinalTime = false
bar.TimeBoxWidth = 8
bar.BarWidth = 20
bar.SetMaxWidth(80)
theme.apply(bar)
bar.Start()
}
func PauseProgress() {
if bar != nil {
bar.AlwaysUpdate = false
}
}
func ResumeProgress() {
if bar != nil {
bar.AlwaysUpdate = true
}
}
// Progress sets the completion of a task whose progress is being printed
// It only has an effect if StartProgress was already called.
func Progress(perc float64) {
if settings.quiet {
return
}
send("progress", jsonMessage{
"percentage": perc * 100.0,
})
}
func ProgressScale(scale float64) {
if settings.quiet {
return
}
if bar != nil {
bar.SetScale(scale)
}
}
func setBarProgress(perc float64) {
if bar != nil {
bar.Set64(int64(perc * 10000.0))
}
}
// EndProgress stops refreshing the progress bar and erases it.
func EndProgress() {
if bar != nil {
bar.Set64(10000)
if !settings.noProgress {
bar.Postfix("")
bar.Finish()
}
bar = nil
}
}