diff --git a/src/input.go b/src/input.go new file mode 100644 index 0000000..0b83048 --- /dev/null +++ b/src/input.go @@ -0,0 +1,54 @@ +package slapperx + +import ( + term "github.com/nsf/termbox-go" + "log" +) + +// keyPressListener listens for key presses and sends rate changes to rateChanger channel +func keyPressListener(rateChanger chan<- float64) { + // start keyPress listener + err := term.Init() + term.HideCursor() + if err != nil { + log.Fatal(err) + } + + defer term.Close() + + for { + ev := term.PollEvent() + switch ev.Type { + case term.EventKey: + if handleKeyPress(ev, rateChanger) { + return + } + case term.EventInterrupt: + return + case term.EventResize: + break + case term.EventError: + log.Fatal(ev.Err) + } + } +} + +// handleKeyPress processes key press events and updates the rateChanger channel +func handleKeyPress(ev term.Event, rateChanger chan<- float64) bool { + switch ev.Key { + case term.KeyCtrlC: + return true + default: + switch ev.Ch { + case 'q': + return true + case 'r': + stats.reset() + case 'k': // up + rateChanger <- rateIncreaseStep + case 'j': + rateChanger <- rateDecreaseStep + } + } + return false +} diff --git a/src/slapper.go b/src/slapper.go index ad65fb7..42aa789 100644 --- a/src/slapper.go +++ b/src/slapper.go @@ -68,7 +68,7 @@ func Main() { if config.Verbose { <-make(chan bool) // just wait for Ctrl-C } else { - ui.keyPressListener(rampUpController.GetRateChanger()) + keyPressListener(rampUpController.GetRateChanger()) } // bye diff --git a/src/ui.go b/src/ui.go index 9189600..601cd44 100644 --- a/src/ui.go +++ b/src/ui.go @@ -3,7 +3,6 @@ package slapperx import ( "bytes" "fmt" - term "github.com/nsf/termbox-go" terminal "golang.org/x/term" "log" "math" @@ -88,49 +87,6 @@ func (ui *UI) listParameters() { */ } -// keyPressListener listens for key presses and sends rate changes to rateChanger channel -func (ui *UI) keyPressListener(rateChanger chan<- float64) { - // start keyPress listener - err := term.Init() - term.HideCursor() - if err != nil { - log.Fatal(err) - } - - defer term.Close() - - for { - switch ev := term.PollEvent(); ev.Type { - case term.EventKey: - if ui.handleKeyPress(ev, rateChanger) { - return - } - case term.EventError: - log.Fatal(ev.Err) - } - } -} - -// handleKeyPress processes key press events and updates the rateChanger channel -func (ui *UI) handleKeyPress(ev term.Event, rateChanger chan<- float64) bool { - switch ev.Key { - case term.KeyCtrlC: - return true - default: - switch ev.Ch { - case 'q': - return true - case 'r': - stats.reset() - case 'k': // up - rateChanger <- rateIncreaseStep - case 'j': - rateChanger <- rateDecreaseStep - } - } - return false -} - // printHistogramHeader prints the header of the histogram with sent, in-flight, and responses information func (ui *UI) printHistogramHeader(sb *strings.Builder, currentRate counter, currentSetRate float64) { sent := stats.requestsSent.Load()