-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogress_bar.go
More file actions
344 lines (306 loc) · 13.8 KB
/
progress_bar.go
File metadata and controls
344 lines (306 loc) · 13.8 KB
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package consolizer
import (
"fmt"
"github.com/supercom32/consolizer/memory"
"github.com/supercom32/consolizer/stringformat"
"github.com/supercom32/consolizer/constants"
"github.com/supercom32/consolizer/types"
"github.com/u2takey/go-utils/strings"
)
type ProgressBarInstanceType struct {
BaseControlInstanceType
}
type progressBarType struct{}
var ProgressBar progressBarType
var ProgressBars = memory.NewControlMemoryManager[types.ProgressBarEntryType]()
// ============================================================================
// REGULAR ENTRY
// ============================================================================
func (shared *ProgressBarInstanceType) AddToTabIndex() {
addTabIndex(shared.layerAlias, shared.controlAlias, constants.CellTypeProgressBar)
}
func (shared *ProgressBarInstanceType) Delete() *ProgressBarInstanceType {
if ProgressBars.IsExists(shared.layerAlias, shared.controlAlias) {
ProgressBars.Remove(shared.layerAlias, shared.controlAlias)
}
return nil
}
func (shared *ProgressBarInstanceType) SetProgressBarValue(value int) {
progressBarEntry := ProgressBars.Get(shared.layerAlias, shared.controlAlias)
if value <= progressBarEntry.MaxValue {
progressBarEntry.Value = value
} else {
progressBarEntry.Value = progressBarEntry.MaxValue
}
}
func (shared *ProgressBarInstanceType) SetProgressBarMaxValue(value int) {
progressBarEntry := ProgressBars.Get(shared.layerAlias, shared.controlAlias)
if value > 0 {
progressBarEntry.MaxValue = value
}
}
func (shared *ProgressBarInstanceType) SetProgressBarLabel(label string) {
progressBarEntry := ProgressBars.Get(shared.layerAlias, shared.controlAlias)
progressBarEntry.Label = label
}
func (shared *ProgressBarInstanceType) IncrementProgressBarValue() {
progressBarEntry := ProgressBars.Get(shared.layerAlias, shared.controlAlias)
progressBarEntry.Value = progressBarEntry.Value + 1
if progressBarEntry.Value > progressBarEntry.MaxValue {
progressBarEntry.Value = progressBarEntry.MaxValue
}
}
func (shared *ProgressBarInstanceType) GetProgressBarValueAsRatio() string {
progressBarEntry := ProgressBars.Get(shared.layerAlias, shared.controlAlias)
valueAsString := fmt.Sprintf("%d/%d", progressBarEntry.Value, progressBarEntry.MaxValue)
return valueAsString
}
func (shared *ProgressBarInstanceType) GetProgressBarValue() int {
progressBarEntry := ProgressBars.Get(shared.layerAlias, shared.controlAlias)
return progressBarEntry.Value
}
func (shared *ProgressBarInstanceType) GetProgressBarValueAsPercent() string {
var returnValue int
progressBarEntry := ProgressBars.Get(shared.layerAlias, shared.controlAlias)
if progressBarEntry.MaxValue > 0 {
returnValue = (progressBarEntry.Value * 100) / progressBarEntry.MaxValue
}
return fmt.Sprintf("%d", returnValue)
}
func (shared *progressBarType) Add(layerAlias string, progressBarAlias string, progressBarLabel string, styleEntry types.TuiStyleEntryType, xLocation int, yLocation int, width int, height int, isVertical bool, value int, maxValue int, isBackgroundTransparent bool) ProgressBarInstanceType {
progressBarEntry := types.NewProgressBarEntry()
progressBarEntry.StyleEntry = styleEntry
progressBarEntry.Alias = progressBarAlias
progressBarEntry.Label = progressBarLabel
progressBarEntry.Value = value
progressBarEntry.MaxValue = maxValue
progressBarEntry.IsBackgroundTransparent = isBackgroundTransparent
progressBarEntry.XLocation = xLocation
progressBarEntry.YLocation = yLocation
progressBarEntry.Width = width
progressBarEntry.Height = height
progressBarEntry.IsVertical = isVertical
progressBarEntry.TooltipAlias = stringformat.GetLastSortedUUID()
// Create associated tooltip (always created but disabled by default)
tooltipInstance := Tooltip.Add(layerAlias, progressBarEntry.TooltipAlias, "", styleEntry,
progressBarEntry.XLocation, progressBarEntry.YLocation,
progressBarEntry.Width, progressBarEntry.Height,
progressBarEntry.XLocation, progressBarEntry.YLocation+progressBarEntry.Height+1,
progressBarEntry.Width, 3,
false, true, constants.DefaultTooltipHoverTime)
tooltipInstance.SetEnabled(false)
tooltipInstance.setParentControlAlias(progressBarAlias)
// Use the ControlMemoryManager to add the progress bar entry
ProgressBars.Add(layerAlias, progressBarAlias, &progressBarEntry)
var progressBarInstance ProgressBarInstanceType
progressBarInstance.layerAlias = layerAlias
progressBarInstance.controlAlias = progressBarAlias
progressBarInstance.controlType = "progressbar"
return progressBarInstance
}
/*
DeleteButton allows you to remove a button from a text layer. In addition,
the following information should be noted:
- If you attempt to delete a button which does not exist, then the request
will simply be ignored.
*/
func (shared *progressBarType) DeleteProgressBar(layerAlias string, progressBarAlias string) {
Buttons.Remove(layerAlias, progressBarAlias)
}
func (shared *progressBarType) DeleteAllProgressBars(layerAlias string) {
ProgressBars.RemoveAll(layerAlias)
}
/*
drawButtonsOnLayer allows you to draw all buttons on a given text layer.
*/
func (shared *progressBarType) drawProgressBarsOnLayer(layerEntry types.LayerEntryType) {
layerAlias := layerEntry.LayerAlias
for _, currentProgressBarEntry := range ProgressBars.GetAllEntries(layerAlias) {
progressBarEntry := currentProgressBarEntry
drawProgressBar(&layerEntry, progressBarEntry.Alias, progressBarEntry.Label, progressBarEntry.StyleEntry, progressBarEntry.XLocation, progressBarEntry.YLocation, progressBarEntry.Width, progressBarEntry.Height, progressBarEntry.Value, progressBarEntry.MaxValue, progressBarEntry.IsBackgroundTransparent, progressBarEntry.IsVertical)
}
}
// getHorizontalPartialFillChar returns the appropriate block character for a partial horizontal fill
func getHorizontalPartialFillChar(partialFill float64) rune {
// Map the partial fill (0.0 to 1.0) to one of the block characters
// For horizontal bars, we use the left-partial block characters
if partialFill < 0.125 {
return constants.CharBlockLeftOneEighth
} else if partialFill < 0.25 {
return constants.CharBlockLeftOneQuarter
} else if partialFill < 0.375 {
return constants.CharBlockLeftThreeEighths
} else if partialFill < 0.5 {
return constants.CharBlockLeftHalf
} else if partialFill < 0.625 {
return constants.CharBlockLeftFiveEighths
} else if partialFill < 0.75 {
return constants.CharBlockLeftThreeQuarters
} else if partialFill < 0.875 {
return constants.CharBlockLeftSevenEighths
} else {
return constants.CharBlockFull
}
}
// getVerticalPartialFillChar returns the appropriate block character for a partial vertical fill
func getVerticalPartialFillChar(partialFill float64) rune {
// Map the partial fill (0.0 to 1.0) to one of the block characters
// For vertical bars, we use the lower-partial block characters
if partialFill < 0.125 {
return constants.CharBlockLowerOneEighth
} else if partialFill < 0.25 {
return constants.CharBlockLowerOneQuarter
} else if partialFill < 0.375 {
return constants.CharBlockLowerThreeEighths
} else if partialFill < 0.5 {
return constants.CharBlockLowerHalf
} else if partialFill < 0.625 {
return constants.CharBlockLowerFiveEighths
} else if partialFill < 0.75 {
return constants.CharBlockLowerThreeQuarters
} else if partialFill < 0.875 {
return constants.CharBlockLowerSevenEighths
} else {
return constants.CharBlockFull
}
}
func drawProgressBar(layerEntry *types.LayerEntryType, progressBarAlias string, progressBarLabel string, styleEntry types.TuiStyleEntryType, xLocation int, yLocation int, width int, height int, currentValue int, maxValue int, isBackgroundTransparent bool, isVertical bool) {
attributeEntry := types.NewAttributeEntry()
attributeEntry.ForegroundColor = styleEntry.ProgressBar.UnfilledForegroundColor
attributeEntry.BackgroundColor = styleEntry.ProgressBar.UnfilledBackgroundColor
attributeEntry.CellType = constants.CellTypeProgressBar
attributeEntry.CellControlAlias = progressBarAlias
if height < 1 {
height = 1
}
// Fill the entire area with the unfilled pattern
unfilledPattern := styleEntry.ProgressBar.UnfilledPattern
// If in high-res mode do not use a pattern and simply use a block character since it is the only valid one.
if styleEntry.ProgressBar.IsHighResolution {
unfilledPattern = constants.CharBlockSolid
}
fillArea(layerEntry, attributeEntry, string(unfilledPattern), xLocation, yLocation, width, height, constants.CellTypeProgressBar)
// Calculate and draw the filled portion based on orientation
attributeEntry.ForegroundColor = styleEntry.ProgressBar.FilledForegroundColor
attributeEntry.BackgroundColor = styleEntry.ProgressBar.FilledBackgroundColor
// Create a separate attribute entry for partial fill characters
partialFillAttributeEntry := types.NewAttributeEntry(&attributeEntry)
partialFillAttributeEntry.ForegroundColor = styleEntry.ProgressBar.FilledForegroundColor
// Use the unfilled background color to avoid visible color differences for partial fill characters
partialFillAttributeEntry.BackgroundColor = styleEntry.ProgressBar.UnfilledBackgroundColor
if !isVertical {
// Horizontal progress bar (left to right)
progressBarWidthExact := float64(width) * float64(currentValue) / float64(maxValue)
fullCellsCount := int(progressBarWidthExact)
// Fill the complete cells
if fullCellsCount > 0 {
fillArea(layerEntry, attributeEntry, string(styleEntry.ProgressBar.FilledPattern), xLocation, yLocation, fullCellsCount, height, constants.CellTypeProgressBar)
}
// Calculate the partial fill for the last cell
partialFill := progressBarWidthExact - float64(fullCellsCount)
if partialFill > 0 && fullCellsCount < width {
if styleEntry.ProgressBar.IsHighResolution {
// Map the partial fill to one of the block characters
partialChar := getHorizontalPartialFillChar(partialFill)
// Draw the partial fill character
for h := 0; h < height; h++ {
printLayer(layerEntry, partialFillAttributeEntry, xLocation+fullCellsCount, yLocation+h, []rune{partialChar})
}
} else {
// For low resolution, only fill whole blocks
// If partialFill is significant enough (e.g., > 0.5), consider it a full block
if partialFill > 0.5 && fullCellsCount < width {
for h := 0; h < height; h++ {
printLayer(layerEntry, attributeEntry, xLocation+fullCellsCount, yLocation+h, []rune{styleEntry.ProgressBar.FilledPattern})
}
}
}
}
} else {
// Vertical progress bar (bottom to top)
progressBarHeightExact := float64(height) * float64(currentValue) / float64(maxValue)
fullCellsCount := int(progressBarHeightExact)
// Calculate the starting position for the filled area
fillStartY := yLocation + height - fullCellsCount
// Fill the complete cells
if fullCellsCount > 0 {
fillArea(layerEntry, attributeEntry, string(styleEntry.ProgressBar.FilledPattern), xLocation, fillStartY, width, fullCellsCount, constants.CellTypeProgressBar)
}
// Calculate the partial fill for the last cell
partialFill := progressBarHeightExact - float64(fullCellsCount)
if partialFill > 0 && fullCellsCount < height {
if styleEntry.ProgressBar.IsHighResolution {
// Map the partial fill to one of the block characters
partialChar := getVerticalPartialFillChar(partialFill)
// Draw the partial fill character
partialCellY := fillStartY - 1
if partialCellY >= yLocation {
for w := 0; w < width; w++ {
printLayer(layerEntry, partialFillAttributeEntry, xLocation+w, partialCellY, []rune{partialChar})
}
}
} else {
// For low resolution, only fill whole blocks
// If partialFill is significant enough (e.g., > 0.5), consider it a full block
if partialFill > 0.5 && fullCellsCount < height {
partialCellY := fillStartY - 1
if partialCellY >= yLocation {
for w := 0; w < width; w++ {
printLayer(layerEntry, attributeEntry, xLocation+w, partialCellY, []rune{styleEntry.ProgressBar.FilledPattern})
}
}
}
}
}
}
// Handle label display
arrayOfRunes := stringformat.GetRunesFromString(progressBarLabel)
attributeEntry.ForegroundColor = styleEntry.ProgressBar.TextForegroundColor
attributeEntry.BackgroundColor = styleEntry.ProgressBar.TextBackgroundColor
attributeEntry.IsBackgroundTransparent = isBackgroundTransparent
if !isVertical {
// For horizontal progress bars, check if label is too long for width
if len(progressBarLabel) > width {
if width <= 3 {
// If width is too small, just show what we can or nothing
if width > 0 {
progressBarLabel = progressBarLabel[:width]
} else {
progressBarLabel = ""
}
} else {
progressBarLabel = strings.ShortenString(progressBarLabel, width-3)
progressBarLabel = progressBarLabel + "..."
}
arrayOfRunes = stringformat.GetRunesFromString(progressBarLabel)
}
// Center the label for horizontal progress bars
centerXLocation := (width - len(progressBarLabel)) / 2
centerYLocation := height / 2
printLayer(layerEntry, attributeEntry, xLocation+centerXLocation, yLocation+centerYLocation, arrayOfRunes)
} else {
// For vertical progress bars, check if label is too long for height
if len(progressBarLabel) > height {
if height <= 3 {
// If height is too small, just show what we can or nothing
if height > 0 {
progressBarLabel = progressBarLabel[:height]
} else {
progressBarLabel = ""
}
} else {
progressBarLabel = strings.ShortenString(progressBarLabel, height-3)
progressBarLabel = progressBarLabel + "..."
}
arrayOfRunes = stringformat.GetRunesFromString(progressBarLabel)
}
// For vertical progress bars, print each character vertically and center it
centerXLocation := width / 2
// Calculate vertical starting position to center the label
centerYLocation := (height - len(progressBarLabel)) / 2
// Print each character of the label vertically
for i, char := range arrayOfRunes {
printLayer(layerEntry, attributeEntry, xLocation+centerXLocation, yLocation+centerYLocation+i, []rune{char})
}
}
}