From f77443480ba0eadeaaa2695dda330dca76e76ac3 Mon Sep 17 00:00:00 2001 From: nakabonne Date: Sat, 12 Sep 2020 21:41:24 +0900 Subject: [PATCH] Ensure to attack with pushing key --- gui/gui.go | 20 +++++++++++++++++--- gui/widgets.go | 20 +++----------------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/gui/gui.go b/gui/gui.go index e54aaec..ec10bd1 100644 --- a/gui/gui.go +++ b/gui/gui.go @@ -3,7 +3,9 @@ package gui import ( "context" "fmt" + "time" + "github.com/k0kubun/pp" "github.com/mum4k/termdash" "github.com/mum4k/termdash/container" "github.com/mum4k/termdash/container/grid" @@ -11,6 +13,8 @@ import ( "github.com/mum4k/termdash/linestyle" "github.com/mum4k/termdash/terminal/termbox" "github.com/mum4k/termdash/terminal/terminalapi" + + "github.com/nakabonne/ali/attacker" ) const rootID = "root" @@ -42,12 +46,22 @@ func Run() error { return fmt.Errorf("failed to update container: %w", err) } - quitter := func(k *terminalapi.Keyboard) { - if k.Key == keyboard.KeyEsc || k.Key == keyboard.KeyCtrlC { + keybinds := func(k *terminalapi.Keyboard) { + switch k.Key { + case keyboard.KeyEsc, keyboard.KeyCtrlC: cancel() + case keyboard.KeyEnter: + resultCh := make(chan *attacker.Result) + go func() { + // TODO: Enalble to poplulate from input + metrics := attacker.Attack(ctx, "http://34.84.111.163:9898", resultCh, attacker.Options{Rate: 50, Duration: 10 * time.Second}) + pp.Println(metrics) + }() + go redrawChart(ctx, w.plotChart, resultCh) } } - return termdash.Run(ctx, t, c, termdash.KeyboardSubscriber(quitter), termdash.RedrawInterval(redrawInterval)) + + return termdash.Run(ctx, t, c, termdash.KeyboardSubscriber(keybinds), termdash.RedrawInterval(redrawInterval)) } func gridLayout(w *widgets) ([]container.Option, error) { diff --git a/gui/widgets.go b/gui/widgets.go index 534acbf..81ca8b0 100644 --- a/gui/widgets.go +++ b/gui/widgets.go @@ -26,7 +26,7 @@ type widgets struct { } func newWidgets(ctx context.Context, c *container.Container) (*widgets, error) { - l, err := newLineChart(ctx) + l, err := newLineChart() if err != nil { return nil, err } @@ -38,26 +38,12 @@ func newWidgets(ctx context.Context, c *container.Container) (*widgets, error) { }, nil } -// newLineChart returns a line plotChart that displays a heartbeat-like progression. -func newLineChart(ctx context.Context) (*linechart.LineChart, error) { - lc, err := linechart.New( +func newLineChart() (*linechart.LineChart, error) { + return linechart.New( linechart.AxesCellOpts(cell.FgColor(cell.ColorRed)), linechart.YLabelCellOpts(cell.FgColor(cell.ColorGreen)), linechart.XLabelCellOpts(cell.FgColor(cell.ColorGreen)), ) - if err != nil { - return nil, err - } - - resultCh := make(chan *attacker.Result) - go func() { - // TODO: Enalble to poplulate from input - metrics := attacker.Attack(ctx, "http://34.84.111.163:9898", resultCh, attacker.Options{Rate: 50, Duration: 10 * time.Second}) - pp.Println(metrics) - }() - go redrawChart(ctx, lc, resultCh) - - return lc, nil } func redrawChart(ctx context.Context, lineChart *linechart.LineChart, resultCh chan *attacker.Result) {