-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprogressBar.go
216 lines (199 loc) · 5.33 KB
/
progressBar.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
package main
import (
"fmt"
lolgopher "github.com/kris-nova/lolgopher"
terminal "golang.org/x/crypto/ssh/terminal"
"io"
"os"
"strings"
"sync"
"time"
)
var spinnerStrings = [][2]string{
[2]string{"⡇ ", " "},
[2]string{"⠏ ", " "},
[2]string{"⠋⠁ ", " "},
[2]string{"⠉⠉ ", " "},
[2]string{"⠈⠉⠁ ", " "},
[2]string{" ⠉⠉ ", " "},
[2]string{" ⠈⠉⠁", " "},
[2]string{" ⠉⠉", " "},
[2]string{" ⠈⠙", " "},
[2]string{" ⠹", " "},
[2]string{" ⢸", " "},
[2]string{" ⢰", " ⠈"},
[2]string{" ⢠", " ⠘"},
[2]string{" ⢀", " ⠸"},
[2]string{" ", " ⢸"},
[2]string{" ", " ⣰"},
[2]string{" ", " ⢀⣠"},
[2]string{" ", " ⣀⣀"},
[2]string{" ", " ⢀⣀⡀"},
[2]string{" ", " ⣀⣀ "},
[2]string{" ", "⢀⣀⡀ "},
[2]string{" ", "⣀⣀ "},
[2]string{" ", "⣄⡀ "},
[2]string{" ", "⣆ "},
[2]string{" ", "⡇ "},
[2]string{"⡀ ", "⠇ "},
[2]string{"⡄ ", "⠃ "},
[2]string{"⡆ ", "⠁ "},
[2]string{"⡆ ", "⠁ "},
}
var nyanStrings = [2]string{
"\u001b[95m╔═══╗\u001b[0m⠡⠡",
"\u001b[95m╚═\u001b[90m(OﻌO)\u001b[0m",
}
type DummyWriter struct {
buffer *([]byte)
}
func (dw DummyWriter) Write(p []byte) (n int, err error) {
*dw.buffer = append(*dw.buffer, p...)
return len(p), nil
}
func (dw *DummyWriter) ReadBuffer() (buffer []byte) {
buffer = *dw.buffer
*dw.buffer = (*dw.buffer)[:0]
return
}
type ProgressBarWriter struct {
mux sync.Mutex
wnl *WebNodeList
barShown bool
lastBarLen int
bar1 string
bar2 string
currentSpin int
lol bool
lolWriter io.Writer
lolDw DummyWriter
}
func CreateProgressBarWriter(wnl *WebNodeList, showProgress, lol bool) (pbwRef *ProgressBarWriter, pbwoRef *PbwStdout, pbweRef *PbwStderr) {
var pbw ProgressBarWriter
var pbwo PbwStdout
var pbwe PbwStderr
pbwo.pbw = &pbw
pbwe.pbw = &pbw
pbw.wnl = wnl
pbw.lol = lol
if lol {
lolbuf := make([]byte, 0)
pbw.lolDw.buffer = &lolbuf
pbwo.outputWriter = &lolgopher.Writer{
Output: os.Stdout,
ColorMode: lolgopher.ColorModeTrueColor,
}
pbw.lolWriter = &lolgopher.Writer{
Output: &pbw.lolDw,
ColorMode: lolgopher.ColorModeTrueColor,
}
} else {
pbwo.outputWriter = os.Stdout
}
ticker := time.NewTicker(time.Millisecond * 40)
go func() {
var once sync.Once
for {
if showProgress {
once.Do(pbw.ShowBar)
}
<-ticker.C
pbw.currentSpin++
if pbw.currentSpin >= len(spinnerStrings) {
pbw.currentSpin = 0
}
pbw.updateBar()
}
}()
return &pbw, &pbwo, &pbwe
}
func (pbw *ProgressBarWriter) HideBar() {
if pbw.barShown {
esc := "\033[A\r"
os.Stderr.WriteString(esc + strings.Repeat(" ", pbw.lastBarLen) + "\n" + strings.Repeat(" ", pbw.lastBarLen) + esc)
pbw.barShown = false
}
}
func (pbw *ProgressBarWriter) ShowBar() {
if !pbw.barShown {
os.Stderr.WriteString(pbw.bar1 + "\n" + pbw.bar2)
pbw.barShown = true
}
}
func (pbw *ProgressBarWriter) getRainbow(input []byte) (rainbow []byte) {
input = append(input, '\n')
pbw.lolWriter.Write(input)
rainbow = pbw.lolDw.ReadBuffer()
rainbow = rainbow[:len(rainbow)-1]
return
}
func (pbw *ProgressBarWriter) updateBar() {
pbw.mux.Lock()
defer pbw.mux.Unlock()
if !pbw.barShown {
return
}
pbw.HideBar()
defer pbw.ShowBar()
termWidth, _, _ := terminal.GetSize(2)
done, fail, total := pbw.wnl.GetStats()
barWidth := int(termWidth - 55)
doneWidth := barWidth * done / total
percent := 100 * done / total
padWidth := barWidth - doneWidth
var loadBar1 string
var loadBar2 string
if pbw.lol {
if barWidth > 8 {
doneWidth -= 8
if doneWidth < 0 {
padWidth += doneWidth
doneWidth = 0
}
loadBar1 = string(pbw.getRainbow([]byte(strings.Repeat("⡪", doneWidth)))) + nyanStrings[0] + strings.Repeat("⠡", padWidth) + " ┋"
loadBar2 = string(pbw.getRainbow([]byte(strings.Repeat("⡪", doneWidth)))) + nyanStrings[1] + strings.Repeat("⠡", padWidth) + " ┋"
}
} else {
if barWidth > 0 {
loadBar1 = strings.Repeat("█", doneWidth) + strings.Repeat("░", padWidth) + " ┋"
loadBar2 = loadBar1
}
}
pbw.bar1 = fmt.Sprintf("%s ┋ Failed:%12d ┋ Total ┋ %s Progress ┋", spinnerStrings[pbw.currentSpin][0], fail, loadBar1)
pbw.bar2 = fmt.Sprintf("%s ┋ Completed:%9d ┋%10d ┋ %s %3d%% ┋", spinnerStrings[pbw.currentSpin][1], done, total, loadBar2, percent)
pbw.lastBarLen = int(termWidth)
}
type PbwStderr struct {
pbw *ProgressBarWriter
}
func (pbwe *PbwStderr) Write(p []byte) (n int, err error) {
pbwe.pbw.mux.Lock()
defer pbwe.pbw.mux.Unlock()
if pbwe.pbw.barShown {
pbwe.pbw.HideBar()
defer pbwe.pbw.ShowBar()
}
return os.Stderr.Write(p)
}
// This is buffered to ensure the progress bar flickers as little as possible. It won't flush automatically. Flush must be called once this is done.
type PbwStdout struct {
pbw *ProgressBarWriter
outputWriter io.Writer
buffer []byte
}
func (pbwo *PbwStdout) Write(p []byte) (n int, err error) {
pbwo.buffer = append(pbwo.buffer, p...)
return len(p), nil
}
func (pbwo *PbwStdout) Flush() {
if len(pbwo.buffer) > 0 {
pbwo.pbw.mux.Lock()
defer pbwo.pbw.mux.Unlock()
if pbwo.pbw.barShown {
pbwo.pbw.HideBar()
defer pbwo.pbw.ShowBar()
}
pbwo.outputWriter.Write(pbwo.buffer)
pbwo.buffer = pbwo.buffer[:0]
}
}