-
Notifications
You must be signed in to change notification settings - Fork 2
/
termchart.go
193 lines (176 loc) · 4.18 KB
/
termchart.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
package main
import (
"bufio"
"flag"
"math"
"os"
"strconv"
"strings"
log "github.com/Sirupsen/logrus"
"github.com/gizak/termui"
)
var (
colorForeground = termui.ColorWhite
)
// TODO:
// - Different chart types
// - Check sizing
func drawTermChart(label []string, data []int) {
// Determine width of chart
dataPoints := len(data)
minBarWidth := 7
barGap := 1
width := (minBarWidth * dataPoints) + (barGap * (dataPoints - 2))
termWidth := termui.TermWidth()
termHeight := termui.TermHeight()
// If we can fit it all in one screen
if width < termWidth {
bc := termui.NewBarChart()
bc.Data = data
bc.Height = termHeight
bc.Width = termWidth
bc.BarWidth = (termWidth - dataPoints - 2) / dataPoints
bc.BarGap = barGap
bc.DataLabels = label
bc.TextColor = colorForeground
bc.BarColor = termui.ColorBlue
bc.NumColor = colorForeground
termui.Body.AddRows(
termui.NewRow(
termui.NewCol(12, 0, bc),
))
} else {
requiredCharts := int(math.Ceil(float64(width) / 200.0))
var datas = make([][]int, requiredCharts)
var labels = make([][]string, requiredCharts)
// Split the data
for i := 0; i < dataPoints; i++ {
chartNo := (i % requiredCharts)
datas[chartNo] = append(datas[chartNo], data[i])
labels[chartNo] = append(labels[chartNo], label[i])
}
// Create the charts
barCharts := make([]termui.Bufferer, requiredCharts)
for z := 0; z < requiredCharts; z++ {
bc := termui.NewBarChart()
bclabels := labels[z]
bc.Data = datas[z]
bc.Height = termHeight / requiredCharts
bc.BarWidth = (termWidth - (dataPoints / requiredCharts) - 2) / (dataPoints / requiredCharts)
bc.BarGap = barGap
bc.DataLabels = bclabels
bc.TextColor = colorForeground
bc.BarColor = termui.ColorBlue
bc.NumColor = colorForeground
bc.SetY(z * 30)
barCharts[z] = bc
termui.Body.AddRows(
termui.NewRow(
termui.NewCol(12, 0, bc),
),
)
}
}
termui.Body.Align()
termui.Render(termui.Body)
}
func tryDetectDelimeter(l string) string {
var bd string
md := 0
ds := []string{",", "|", "\t", "."}
for _, d := range ds {
c := strings.Split(l, d)
if len(c) > md {
md = len(c)
bd = d
}
}
return bd
}
func tryDetectColTypes(l string, d string) []string {
cols := strings.Split(l, d)
colTypes := make([]string, len(cols))
for i, col := range cols {
_, err := strconv.ParseInt(col, 10, 64)
if err == nil {
colTypes[i] = "int"
continue
}
_, err = strconv.ParseFloat(col, 10)
if err == nil {
colTypes[i] = "float"
continue
}
_, err = strconv.ParseBool(col)
if err == nil {
colTypes[i] = "bool"
continue
}
colTypes[i] = "string"
}
return colTypes
}
func mustParseFlags() {
var theme string
flag.StringVar(&theme, "theme", "dark", "color theme to use; one of: light, dark")
flag.Parse()
if theme != "light" && theme != "dark" {
log.WithFields(log.Fields{"value": theme}).Fatal("unsupported theme name")
}
if theme == "light" {
colorForeground = termui.ColorBlack
}
}
func main() {
log.SetFormatter(&log.JSONFormatter{})
var (
data []int
labels []string
delim string
rows int
)
mustParseFlags()
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := scanner.Text()
if line == "" {
break
}
if rows == 0 {
delim = tryDetectDelimeter(line)
}
values := strings.Split(line, delim)
intVal, err := strconv.ParseInt(values[1], 10, 64)
if err != nil {
log.WithField("value", values[1]).Fatal("Failed to convert input data to int")
}
labels = append(labels, values[0])
data = append(data, int(intVal))
rows++
}
if err := termui.Init(); err != nil {
log.WithField("value", err).Fatal("Failed to start termui")
}
defer termui.Close()
// Handlers
termui.Handle("/sys/kbd/q", func(termui.Event) {
termui.StopLoop()
})
termui.Handle("/sys/kbd/C-c", func(termui.Event) {
termui.StopLoop()
})
termui.Handle("/sys/kbd/C-d", func(termui.Event) {
termui.StopLoop()
})
termui.Handle("/sys/kbd/C-x", func(termui.Event) {
termui.StopLoop()
})
termui.Handle("/sys/wnd/resize", func(e termui.Event) {
termui.Body.Width = termui.TermWidth()
termui.Body.Align()
termui.Clear()
termui.Render(termui.Body)
})
drawTermChart(labels, data)
termui.Loop()
}