-
Notifications
You must be signed in to change notification settings - Fork 12
/
utils.go
178 lines (145 loc) · 2.99 KB
/
utils.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
package tvxwidgets
import (
"math"
"strings"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
type drawLineMode int
const (
horizontalLine drawLineMode = iota
verticalLine
)
const (
// gauge cell.
prgCell = "▉"
// form height.
dialogFormHeight = 3
// gauge warning percentage.
gaugeWarnPc = 60.00
// gauge critical percentage.
gaugeCritPc = 85.00
// gauge min percentage.
gaugeMinPc = 0.00
// gauge max percentage.
gaugeMaxPc = 100
// dialog padding.
dialogPadding = 2
// empty space parts.
emptySpaceParts = 2
brailleOffsetRune = '\u2800'
dotRune = '\u25CF'
fullBlockRune = '\u2588'
)
var (
brailleRune = [4][2]rune{ //nolint:gochecknoglobals
{'\u0001', '\u0008'},
{'\u0002', '\u0010'},
{'\u0004', '\u0020'},
{'\u0040', '\u0080'},
}
barsRune = [...]rune{' ', '▁', '▂', '▃', '▄', '▅', '▆', '▇', '█'} //nolint:gochecknoglobals
)
// getColorName returns convert tcell color to its name.
func getColorName(color tcell.Color) string {
for name, c := range tcell.ColorNames {
if c == color {
return name
}
}
return ""
}
// getMessageWidth returns width size for dialogs based on messages.
func getMessageWidth(message string) int {
var messageWidth int
for _, msg := range strings.Split(message, "\n") {
if len(msg) > messageWidth {
messageWidth = len(msg)
}
}
return messageWidth
}
// returns max values in 2D float64 slices.
func getMaxFloat64From2dSlice(slices [][]float64) float64 {
if len(slices) == 0 {
return 0
}
var (
maxValue float64
maxIsInit bool
)
for _, slice := range slices {
for _, val := range slice {
if math.IsNaN(val) {
continue
}
if !maxIsInit {
maxIsInit = true
maxValue = val
continue
}
if val > maxValue {
maxValue = val
}
}
}
return maxValue
}
func getMinFloat64From2dSlice(slices [][]float64) float64 {
if len(slices) == 0 {
return 0
}
var (
minValue float64
minIsInit bool
)
for _, slice := range slices {
for _, val := range slice {
if math.IsNaN(val) {
continue
}
if !minIsInit {
minIsInit = true
minValue = val
continue
}
if val < minValue {
minValue = val
}
}
}
return minValue
}
// returns max values in float64 slices.
func getMaxFloat64FromSlice(slice []float64) float64 {
if len(slice) == 0 {
return 0
}
maxValue := -1.0
for i := range slice {
if math.IsNaN(slice[i]) {
continue
}
if slice[i] > maxValue {
maxValue = slice[i]
}
}
return maxValue
}
func absInt(x int) int {
if x >= 0 {
return x
}
return -x
}
func drawLine(screen tcell.Screen, startX int, startY int, length int, mode drawLineMode, style tcell.Style) {
if mode == horizontalLine {
for i := range length {
tview.PrintJoinedSemigraphics(screen, startX+i, startY, tview.BoxDrawingsLightTripleDashHorizontal, style)
}
} else if mode == verticalLine {
for i := range length {
tview.PrintJoinedSemigraphics(screen, startX, startY+i, tview.BoxDrawingsLightTripleDashVertical, style)
}
}
}