-
Notifications
You must be signed in to change notification settings - Fork 0
/
scale.go
309 lines (266 loc) · 7.61 KB
/
scale.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package main
import ("os"
"bufio"
"bytes"
"flag"
"fmt"
"math"
"strconv"
"strings"
"os/signal"
"syscall"
"time"
"unsafe"
)
/****/
type winsize struct {
Row uint16
Col uint16
Xpixel uint16
Ypixel uint16
}
const (
Level5 = 0x6f
Level4 = 0x70
Level3 = 0x71
Level2 = 0x72
Level1 = 0x73
)
/** what is the size of the terminal
@param pointer to output stream
@return width, height
*/
func get_term_size(fd uintptr) (int, int) {
var sz winsize
_, _, _ = syscall.Syscall(syscall.SYS_IOCTL,
fd, uintptr(syscall.TIOCGWINSZ), uintptr(unsafe.Pointer(&sz)))
return int(sz.Col), int(sz.Row)
}
/** ESC Commands */
const (
ESC_SAVE_SCREEN = "?47h"
ESC_RESTORE_SCREEN = "?47l"
ESC_SAVE_CURSOR = "s"
ESC_RESTORE_CURSOR = "u"
ESC_BOLD_ON = "1m"
ESC_BOLD_OFF = "0m"
ESC_CURSOR_ON = "?25h"
ESC_CURSOR_OFF = "?25l"
ESC_HOME = "H"
ESC_CLEAR_DOWN = "J"
ESC_CLEAR_EOL = "K"
ESC_CLEAR_SCREEN = "2J"
ESC_CLEAR_LINE = "2K"
ESC_BOLD = "1m"
ESC_UNDER_LINE = "4m"
ESC_BLINK = "5m"
ESC_REVERSE = "7m"
ESC_END_FORMAT = "0m"
ESC_BACK_X = "D"
ESC_START_LINE = "\r"
)
/** some common symbols */
const (
RuneSterling = '£'
RuneDArrow = '↓'
RuneLArrow = '←'
RuneRArrow = '→'
RuneUArrow = '↑'
RuneBullet = '·'
RuneBoard = '░'
RuneCkBoard = '▒'
RuneDegree = '°'
RuneDiamond = '◆'
RuneGEqual = '≥'
RunePi = 'π'
RuneHLine = '─'
RuneLantern = '§'
RunePlus = '┼'
RuneLEqual = '≤'
RuneLLCorner = '└'
RuneLRCorner = '┘'
RuneNEqual = '≠'
RunePlMinus = '±'
RuneS1 = '⎺'
RuneS3 = '⎻'
RuneS7 = '⎼'
RuneS9 = '⎽'
RuneBlock = '█'
RuneTTee = '┬'
RuneRTee = '┤'
RuneLTee = '├'
RuneBTee = '┴'
RuneULCorner = '┌'
RuneURCorner = '┐'
RuneVLine = '│' //'│'
RuneUVLine = '╷'
RuneDVLine = '╵'
)
func Print(esc string) {
fmt.Fprintf(os.Stdout, "%s", esc)
}
func PrintCtrAt(esc string, y, x int) {
fmt.Fprintf(os.Stdout, "\033[%d;%dH\033[%s", y, x)
}
func PrintAlt(esc string) {
//printf "\x1b(0\x73\x72\x71\x70\x6f\x1b(B\n"
//fmt.Fprintf(os.Stdout, "\033(0%s\033(B", esc)
fmt.Fprintf(os.Stdout, "\x1b(0%s\x1b(B", esc)
}
/**
print out an esc control
@param esc control code to print out
*/
func PrintCtrOnErr(esc string) {
fmt.Fprintf(os.Stderr, "\033[%s", esc)
}
func PrintCtrOnErrAt(esc string, y, x int) {
fmt.Fprintf(os.Stderr, "\033[%d;%dH\033[%s", y, x)
}
/**
print a string on the error console at a specific location on the screen
@param msg text to print out
@param y row to print on
@param x col to print one
*/
func PrintStrOnErrAt(msg string, y, x int) {
fmt.Fprintf(os.Stderr, "\033[%d;%dH%s", y, x, msg)
}
/** Save the screen setup at the start of the app */
func ScrSave() {
PrintCtrOnErr(ESC_SAVE_SCREEN)
PrintCtrOnErr(ESC_SAVE_CURSOR)
PrintCtrOnErr(ESC_CURSOR_OFF)
PrintCtrOnErr(ESC_CLEAR_SCREEN)
}
/** Restore the screen setup from SrcSave() */
func ScrRestore() {
PrintCtrOnErr(ESC_CURSOR_ON)
PrintCtrOnErr(ESC_RESTORE_CURSOR)
PrintCtrOnErr(ESC_RESTORE_SCREEN)
}
/**
get the current time as a timestamp
@return timestamp
*/
func makeTimestamp() int64 {
return time.Now().UnixNano() / int64(time.Millisecond)
}
/** sleep for a second */
func WaitSecond() {time.Sleep(1*1000 * time.Millisecond)}
func PrintScale(min float64, num float64, max float64) {
PrintAlt(Scale(min, num, max))
}
func Scale(min float64, num float64, max float64) string {
/* */
var ans = math.Round((num*5.0)/(max-min))
var out = ""
//fmt.Printf("ans:%f num:%f max:%f min:%f\n", ans, num, max-min, min)
if (ans<=1.0){
out = string(RuneS9)
} else if (ans<=2.0) {
out = string(RuneS7)
} else if (ans<=3.0) {
out = string(RuneHLine)
} else if (ans<=4.0) {
out = string(RuneS3)
} else if (ans<=5.0) {
out = string(RuneS1)
}
return out
}
func getWidth() uint {
ws := &winsize{}
retCode, _, errno := syscall.Syscall(syscall.SYS_IOCTL,
uintptr(syscall.Stdin),
uintptr(syscall.TIOCGWINSZ),
uintptr(unsafe.Pointer(ws)))
if int(retCode) == -1 {
panic(errno)
}
return uint(ws.Col)
}
func main() {
//trap control - c
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
fmt.Printf("\033[2K\r") //clear line and move back to beginning
fmt.Printf("\n\033[2K\r") //clear line and move back to beginning
fmt.Printf("\n\033[2K\r") //clear line and move back to beginning
os.Exit(0)
}()
var ceil *float64 = flag.Float64("ceil", 1.0, "highest possible value, assume 1.0")
var floor *float64 = flag.Float64("floor", 0.0, "lowest possible value, assume 0.0")
var dynamic = flag.Bool("dynamic", true, "Adjust the floor and ceiling dynamically")
var dump = *flag.Bool("dump", false, "Don't use terminal esc sequences")
var wait = flag.Int("wait", 2000, "delay between reads in milliseconds")
flag.Parse()
/*if 0==flag.NFlag() || *help {
flag.PrintDefaults()
}*/
//ScrSave()
scanner := bufio.NewScanner(os.Stdin)
var buf bytes.Buffer
var avg_sum float64 = 0.0
var avg_count int = 0
var avg float64 = 0.0
for scanner.Scan() {
var raw = scanner.Text()
var list = strings.Split(raw, " ")
for _, item := range list {
f, _ := strconv.ParseFloat(item, 64)
avg_sum += f
avg_count++
avg = avg_sum / float64(avg_count)
if (*dynamic) {
if f<*floor {
//buf.WriteString("\\033[31m")
buf.WriteRune(RuneDArrow)
//buf.WriteString("\\033[0m")
*floor = f
}
if *ceil<f {
buf.WriteRune(RuneUArrow)
*ceil = f
}
}
buf.WriteString(Scale(*floor, f, *ceil))
//limit buffer to this size
var width, _ = get_term_size(uintptr(syscall.Stdout))
var buf_count = strings.Count(buf.String(), "") + 0
if (buf_count>=width) {
var hold_it string = buf.String()[buf_count-width:]
buf.Reset()
buf.WriteString(hold_it)
}
/******************************************************/
//print first line
if (!dump) {
fmt.Printf("\033[2K\r") //clear line and move back to beginning
}
fmt.Printf(buf.String())
//print second line
if (!dump) {
fmt.Printf("\n\033[2K\r")
fmt.Printf("floor: %f, avg: %f, ceil: %f", *floor, avg, *ceil)
}
//print third line
if (!dump) {
fmt.Printf("\n\033[2K\r")
fmt.Printf("w=%d, c=%d", buf_count, avg_count)
}
//reset back to first line
if (!dump) {
fmt.Printf("\n\033[3A")
time.Sleep( time.Duration(*wait) * time.Millisecond)
}
}
}
if (dump) {
fmt.Printf("\nfloor: %f, avg: %f, ceil: %f", *floor, avg, *ceil)
} else {
fmt.Printf("\n\n\n")
}
}