forked from ikruglov/slapper
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate Input logic from UI. Graceful Interrupt handling.
- Loading branch information
Showing
3 changed files
with
55 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters