forked from navidys/tvxwidgets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgauge_pm.go
139 lines (113 loc) · 3.31 KB
/
gauge_pm.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
package tvxwidgets
import (
"fmt"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
// PercentageModeGauge represents percentage mode gauge permitive.
type PercentageModeGauge struct {
*tview.Box
// maxValue value
maxValue int
// value is current value
value int
// pgBgColor: progress block background color
pgBgColor tcell.Color
}
// NewPercentageModeGauge returns new percentage mode gauge permitive.
func NewPercentageModeGauge() *PercentageModeGauge {
gauge := &PercentageModeGauge{
Box: tview.NewBox(),
value: 0,
pgBgColor: tcell.ColorBlue,
}
return gauge
}
// Draw draws this primitive onto the screen.
func (g *PercentageModeGauge) Draw(screen tcell.Screen) {
g.Box.DrawForSubclass(screen, g)
if g.maxValue == 0 {
return
}
x, y, width, height := g.Box.GetInnerRect()
pcWidth := 3
pc := g.value * gaugeMaxPc / g.maxValue
pcString := fmt.Sprintf("%d%%", pc)
tW := width - pcWidth
tX := x + (tW / emptySpaceParts)
tY := y + height/emptySpaceParts
prgBlock := g.progressBlock(width)
style := tcell.StyleDefault.Background(g.pgBgColor).Foreground(tview.Styles.PrimaryTextColor)
for i := 0; i < height; i++ {
for j := 0; j < prgBlock; j++ {
screen.SetContent(x+j, y+i, ' ', nil, style)
}
}
// print percentage in middle of box
pcRune := []rune(pcString)
for j := 0; j < len(pcRune); j++ {
style = tcell.StyleDefault.Background(tview.Styles.PrimitiveBackgroundColor).Foreground(tview.Styles.PrimaryTextColor)
if x+prgBlock >= tX+j {
style = tcell.StyleDefault.Background(g.pgBgColor).Foreground(tview.Styles.PrimaryTextColor)
}
for i := 0; i < height; i++ {
screen.SetContent(tX+j, y+i, ' ', nil, style)
}
screen.SetContent(tX+j, tY, pcRune[j], nil, style)
}
}
// SetTitle sets title for this primitive.
func (g *PercentageModeGauge) SetTitle(title string) {
g.Box.SetTitle(title)
}
// Focus is called when this primitive receives focus.
func (g *PercentageModeGauge) Focus(delegate func(p tview.Primitive)) {
}
// HasFocus returns whether or not this primitive has focus.
func (g *PercentageModeGauge) HasFocus() bool {
return g.Box.HasFocus()
}
// GetRect return primitive current rect.
func (g *PercentageModeGauge) GetRect() (int, int, int, int) {
return g.Box.GetRect()
}
// SetRect sets rect for this primitive.
func (g *PercentageModeGauge) SetRect(x, y, width, height int) {
g.Box.SetRect(x, y, width, height)
}
// SetPgBgColor sets progress block background color.
func (g *PercentageModeGauge) SetPgBgColor(color tcell.Color) {
g.pgBgColor = color
}
// SetValue update the gauge progress.
func (g *PercentageModeGauge) SetValue(value int) {
if value <= g.maxValue {
g.value = value
}
}
// GetValue returns current gauge value.
func (g *PercentageModeGauge) GetValue() int {
return g.value
}
// SetMaxValue set maximum allows value for the gauge.
func (g *PercentageModeGauge) SetMaxValue(value int) {
if value > 0 {
g.maxValue = value
}
}
// GetMaxValue returns maximum allows value for the gauge.
func (g *PercentageModeGauge) GetMaxValue() int {
return g.maxValue
}
// Reset resets the gauge counter (set to 0).
func (g *PercentageModeGauge) Reset() {
g.value = 0
}
func (g *PercentageModeGauge) progressBlock(max int) int {
if g.maxValue == 0 {
return g.maxValue
}
pc := g.value * gaugeMaxPc / g.maxValue
value := pc * max / gaugeMaxPc
return value
}