/
keybinds.go
61 lines (54 loc) · 1.42 KB
/
keybinds.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
package gui
import (
"context"
"github.com/mum4k/termdash/container"
"github.com/mum4k/termdash/keyboard"
"github.com/mum4k/termdash/terminal/terminalapi"
"github.com/nakabonne/ali/attacker"
)
func navigateCharts(chartFuncs []func()) func(bool) {
position := 0
numFuncs := len(chartFuncs)
return func(backwards bool) {
position++
if backwards {
position -= 2
if position < 0 {
position = numFuncs - 1
}
}
chartFuncs[position%numFuncs]()
}
}
func keybinds(ctx context.Context, cancel context.CancelFunc, c *container.Container, dr *drawer, a attacker.Attacker) func(*terminalapi.Keyboard) {
funcs := []func(){
func() { c.Update(chartID, dr.gridOpts.latency...) },
func() { c.Update(chartID, dr.gridOpts.percentiles...) },
}
navigateFunc := navigateCharts(funcs)
return func(k *terminalapi.Keyboard) {
switch k.Key {
case keyboard.KeyCtrlC, 'q': // Quit
cancel()
case keyboard.KeyEnter: // Attack
attack(ctx, dr, a)
case 'H', 'h': // backwards
navigateFunc(true)
case 'L', 'l': // forwards
navigateFunc(false)
}
}
}
func attack(ctx context.Context, d *drawer, a attacker.Attacker) {
if d.chartDrawing.Load() {
return
}
child, cancel := context.WithCancel(ctx)
// To initialize, run redrawChart on a per-attack basis.
go d.redrawCharts(child)
go d.redrawGauge(child, a.Duration())
go func() {
a.Attack(child, d.metricsCh) // this blocks until attack finishes
cancel()
}()
}