-
Notifications
You must be signed in to change notification settings - Fork 2
/
cui.go
182 lines (153 loc) · 3.13 KB
/
cui.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
package geektime
import (
"fmt"
"os"
"strings"
"syscall"
"unicode/utf8"
"golang.org/x/sys/unix"
)
type progressCUI struct {
label string
progress *progress
progressWidth int
}
func newProgressCUI(width, nameWidth int, p *progress) *progressCUI {
name := simplify(p.name)
return &progressCUI{
label: name + strings.Repeat(" ", nameWidth-calcWidth(simplify(name))+1),
progress: p,
progressWidth: width - nameWidth - 1,
}
}
func calcWidth(s string) (w int) {
for _, c := range s {
if c == '“' || c == '”' {
return 2
}
switch l := utf8.RuneLen(c); l {
case 1, 2:
w++
default:
w += 2
}
}
return
}
func (ui *progressCUI) content() string {
p := ui.progress
if p.err != nil {
return fmt.Sprintf("%s\x1b[31mFailed\x1b[0m:%s", ui.label, p.err)
}
width := ui.progressWidth - 7
percent := float64(p.current) / float64(p.total)
finishedCount := int(float64(width) * percent)
r := strings.Repeat
return fmt.Sprintf(
"%s%3d%%[%s>%s]", ui.label, int(100*percent), r("=", finishedCount), r(" ", width-finishedCount),
)
}
type cui struct {
puis []*progressCUI
width int
lineCount int
currentLine int
progressChan chan *progress
exitChan chan struct{}
}
func newCUI() *cui {
width := 200
ws, err := unix.IoctlGetWinsize(syscall.Stdout, unix.TIOCGWINSZ)
if err == nil {
width = int(ws.Col)
}
return &cui{
width: width,
progressChan: make(chan *progress, 1024),
exitChan: make(chan struct{}),
}
}
func (ui *cui) run() {
hideCursor()
defer showCursor()
go func() {
for {
OUT:
select {
case <-ui.exitChan:
return
case p, ok := <-ui.progressChan:
if !ok {
return
}
ui.drawLine(ui.findLineNo(p))
for _, p := range ui.puis {
if !p.progress.isEnd() {
break OUT
}
}
close(ui.exitChan)
}
}
}()
ui.wait()
ui.drawLeft()
ui.moveTo(len(ui.puis))
}
func (ui *cui) findLineNo(p *progress) int {
for i, p0 := range ui.puis {
if p0.progress == p {
return i
}
}
panic("cannot find progress:" + p.String())
}
func (ui *cui) moveTo(lineNo int) {
switch {
case lineNo > ui.currentLine:
moveDown(lineNo - ui.currentLine)
case lineNo < ui.currentLine:
moveUp(ui.currentLine - lineNo)
}
}
func (ui *cui) drawLine(lineNo int) {
ui.moveTo(lineNo)
ereaseCurrentLine()
fmt.Println(ui.puis[lineNo].content())
ui.currentLine = lineNo + 1
}
func (ui *cui) wait() {
select {
case <-ui.exitChan:
close(ui.progressChan)
case <-make(chan os.Signal, 1):
}
}
func (ui *cui) drawLeft() {
for p := range ui.progressChan {
ui.drawLine(ui.findLineNo(p))
}
}
func (ui *cui) subscribeEvents(bus *bus) {
bus.subscribe(eventUIUpdateProgress, func(v interface{}) {
ui.progressChan <- v.(*progress)
})
bus.subscribe(eventUIProgressTotal, func(v interface{}) {
ps := v.([]*progress)
ui.puis = make([]*progressCUI, len(ps))
width := calcMaxWidth(ps)
for i, p := range ps {
ui.puis[i] = newProgressCUI(ui.width, width, p)
ui.drawLine(i)
}
})
}
func calcMaxWidth(ps []*progress) (maxWidth int) {
for _, p := range ps {
w := calcWidth(p.name)
if maxWidth < w {
maxWidth = w
}
}
return
}