-
Notifications
You must be signed in to change notification settings - Fork 398
/
ui.go
229 lines (202 loc) · 5.14 KB
/
ui.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
//-----------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license.
// See LICENSE.txt file in the project root for full license information.
//-----------------------------------------------------------------------------
package main
import (
"fmt"
"math"
"time"
"github.com/mattn/go-runewidth"
tm "github.com/nsf/termbox-go"
)
const (
lefttop = iota
horizontal
righttop
vertical
leftbottom
rightbottom
middlebottom
middletop
middleleft
middleright
middlemiddle
space
box1
box2
box3
box4
uparrow
dnarrow
)
var symbols = []rune{'┌', '─', '┐', '│', '└', '┘', '┴', '┬', '├', '┤', '┼', ' ', '░', '▒', '▓', '█', '↑', '↓'}
const (
justifyLeft = iota
justifyRight
justifyCenter
)
const (
border = iota
noBorder
)
type table struct {
ccount int
cwidth []int
x int
y int
cr int
justify int
border int
}
func init() {
if runewidth.IsEastAsian() {
symbols = []rune{'+', '-', '+', '|', '+', '+', '+', '+', '+', '+', '+', ' ', '░', '▒', '▓', '█', '^', 'v'}
}
}
func (t *table) drawTblRow(ledge, redge, middle, spr rune, fg, bg tm.Attribute) {
twidth := t.ccount + 1
for _, w := range t.cwidth {
twidth += w
}
for i := 0; i < twidth; i++ {
tm.SetCell(t.x+i, t.y+t.cr, middle, fg, bg)
}
if t.border == border {
tm.SetCell(t.x, t.y+t.cr, ledge, fg, bg)
tm.SetCell(t.x+twidth, t.y+t.cr, redge, fg, bg)
}
o := 0
for c, w := range t.cwidth {
o += w + 1
if c < t.ccount-1 {
tm.SetCell(t.x+o, t.y+t.cr, spr, fg, bg)
}
}
t.cr++
}
func (t *table) addTblRow(row []string) {
t.drawTblRow(symbols[vertical], symbols[vertical], symbols[space],
symbols[vertical], tm.ColorDefault, tm.ColorDefault)
t.cr--
o := 1
alignOffset := 0
for i := 0; i < t.ccount; i++ {
w := t.cwidth[i]
var s string
if t.justify == justifyLeft {
s = fmt.Sprintf("%-*s", w, row[i])
if i == 0 && t.border == noBorder {
alignOffset = -1
}
} else {
s = fmt.Sprintf("%*s", w, row[i])
}
printText(t.x+o+alignOffset, t.y+t.cr, w, s, tm.ColorDefault, tm.ColorDefault)
o += w + 1
alignOffset = 0
}
t.cr++
}
func (t *table) addTblSpr() {
t.drawTblRow(symbols[middleleft], symbols[middleright], symbols[horizontal],
symbols[middlemiddle], tm.ColorDefault, tm.ColorDefault)
}
func (t *table) addTblHdr() {
t.drawTblRow(symbols[lefttop], symbols[righttop], symbols[horizontal],
symbols[middletop], tm.ColorDefault, tm.ColorDefault)
}
func (t *table) addTblFtr() {
t.drawTblRow(symbols[leftbottom], symbols[rightbottom], symbols[horizontal],
symbols[middlebottom], tm.ColorDefault, tm.ColorDefault)
}
func printHLineText(x, y int, w int, text string) {
for i := 0; i < w; i++ {
tm.SetCell(x+i, y, symbols[horizontal], tm.ColorWhite, tm.ColorDefault)
}
offset := (w - runewidth.StringWidth(text)) / 2
textArr := []rune(text)
xoff := 0
for i := 0; i < len(text); i++ {
tm.SetCell(x+offset+i+xoff, y, textArr[i], tm.ColorWhite, tm.ColorDefault)
if runewidth.RuneWidth(textArr[i]) == 2 {
xoff++
}
}
}
func printVLine(x, y int, h int) {
tm.SetCell(x, y, symbols[middletop], tm.ColorWhite, tm.ColorDefault)
for i := 1; i < h; i++ {
tm.SetCell(x, y+i, symbols[vertical], tm.ColorWhite, tm.ColorDefault)
}
}
func printText(x, y, w int, text string, fg, bg tm.Attribute) {
textArr := []rune(text)
for i := 0; i < w; i++ {
tm.SetCell(x+i, y, ' ', fg, bg)
}
xoff := 0
for i := 0; i < len(textArr); i++ {
tm.SetCell(x+i+xoff, y, textArr[i], fg, bg)
if runewidth.RuneWidth(textArr[i]) == 2 {
xoff++
}
}
}
func printCenterText(x, y, w int, text string, fg, bg tm.Attribute) {
offset := (w - runewidth.StringWidth(text)) / 2
textArr := []rune(text)
for i := 0; i < w; i++ {
tm.SetCell(x+i, y, ' ', fg, bg)
}
xoff := 0
for i := 0; i < len(textArr); i++ {
tm.SetCell(x+offset+i+xoff, y, textArr[i], fg, bg)
if runewidth.RuneWidth(textArr[i]) == 2 {
xoff++
}
}
}
func printHLine(x, y int, w int) {
for i := 0; i < w; i++ {
tm.SetCell(x+i, y, symbols[horizontal], tm.ColorWhite, tm.ColorDefault)
}
}
func printUsageBar(x, y, w int, usage, scale uint64, clr tm.Attribute) {
barw := int(math.Log10(float64(uint64((usage + scale - 1) / (scale / 10)))))
if barw > w {
barw = w
} else if barw < 0 {
barw = 0
}
for j := 0; j < w; j++ {
tm.SetCell(x+j, y, symbols[box3], clr, tm.ColorDefault)
}
for j := 0; j < barw; j++ {
tm.SetCell(x+j, y, symbols[box3], clr|tm.AttrBold, clr)
}
}
func printDivider() {
ui.printMsg("-----------------------------------------------------------")
}
func printDivider2() {
ui.printMsg("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -")
}
type ethrUI interface {
fini()
getTitle() string
printMsg(format string, a ...interface{})
printErr(format string, a ...interface{})
printDbg(format string, a ...interface{})
paint(uint64)
emitTestHdr()
emitLatencyHdr()
emitLatencyResults(remote, proto string, avg, min, max, p50, p90, p95, p99, p999, p9999 time.Duration)
emitTestResultBegin()
emitTestResult(*ethrSession, EthrProtocol, uint64)
printTestResults([]string)
emitTestResultEnd()
emitStats(ethrNetStat)
}
var ui ethrUI