Browse Source

cmd: add quit keybinding (#437)

Co-authored-by: ayn2op <ayn2op@gmail.com>
Retropaint 1 year ago
parent
commit
501cdf3aa4
2 changed files with 16 additions and 0 deletions
  1. 14 0
      cmd/application.go
  2. 2 0
      internal/config/keys.go

+ 14 - 0
cmd/application.go

@@ -3,6 +3,7 @@ package cmd
 import (
 	"log/slog"
 
+	"github.com/gdamore/tcell/v2"
 	"github.com/rivo/tview"
 )
 
@@ -16,9 +17,22 @@ func newApplication() *Application {
 	}
 
 	app.EnableMouse(cfg.Mouse)
+	app.SetInputCapture(app.onInputCapture)
 	return app
 }
 
+func (app *Application) onInputCapture(event *tcell.EventKey) *tcell.EventKey {
+	switch event.Name() {
+	case cfg.Keys.Quit:
+		app.Stop()
+	case "Ctrl+C":
+		// https://github.com/rivo/tview/blob/a64fc48d7654432f71922c8b908280cdb525805c/application.go#L153
+		return tcell.NewEventKey(tcell.KeyCtrlC, 0, tcell.ModNone)
+	}
+
+	return event
+}
+
 func (app *Application) Show(token string) error {
 	if token == "" {
 		loginForm := NewLoginForm(func(token string, err error) {

+ 2 - 0
internal/config/keys.go

@@ -17,6 +17,7 @@ type (
 		MessageInput MessageInputKeys `toml:"message_input"`
 
 		Logout string `toml:"logout"`
+		Quit   string `toml:"quit"`
 	}
 
 	GuildsTreeKeys struct {
@@ -49,6 +50,7 @@ func defaultKeys() Keys {
 		ToggleGuildsTree:  "Ctrl+B",
 
 		Logout: "Ctrl+D",
+		Quit:   "Ctrl+C",
 
 		SelectPrevious: "Rune[k]",
 		SelectNext:     "Rune[j]",