-
Notifications
You must be signed in to change notification settings - Fork 12
/
gauge_um.go
154 lines (131 loc) · 3.67 KB
/
gauge_um.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
package tvxwidgets
import (
"fmt"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
// UtilModeGauge represents utilisation mode gauge permitive.
type UtilModeGauge struct {
*tview.Box
// pc percentage value
pc float64
// warn percentage value
warnPc float64
// critical percentage value
critPc float64
// okColor ok color
okColor tcell.Color
// warnColor warning block color
warnColor tcell.Color
// critColor critical block color
critColor tcell.Color
// emptyColor empty block color
emptyColor tcell.Color
// label prints label on the left of the gauge
label string
// labelColor label and percentage text color
labelColor tcell.Color
}
// NewUtilModeGauge returns new utilisation mode gauge permitive.
func NewUtilModeGauge() *UtilModeGauge {
gauge := &UtilModeGauge{
Box: tview.NewBox(),
pc: gaugeMinPc,
warnPc: gaugeWarnPc,
critPc: gaugeCritPc,
warnColor: tcell.ColorOrange,
critColor: tcell.ColorRed,
okColor: tcell.ColorGreen,
emptyColor: tcell.ColorWhite,
labelColor: tview.Styles.PrimaryTextColor,
label: "",
}
return gauge
}
// SetLabel sets label for this primitive.
func (g *UtilModeGauge) SetLabel(label string) {
g.label = label
}
// SetLabelColor sets label text color.
func (g *UtilModeGauge) SetLabelColor(color tcell.Color) {
g.labelColor = color
}
// Focus is called when this primitive receives focus.
func (g *UtilModeGauge) Focus(delegate func(p tview.Primitive)) { //nolint:revive
}
// HasFocus returns whether or not this primitive has focus.
func (g *UtilModeGauge) HasFocus() bool {
return g.Box.HasFocus()
}
// GetRect return primitive current rect.
func (g *UtilModeGauge) GetRect() (int, int, int, int) {
return g.Box.GetRect()
}
// SetRect sets rect for this primitive.
func (g *UtilModeGauge) SetRect(x, y, width, height int) {
g.Box.SetRect(x, y, width, height)
}
// SetValue update the gauge progress.
func (g *UtilModeGauge) SetValue(value float64) {
if value <= float64(gaugeMaxPc) {
g.pc = value
}
}
// GetValue returns current gauge value.
func (g *UtilModeGauge) GetValue() float64 {
return g.pc
}
// Draw draws this primitive onto the screen.
func (g *UtilModeGauge) Draw(screen tcell.Screen) {
g.Box.DrawForSubclass(screen, g)
x, y, width, height := g.Box.GetInnerRect()
labelPCWidth := 7
labelWidth := len(g.label)
barWidth := width - labelPCWidth - labelWidth
for i := range barWidth {
for j := range height {
value := float64(i * 100 / barWidth)
color := g.getBarColor(value)
if value > g.pc {
color = g.emptyColor
}
tview.Print(screen, prgCell, x+labelWidth+i, y+j, 1, tview.AlignCenter, color)
}
}
// draw label
tY := y + (height / emptySpaceParts)
if labelWidth > 0 {
tview.Print(screen, g.label, x, tY, labelWidth, tview.AlignLeft, g.labelColor)
}
// draw percentage text
tview.Print(screen, fmt.Sprintf("%6.2f%%", g.pc),
x+barWidth+labelWidth,
tY,
labelPCWidth,
tview.AlignLeft,
tview.Styles.PrimaryTextColor)
}
// SetWarnPercentage sets warning percentage start range.
func (g *UtilModeGauge) SetWarnPercentage(percentage float64) {
if percentage > 0 && percentage < 100 {
g.warnPc = percentage
}
}
// SetCritPercentage sets critical percentage start range.
func (g *UtilModeGauge) SetCritPercentage(percentage float64) {
if percentage > 0 && percentage < 100 && percentage > g.warnPc {
g.critPc = percentage
}
}
func (g *UtilModeGauge) getBarColor(percentage float64) tcell.Color {
if percentage < g.warnPc {
return g.okColor
} else if percentage < g.critPc {
return g.warnColor
}
return g.critColor
}
// SetEmptyColor sets empty gauge color.
func (g *UtilModeGauge) SetEmptyColor(color tcell.Color) {
g.emptyColor = color
}