-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpb.go
More file actions
62 lines (50 loc) · 1.5 KB
/
pb.go
File metadata and controls
62 lines (50 loc) · 1.5 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
package console
import "fmt"
const (
// Stylesheet.Sequence indices for styling progress bars
PbCaseLeft = iota
PbCaseRight
PbFluid
PbVoid
PbTail
PbHead
)
type ProgressBar struct {
Stylesheet Stylesheet
// Must be in range from 0-1
Progress float64
}
func (pb *ProgressBar) Render(line []Cell) (n int, err error) {
actual_width := pb.Stylesheet.Width
actual_width -= pb.Stylesheet.Margin[Left]
actual_width -= pb.Stylesheet.Margin[Right]
var start_index int
switch pb.Stylesheet.Alignment {
case Left:
start_index = pb.Stylesheet.Margin[Left]
case Right:
start_index = len(line) - (pb.Stylesheet.Margin[Right] + pb.Stylesheet.Margin[Left] + actual_width)
case Center:
start_index = ((len(line) - start_index) / 2) - (actual_width / 2)
}
// calculate the number of progress cells to fill in
progress_cells := int(float64(actual_width-2) * pb.Progress)
if len(line) > (start_index + actual_width) {
err = fmt.Errorf("console: ProgressBar.Render: not enough space")
return
}
// [ ]
line[start_index] = pb.Stylesheet.Sequence[PbCaseLeft]
line[start_index+actual_width-1] = pb.Stylesheet.Sequence[PbCaseRight]
for i := 1; i < (actual_width - 1); i++ {
if progress_cells > i {
line[start_index+i] = pb.Stylesheet.Sequence[PbFluid]
} else if progress_cells == i {
line[start_index+i] = pb.Stylesheet.Sequence[PbHead]
} else if progress_cells < i {
line[start_index+i] = pb.Stylesheet.Sequence[PbVoid]
}
}
n = start_index + actual_width + pb.Stylesheet.Margin[Right]
return
}