Skip to content

Commit

Permalink
Re-bind Q to quit on all but the nickname scene
Browse files Browse the repository at this point in the history
  • Loading branch information
armsnyder committed Oct 20, 2020
1 parent 4441741 commit 06187f9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
9 changes: 7 additions & 2 deletions pkg/client/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"reflect"
"time"
"unicode"

"github.com/gorilla/websocket"
"github.com/nsf/termbox-go"
Expand Down Expand Up @@ -73,7 +74,7 @@ func Run() (err error) {
case event := <-terminalEvents:
log.Printf("Received terminal event (type=%d)", event.Type)

if shouldInterrupt(event) {
if shouldInterrupt(event, currentScene) {
log.Println("Interrupting terminal")
termbox.Interrupt()

Expand Down Expand Up @@ -178,7 +179,11 @@ func receiveMessages(c *websocket.Conn, messageQueue chan<- common.AnyMessage, m
}
}

func shouldInterrupt(event termbox.Event) bool {
func shouldInterrupt(event termbox.Event, scene scenes.Scene) bool {
if unicode.ToLower(event.Ch) == 'q' && !scene.HasFreeKeyboardInput() {
return true
}

return event.Key == termbox.KeyCtrlC || event.Key == termbox.KeyEsc
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/client/scenes/nickname.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ func (n *Nickname) Draw() {
setCursor(offset(center, cursorX, 4))
}

func (n *Nickname) HasFreeKeyboardInput() bool {
return true
}

func (n *Nickname) load() error {
configPath, err := n.configPath()
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions pkg/client/scenes/scene.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Scene interface {
OnTerminalEvent(event termbox.Event) error
Tick() bool
Draw()
HasFreeKeyboardInput() bool
}

// types for Scene setup method.
Expand Down Expand Up @@ -45,3 +46,7 @@ func (s *scene) Tick() bool {
// Default implementation is a no-op.
return false
}

func (s *scene) HasFreeKeyboardInput() bool {
return false
}

0 comments on commit 06187f9

Please sign in to comment.