/
main.go
324 lines (289 loc) · 8.58 KB
/
main.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package main
import (
"bufio"
"flag"
"fmt"
"github.com/nsf/termbox-go"
"github.com/pebbe/util"
"os"
"strings"
"time"
)
type ProgramState struct {
offset int
buffer []string
timestamps []int64
log map[string]*HistoricalVariable
log_keys []string // Kept around to keep order
focused_pane int
selected_index int
selected_buffer_line int
timecursor int64
realtime bool
start_time int64
}
type HistoricalVariable struct {
Name string
Values []string
Timestamp []int64
Focused int
}
func max(a, b int) int {
if a < b {
return b
}
return a
}
func min(a, b int) int {
if a > b {
return b
}
return a
}
func draw_all(state *ProgramState) {
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
width, height := termbox.Size()
// Draw the buffer
for i := state.offset; i < min(state.offset+height-1, len(state.buffer)); i++ {
if i > state.selected_buffer_line {
break
}
for j, c := range state.buffer[i] {
if j == width/2 {
break
}
termbox.SetCell(j, i-state.offset, c, termbox.ColorDefault, termbox.ColorDefault)
}
}
// Draw the divider line
for i := 0; i < height; i++ {
termbox.SetCell(width/2, i, '|', termbox.ColorDefault, termbox.ColorDefault)
}
// Draw the variables
for i, k := range state.log_keys {
v := state.log[k]
for j, c := range v.Name {
termbox.SetCell(width/2+2+j, i, c, termbox.ColorDefault, termbox.ColorDefault)
}
if !state.realtime && state.timecursor < v.Timestamp[0] {
continue
}
for j, c := range v.Values[v.Focused] {
termbox.SetCell(width/2+2+j+len(v.Name)+1, i, c, termbox.ColorDefault, termbox.ColorDefault)
}
pos := fmt.Sprintf("%d/%d", v.Focused+1, len(v.Values))
for j, c := range pos {
termbox.SetCell(width-len(pos)+j, i, c, termbox.ColorDefault, termbox.ColorDefault)
}
}
// Draw the selected index
termbox.SetCell(width/2+1, state.selected_index, '>', termbox.ColorDefault, termbox.ColorDefault)
status := "Running"
extra := ""
if !state.realtime {
status = fmt.Sprintf("Frozen @ %0.2fs", float64(state.timecursor-state.start_time)/float64(1e9))
extra = ", [ctrl-r] resume"
}
var full_status string
if state.focused_pane == 0 {
full_status = fmt.Sprintf("%s. [space] switch pane, [up/down] scroll%s", status, extra)
} else {
full_status = fmt.Sprintf("%s. [space] toggle pane, [up/down] select variable, [left/right] select frame%s", status, extra)
}
// Draw the selection box indicating which pane we're focused on
for i := 0; i < width/2; i++ {
c := ' '
if i < len(full_status) {
c = rune(full_status[i])
}
termbox.SetCell(state.focused_pane*((width/2)+1)+i, height-1, c, termbox.ColorBlack, termbox.ColorWhite)
}
termbox.Flush()
}
func seek_to_time(timestamp int64, variables *map[string]*HistoricalVariable) {
for _, v := range *variables {
// If the timestamp is before this current variable
if v.Focused > 0 && v.Timestamp[v.Focused] > timestamp {
for {
v.Focused = max(0, v.Focused-1)
if v.Focused == 0 || timestamp >= v.Timestamp[v.Focused] {
break
}
}
} else
// If the timestamp is later than this current variable
// increase the focus until the next one is after
if v.Focused < len(v.Values)-1 && v.Timestamp[v.Focused+1] <= timestamp {
for {
v.Focused = min(v.Focused+1, len(v.Values)-1)
if v.Focused == len(v.Values)-1 || v.Timestamp[v.Focused+1] >= timestamp {
break
}
}
}
}
}
// This assumes that every timestamp exists in timestamps, otherwise
// this logic is too stupid at the moment and will crash beyond the ends of the
// array bounds
func scroll_to_time(timestamp int64, current_offset int, timestamps *[]int64) int {
for timestamp > (*timestamps)[current_offset] {
current_offset += 1
}
for timestamp < (*timestamps)[current_offset] {
current_offset -= 1
}
return current_offset
}
func main() {
var prefix = flag.String("prefix", ".", "Prefix used to match lines")
var delimeter = flag.String("delimeter", "=", "Prefix used to match lines")
flag.Parse()
if util.IsTerminal(os.Stdin) {
fmt.Println("We only support streams from unix pipes at the moment, please pipe something into pfdb")
return
}
err := termbox.Init()
if err != nil {
panic(err)
}
defer termbox.Close()
termbox.SetInputMode(termbox.InputEsc | termbox.InputMouse)
state := ProgramState{
0,
[]string{},
[]int64{},
map[string]*HistoricalVariable{},
[]string{},
1,
0,
0,
int64(0),
true,
time.Now().UnixNano(),
}
stdin_channel := make(chan string)
input_channel := make(chan termbox.Event)
go func() {
bio := bufio.NewReader(os.Stdin)
for {
line, _, err := bio.ReadLine()
if err != nil {
break
}
stdin_channel <- string(line)
}
}()
go func() {
for {
input_channel <- termbox.PollEvent()
}
}()
draw_all(&state)
loop:
for {
select {
case line := <-stdin_channel:
fmt.Println("A LINE")
timestamp := time.Now().UnixNano()
if len(line) > 0 && strings.HasPrefix(line, *prefix) {
slices := strings.SplitN(strings.TrimPrefix(string(line), *prefix), *delimeter, 2)
if len(slices) == 2 {
existing := state.log[slices[0]]
if existing == nil {
existing = &HistoricalVariable{slices[0],
[]string{},
[]int64{},
0}
state.log[slices[0]] = existing
state.log_keys = append(state.log_keys, slices[0])
}
existing.Values = append(existing.Values, slices[1])
existing.Timestamp = append(existing.Timestamp, timestamp)
// Update the existing thing to realtime
if state.realtime {
existing.Focused = len(existing.Values) - 1
}
}
}
state.buffer = append(state.buffer, string(line))
state.timestamps = append(state.timestamps, timestamp)
if state.realtime {
_, height := termbox.Size()
state.offset = max(len(state.buffer)-height+1, 0)
state.selected_buffer_line = len(state.buffer) - 1
}
draw_all(&state)
case ev := <-input_channel:
switch ev.Type {
case termbox.EventKey:
switch ev.Key {
case termbox.KeyEsc:
break loop
case termbox.KeyArrowDown:
switch state.focused_pane {
case 0:
state.realtime = false
state.selected_buffer_line = min(state.selected_buffer_line+1, len(state.buffer)-1)
_, height := termbox.Size()
state.timecursor = state.timestamps[state.selected_buffer_line]
state.offset = max(state.selected_buffer_line-height+2, 0)
seek_to_time(state.timecursor, &state.log)
case 1:
state.selected_index = min(state.selected_index+1, len(state.log)-1)
}
case termbox.KeyArrowUp:
switch state.focused_pane {
case 0:
state.realtime = false
state.selected_buffer_line = max(0, state.selected_buffer_line-1)
_, height := termbox.Size()
state.timecursor = state.timestamps[state.selected_buffer_line]
state.offset = max(state.selected_buffer_line-height+2, 0)
seek_to_time(state.timecursor, &state.log)
case 1:
state.selected_index = max(0, state.selected_index-1)
}
case termbox.KeyArrowLeft:
if state.focused_pane == 1 {
state.realtime = false
// Figure out where the new timecursor should be
v := state.log[state.log_keys[state.selected_index]]
v.Focused = max(0, min(len(v.Values)-1, v.Focused-1))
state.timecursor = v.Timestamp[v.Focused]
// Update
_, height := termbox.Size()
state.selected_buffer_line = scroll_to_time(state.timecursor, state.selected_buffer_line, &state.timestamps)
state.offset = max(state.selected_buffer_line-height+1, 0)
seek_to_time(state.timecursor, &state.log)
}
case termbox.KeyArrowRight:
if state.focused_pane == 1 {
state.realtime = false
// Figure out where the new timecursor should be
v := state.log[state.log_keys[state.selected_index]]
v.Focused = max(0, min(len(v.Values)-1, v.Focused+1))
state.timecursor = v.Timestamp[v.Focused]
// Update
_, height := termbox.Size()
state.selected_buffer_line = scroll_to_time(state.timecursor, state.selected_buffer_line, &state.timestamps)
state.offset = max(state.selected_buffer_line-height+1, 0)
seek_to_time(state.timecursor, &state.log)
}
case termbox.KeySpace:
state.focused_pane = (state.focused_pane + 1) % 2
case termbox.KeyCtrlR:
state.realtime = true
for _, v := range state.log {
v.Focused = len(v.Values) - 1
}
state.selected_buffer_line = len(state.buffer) - 1
}
draw_all(&state)
case termbox.EventMouse:
case termbox.EventResize:
draw_all(&state)
}
}
}
}