Skip to content

Commit

Permalink
Separate Input logic from UI. Graceful Interrupt handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
s-macke committed Jul 22, 2024
1 parent 396bca3 commit ced34d3
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 45 deletions.
54 changes: 54 additions & 0 deletions src/input.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion src/slapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 0 additions & 44 deletions src/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package slapperx
import (
"bytes"
"fmt"
term "github.com/nsf/termbox-go"
terminal "golang.org/x/term"
"log"
"math"
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit ced34d3

Please sign in to comment.