-
Notifications
You must be signed in to change notification settings - Fork 27
/
multibar.go
250 lines (217 loc) · 5.91 KB
/
multibar.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package multibar
import (
"fmt"
"reflect"
"regexp"
"strconv"
"strings"
"sync"
"time"
"github.com/sethgrid/curse"
)
type ProgressFunc func(progress int)
type BarContainer struct {
Bars []*ProgressBar
screenLines int
screenWidth int
startingLine int
totalNewlines int
historicNewlinesCounter int
history map[int]string
sync.Mutex
}
type ProgressBar struct {
Width int
Total int
LeftEnd byte
RightEnd byte
Fill byte
Head byte
Empty byte
ShowPercent bool
ShowTimeElapsed bool
StartTime time.Time
Line int
Prepend string
progressChan chan int
}
func New() (*BarContainer, error) {
// can swallow err because sensible defaults are returned from curse
width, lines, _ := curse.GetScreenDimensions()
_, line, _ := curse.GetCursorPosition()
history := make(map[int]string)
b := &BarContainer{screenWidth: width, screenLines: lines, startingLine: line, history: history}
// todo: need to figure out a way to deal with additional progressbars while the listener
// is listening. for the time being, the calling app will have to call listen after
// all bars are declared
//go b.Listen()
return b, nil
}
func (b *BarContainer) Listen() {
for len(b.Bars) == 0 {
// wait until we have some bars to work with
time.Sleep(time.Millisecond * 100)
}
cases := make([]reflect.SelectCase, len(b.Bars))
for i, bar := range b.Bars {
cases[i] = reflect.SelectCase{Dir: reflect.SelectRecv, Chan: reflect.ValueOf(bar.progressChan)}
}
remaining := len(cases)
for remaining > 0 {
chosen, value, ok := reflect.Select(cases)
if !ok {
// The chosen channel has been closed, so zero out the channel to disable the case
cases[chosen].Chan = reflect.ValueOf(nil)
remaining -= 1
continue
}
b.Bars[chosen].Update(int(value.Int()))
}
b.Println()
}
func (b *BarContainer) MakeBar(total int, prepend string) ProgressFunc {
ch := make(chan int)
bar := &ProgressBar{
Width: b.screenWidth - len(prepend) - 20,
Total: total,
Prepend: prepend,
LeftEnd: '[',
RightEnd: ']',
Fill: '=',
Head: '>',
Empty: '-',
ShowPercent: true,
ShowTimeElapsed: true,
StartTime: time.Now(),
progressChan: ch,
}
b.Bars = append(b.Bars, bar)
bar.Line = b.startingLine + b.totalNewlines
b.history[bar.Line] = ""
bar.Update(0)
b.Println()
return func(progress int) { bar.progressChan <- progress }
}
func (p *ProgressBar) AddPrepend(str string) {
p.Prepend = str
}
func (p *ProgressBar) Update(progress int) {
bar := make([]string, p.Width)
// avoid division by zero errors on non-properly constructed progressbars
if p.Width == 0 {
p.Width = 1
}
if p.Total == 0 {
p.Total = 1
}
justGotToFirstEmptySpace := true
for i, _ := range bar {
if float32(progress)/float32(p.Total) > float32(i)/float32(p.Width) {
bar[i] = string(p.Fill)
} else {
bar[i] = string(p.Empty)
if justGotToFirstEmptySpace {
bar[i] = string(p.Head)
justGotToFirstEmptySpace = false
}
}
}
percent := ""
if p.ShowPercent {
asInt := int(100 * (float32(progress) / float32(p.Total)))
padding := ""
if asInt < 10 {
padding = " "
} else if asInt < 99 {
padding = " "
}
percent = padding + strconv.Itoa(asInt) + "% "
}
timeElapsed := ""
if p.ShowTimeElapsed {
timeElapsed = " " + prettyTime(time.Since(p.StartTime))
}
// record where we are, jump to the progress bar, update it, jump back
c, _ := curse.New()
c.Move(1, p.Line)
c.EraseCurrentLine()
fmt.Printf("\r%s %s%c%s%c%s", p.Prepend, percent, p.LeftEnd, strings.Join(bar, ""), p.RightEnd, timeElapsed)
c.Move(c.StartingPosition.X, c.StartingPosition.Y)
}
func prettyTime(t time.Duration) string {
re, err := regexp.Compile(`(\d+).(\d+)(\w+)`)
if err != nil {
return err.Error()
}
parts := re.FindSubmatch([]byte(t.String()))
if len(parts) != 4 {
return "---"
}
return string(parts[1]) + string(parts[3])
}
func (b *BarContainer) addedNewlines(count int) {
b.totalNewlines += count
b.historicNewlinesCounter += count
// if we hit the bottom of the screen, we "scroll" our bar displays by pushing
// them up count lines (closer to line 0)
if b.startingLine+b.totalNewlines > b.screenLines {
b.totalNewlines -= count
for _, bar := range b.Bars {
bar.Line -= count
}
b.redrawAll(count)
}
}
func (b *BarContainer) redrawAll(moveUp int) {
c, _ := curse.New()
newHistory := make(map[int]string)
for line, printed := range b.history {
newHistory[line+moveUp] = printed
c.Move(1, line)
c.EraseCurrentLine()
c.Move(1, line+moveUp)
c.EraseCurrentLine()
fmt.Print(printed)
}
b.history = newHistory
c.Move(c.StartingPosition.X, c.StartingPosition.Y)
}
// print wrappers to capture newlines to adjust line positions on bars
func (b *BarContainer) Print(a ...interface{}) (n int, err error) {
b.Lock()
defer b.Unlock()
newlines := countAllNewlines(a...)
b.addedNewlines(newlines)
thisLine := b.startingLine + b.totalNewlines
b.history[thisLine] = fmt.Sprint(a...)
return fmt.Print(a...)
}
func (b *BarContainer) Printf(format string, a ...interface{}) (n int, err error) {
b.Lock()
defer b.Unlock()
newlines := strings.Count(format, "\n")
newlines += countAllNewlines(a...)
b.addedNewlines(newlines)
thisLine := b.startingLine + b.totalNewlines
b.history[thisLine] = fmt.Sprintf(format, a...)
return fmt.Printf(format, a...)
}
func (b *BarContainer) Println(a ...interface{}) (n int, err error) {
b.Lock()
defer b.Unlock()
newlines := countAllNewlines(a...) + 1
b.addedNewlines(newlines)
thisLine := b.startingLine + b.totalNewlines
b.history[thisLine] = fmt.Sprint(a...)
return fmt.Println(a...)
}
func countAllNewlines(interfaces ...interface{}) int {
count := 0
for _, iface := range interfaces {
switch s := iface.(type) {
case string:
count += strings.Count(s, "\n")
}
}
return count
}