-
Notifications
You must be signed in to change notification settings - Fork 50
/
progressbar.go
246 lines (214 loc) · 6.11 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
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
package clui
import (
xs "github.com/huandu/xstrings"
term "github.com/nsf/termbox-go"
"strconv"
"strings"
)
/*
ProgressBar control visualizes the progression of extended operation.
The control has two sets of colors(almost all other controls have only
one set: foreground and background colors): for filled part and for
empty one. By default colors are the same.
*/
type ProgressBar struct {
BaseControl
direction Direction
min, max int
value int
emptyFg, emptyBg term.Attribute
titleFg term.Attribute
}
/*
CreateProgressBar creates a new ProgressBar.
parent - is container that keeps the control.
width and heigth - are minimal size of the control.
scale - the way of scaling the control when the parent is resized. Use DoNotScale constant if the
control should keep its original size.
*/
func CreateProgressBar(parent Control, width, height int, scale int) *ProgressBar {
b := new(ProgressBar)
b.BaseControl = NewBaseControl()
if height == AutoSize {
height = 1
}
if width == AutoSize {
width = 10
}
b.SetSize(width, height)
b.SetConstraints(width, height)
b.SetTabStop(false)
b.SetScale(scale)
b.min = 0
b.max = 10
b.direction = Horizontal
b.parent = parent
b.align = AlignCenter
if parent != nil {
parent.AddChild(b)
}
return b
}
// Draw repaints the control on its View surface.
// Horizontal ProgressBar supports custom title over the bar.
// One can set title using method SetTitle. There are a few
// predefined variables that can be used inside title to
// show value or total progress. Variable must be enclosed
// with double curly brackets. Available variables:
// percent - the current percentage rounded to closest integer
// value - raw ProgressBar value
// min - lower ProgressBar limit
// max - upper ProgressBar limit
// Examples:
// pb.SetTitle("{{value}} of {{max}}")
// pb.SetTitle("{{percent}}%")
func (b *ProgressBar) Draw() {
if b.hidden {
return
}
b.mtx.RLock()
defer b.mtx.RUnlock()
if b.max <= b.min {
return
}
PushAttributes()
defer PopAttributes()
fgOff, fgOn := RealColor(b.fg, b.Style(), ColorProgressText), RealColor(b.fgActive, b.Style(), ColorProgressActiveText)
bgOff, bgOn := RealColor(b.bg, b.Style(), ColorProgressBack), RealColor(b.bgActive, b.Style(), ColorProgressActiveBack)
parts := []rune(SysObject(ObjProgressBar))
cFilled, cEmpty := parts[0], parts[1]
prc := 0
if b.value >= b.max {
prc = 100
} else if b.value < b.max && b.value > b.min {
prc = (100 * (b.value - b.min)) / (b.max - b.min)
}
var title string
if b.direction == Horizontal && b.Title() != "" {
title = b.Title()
title = strings.Replace(title, "{{percent}}", strconv.Itoa(prc), -1)
title = strings.Replace(title, "{{value}}", strconv.Itoa(b.value), -1)
title = strings.Replace(title, "{{min}}", strconv.Itoa(b.min), -1)
title = strings.Replace(title, "{{max}}", strconv.Itoa(b.max), -1)
}
x, y := b.Pos()
w, h := b.Size()
if b.direction == Horizontal {
filled := prc * w / 100
sFilled := strings.Repeat(string(cFilled), filled)
sEmpty := strings.Repeat(string(cEmpty), w-filled)
for yy := y; yy < y+h; yy++ {
SetTextColor(fgOn)
SetBackColor(bgOn)
DrawRawText(x, yy, sFilled)
SetTextColor(fgOff)
SetBackColor(bgOff)
DrawRawText(x+filled, yy, sEmpty)
}
if title != "" {
shift, str := AlignText(title, w, b.align)
titleClr := RealColor(b.titleFg, b.Style(), ColorProgressTitleText)
var sOn, sOff string
if filled == 0 || shift >= filled {
sOff = str
} else if w == filled || shift+xs.Len(str) < filled {
sOn = str
} else {
r := filled - shift
sOn = xs.Slice(str, 0, r)
sOff = xs.Slice(str, r, -1)
}
SetTextColor(titleClr)
if sOn != "" {
SetBackColor(bgOn)
DrawRawText(x+shift, y, sOn)
}
if sOff != "" {
SetBackColor(bgOff)
DrawRawText(x+shift+xs.Len(sOn), y, sOff)
}
}
} else {
filled := prc * h / 100
sFilled := strings.Repeat(string(cFilled), w)
sEmpty := strings.Repeat(string(cEmpty), w)
for yy := y; yy < y+h-filled; yy++ {
SetTextColor(fgOff)
SetBackColor(bgOff)
DrawRawText(x, yy, sEmpty)
}
for yy := y + h - filled; yy < y+h; yy++ {
SetTextColor(fgOff)
SetBackColor(bgOff)
DrawRawText(x, yy, sFilled)
}
}
}
//----------------- own methods -------------------------
// SetValue sets new progress value. If value exceeds ProgressBar
// limits then the limit value is used
func (b *ProgressBar) SetValue(pos int) {
b.mtx.Lock()
defer b.mtx.Unlock()
if pos < b.min {
b.value = b.min
} else if pos > b.max {
b.value = b.max
} else {
b.value = pos
}
}
// Value returns the current ProgressBar value
func (b *ProgressBar) Value() int {
b.mtx.RLock()
defer b.mtx.RUnlock()
return b.value
}
// Limits returns current minimal and maximal values of ProgressBar
func (b *ProgressBar) Limits() (int, int) {
return b.min, b.max
}
// SetLimits set new ProgressBar limits. The current value
// is adjusted if it exceeds new limits
func (b *ProgressBar) SetLimits(min, max int) {
b.min = min
b.max = max
if b.value < b.min {
b.value = min
}
if b.value > b.max {
b.value = max
}
}
// Step increases ProgressBar value by 1 if the value is less
// than ProgressBar high limit
func (b *ProgressBar) Step() int {
b.mtx.Lock()
defer b.mtx.Unlock()
b.value++
if b.value > b.max {
b.value = b.max
}
return b.value
}
// SecondaryColors returns text and background colors for empty
// part of the ProgressBar
func (b *ProgressBar) SecondaryColors() (term.Attribute, term.Attribute) {
return b.emptyFg, b.emptyBg
}
// SetSecondaryColors sets new text and background colors for
// empty part of the ProgressBar
func (b *ProgressBar) SetSecondaryColors(fg, bg term.Attribute) {
b.emptyFg, b.emptyBg = fg, bg
}
// TitleColor returns text color of ProgressBar's title. Title
// background color always equals background color of the
// part of the ProgressBar on which it is displayed. In other
// words, background color of title is transparent
func (b *ProgressBar) TitleColor() term.Attribute {
return b.titleFg
}
// SetTitleColor sets text color of ProgressBar's title
func (b *ProgressBar) SetTitleColor(clr term.Attribute) {
b.titleFg = clr
}