-
Notifications
You must be signed in to change notification settings - Fork 0
/
tchart.go
228 lines (203 loc) · 4.29 KB
/
tchart.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
package main
import (
"encoding/csv"
"flag"
"fmt"
"io"
"os"
"strconv"
"strings"
"time"
"github.com/s-westphal/go-tchart"
"github.com/andrew-d/go-termutil"
"github.com/nsf/termbox-go"
ui "github.com/s-westphal/termui/v3"
)
const (
histogramBinCount int = 50
defaultBufferSizeScatter int = 100
bufferSizeLine int = 500
defaultPanelType string = "L"
readFewLines int = 1
readDefaultLines int = 5
readManyLines int = 10
)
var (
delimiter = flag.String(
"delimiter",
"\t",
"Delimiter of input",
)
panelTypes = flag.String(
"panels",
"",
"Panel type of each column ("+
"'L': line chart, "+
"'S': scatter plot, "+
"'P': pie chart, "+
"'.': add to previous panel, "+
"'x': skip column"+
")",
)
dataLabelMode = flag.String(
"data-label",
"None",
"Data labels used by line chart (None, first, time)",
)
dataSpeed = flag.String(
"speed",
"medium",
"Speed at which to scroll though the data (slow, medium, fast)",
)
bufferSizeScatter = flag.Int(
"num-samples",
defaultBufferSizeScatter,
"Number of samples displayed in scatter plot",
)
)
func storeRecords(storages []*tchart.Storage, dataLabelMode string, records [][]string) {
for _, record := range records {
label := ""
switch dataLabelMode {
case "first":
label = record[0]
record = record[1:]
case "time":
now := time.Now()
label = fmt.Sprintf("%02d:%02d:%02d.%03d",
now.Hour(), now.Minute(), now.Second(), now.Nanosecond()/1e6)
}
for i := 0; i < len(record); i++ {
if x, err := strconv.ParseFloat(record[i], 64); err == nil {
storages[i].Add(x, label)
}
}
}
}
func main() {
flag.Parse()
var reader *csv.Reader
if !termutil.Isatty(os.Stdin.Fd()) {
reader = csv.NewReader(os.Stdin)
} else {
return
}
reader.Comma = []rune(*delimiter)[0]
err := ui.Init()
if err != nil {
panic(err)
}
defer ui.Close()
app := tchart.NewApp()
// read header
titles, err := reader.Read()
if err != nil {
panic(err)
}
if *dataLabelMode == "first" {
titles = titles[1:]
}
pTypes := strings.Split(*panelTypes, "")
currentPanelType := defaultPanelType
columnStorages := make([]*tchart.Storage, len(titles))
panelStorages := make([]*tchart.Storage, 0, len(titles))
for i, title := range titles {
panelType := defaultPanelType
if len(pTypes) > i {
panelType = pTypes[i]
}
switch panelType {
case "L", "S", "P":
if len(panelStorages) > 0 {
err := app.AddPanel(currentPanelType, panelStorages)
if err != nil {
panic(err)
}
}
currentPanelType = panelType
panelStorages = nil
}
var bufferSize int
switch currentPanelType {
case "L":
bufferSize = bufferSizeLine
case "S":
bufferSize = *bufferSizeScatter
case "P", "G":
bufferSize = 1
}
columnStorages[i] = tchart.NewStorage(title, bufferSize, histogramBinCount)
if panelType != "x" {
panelStorages = append(panelStorages, columnStorages[i])
}
}
if len(panelStorages) > 0 {
err := app.AddPanel(currentPanelType, panelStorages)
if err != nil {
panic(err)
}
}
app.AddInstructions()
app.Render()
pause := make(chan bool)
evt := make(chan termbox.Event)
dataChan := make(chan [][]string)
var readLines int
switch *dataSpeed {
case "slow":
readLines = readFewLines
case "medium":
readLines = readDefaultLines
case "fast":
readLines = readManyLines
}
go func() {
for {
evt <- termbox.PollEvent()
}
}()
go func() {
p := false
tick := time.Tick(time.Duration(100) * time.Millisecond)
for {
select {
case p = <-pause:
case <-tick:
if !p {
lines := make([][]string, 0, readLines)
for i := 0; i < readLines; i++ {
line, err := reader.Read()
if err != nil {
if err == io.EOF {
return
}
panic(err)
}
lines = append(lines, line)
}
dataChan <- lines
}
}
}
}()
for {
select {
case e := <-evt:
if e.Type == termbox.EventKey && e.Ch == 'q' {
return
}
if e.Type == termbox.EventKey && e.Ch == 'p' {
pause <- true
}
if e.Type == termbox.EventKey && e.Ch == 'c' {
pause <- false
}
if e.Type == termbox.EventResize {
app.Render()
}
case records := <-dataChan:
storeRecords(columnStorages, *dataLabelMode, records)
app.Update()
}
}
}