-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstat.go
232 lines (208 loc) · 4.41 KB
/
stat.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
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
package stat
import (
"fmt"
"strconv"
"strings"
)
const (
UnitNone = iota
UnitBytes
UnitBytesFloat
UnitMetric
)
var colorNameToANSI = map[string]string{
"darkgray": "\033[1;30m",
"red": "\033[1;31m",
"green": "\033[1;32m",
"yellow": "\033[1;33m",
"blue": "\033[1;34m",
"magenta": "\033[1;35m",
"cyan": "\033[1;36m",
"white": "\033[1;37m",
}
var colorNameToHTML = map[string]string{
"darkgray": "#555753",
"red": "#EF2929",
"green": "#8AE234",
"yellow": "#FCE94F",
"blue": "#729FCF",
"magenta": "#EE38DA",
"cyan": "#34E2E2",
"white": "#EEEEEC",
}
const (
black = "\033[0;30m"
darkred = "\033[0;31m"
darkgreen = "\033[0;32m"
darkyellow = "\033[0;33m"
darkblue = "\033[0;34m"
darkmagenta = "\033[0;35m"
darkcyan = "\033[0;36m"
gray = "\033[0;37m"
darkgray = "darkgray"
red = "red"
green = "green"
yellow = "yellow"
blue = "blue"
magenta = "magenta"
cyan = "cyan"
white = "white"
blackbg = "\033[40m"
redbg = "\033[41m"
greenbg = "\033[42m"
yellowbg = "\033[43m"
bluebg = "\033[44m"
magentabg = "\033[45m"
cyanbg = "\033[46m"
whitebg = "\033[47m"
)
var colors = []string{
red,
yellow,
green,
blue,
cyan,
white,
darkred,
darkgreen,
}
type ColType int
const (
ColGauge ColType = iota
ColPercentage
)
type Col struct {
Type ColType
ValU64 uint64
ValInt int // used by ColPercentage
ValFloat64 float64 // used by UnitBytes
Unit int
Width int
Scale int
}
func (c Col) WithWidth(w int) Col {
ret := c
ret.Width = w
return ret
}
var unitsuffix = []string{"B", "k", "M", "G", "T"}
func (c Col) String() string {
return c.colorize(func(col, text string) string {
return colorNameToANSI[col] + text
})
}
func (c Col) HTML() string {
return c.colorize(func(col, text string) string {
return fmt.Sprintf(`<span style="color: %s">%s</span>`, colorNameToHTML[col], text)
})
}
func (c Col) RenderCustom(colorFunc func(col, text string) string) string {
return c.colorize(func(col, text string) string {
return colorFunc(col, text)
})
}
func (c Col) colorize(color func(col, text string) string) string {
switch c.Type {
case ColPercentage:
v := fmt.Sprintf("%"+strconv.Itoa(c.Width)+"d", int(c.ValFloat64))
col := colors[int(c.ValFloat64/float64(c.Scale))%len(colors)]
if strings.TrimSpace(v) == "0" {
return color(darkgray, v)
}
if c.ValFloat64 >= 100 {
return color(white, v)
}
return color(col, v)
case ColGauge:
if c.Unit == UnitBytes || c.Unit == UnitBytesFloat ||
c.Unit == UnitMetric {
base := 1024
if c.Unit == UnitMetric {
base = 1000
}
width := c.Width
if c.ValU64 == 0 && c.ValFloat64 == 0 {
return color(darkgray, fmt.Sprintf("%"+strconv.Itoa(width)+"d", 0))
}
width-- // for the unit suffix
var f string
var cl int
if c.Unit == UnitBytesFloat {
f, cl = fchg(c.ValFloat64, width, base)
} else {
f, cl = dchg(c.ValU64, width, base)
}
if len(f) < width {
f = strings.Repeat(" ", width-len(f)) + f
}
if cl > 0 || c.Unit == UnitBytes || c.Unit == UnitBytesFloat {
if cl < len(unitsuffix) {
f += color(darkgray, unitsuffix[cl])
} else {
f += "?"
}
} else {
f += " " // empty suffix
}
col := colors[cl%len(colors)]
return color(col, f)
}
return fmt.Sprintf("%4d", c.ValU64)
default:
return "?BUG?"
}
}
func ByteCol(v uint64) Col {
return Col{
Type: ColGauge,
Unit: UnitBytes,
ValU64: v,
}
}
func MetricCol(v uint64) Col {
return Col{
Type: ColGauge,
Unit: UnitMetric,
ValU64: v,
}
}
// -----------------------------------------------------------------------------
// dchg and fchg were ported directly from dstat.
// -----------------------------------------------------------------------------
func dchg(v uint64, width int, base int) (string, int) {
c := 0
for {
ret := strconv.FormatUint(v, 10)
if len(ret) <= width {
return ret, c
}
v = v / uint64(base)
c++
}
}
func fchg(v float64, width int, base int) (string, int) {
if v == 0 {
return "0", 0
}
c := 0
var ret string
for {
ret = strconv.Itoa(int(v))
if len(ret) <= width {
i := width - len(ret) - 1
for ; i > 0; i-- {
ret = strconv.FormatFloat(v, 'f', i, 64)
if len(ret) <= width {
break
}
}
if i == 0 {
ret = strconv.Itoa(int(v))
}
break
}
v = v / float64(base)
c++
}
return ret, c
}