From d262054baff67484bda537e73dc846e4b3b738c2 Mon Sep 17 00:00:00 2001 From: ayntgl <90600421+ayntgl@users.noreply.github.com> Date: Mon, 20 Sep 2021 22:11:41 +0400 Subject: [PATCH] Implement custom keybindings (#52) --- README.md | 22 ++-------------------- config.go | 30 +++++++++++++++++++++++++++++- main.go | 16 +++++++--------- ui.go | 14 +++++++------- 4 files changed, 45 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index ed225c1..90f10ea 100644 --- a/README.md +++ b/README.md @@ -42,27 +42,9 @@ By default, Discordo utilizes OS-specific keyring to store credentials such as c - Log in using the email and password (first-time login) and click on the "Login" button to continue. -### Default keybindings +### Configuration -Global: - -- `Alt` + `1`: Sets the focus on the guilds TreeView. -- `Alt` + `2`: Sets the focus on the messages TextView. -- `Alt` + `3`: Sets the focus on the message InputField. - -Messages: - -- `k` or `Up`: Selects the message just before the currently selected message. -- `j` or `Down`: Selects the message just after the currently selected message. -- `g` or `Home`: Selects the first message rendered. -- `G` or `End`: Selects the last message rendered. - -- `r`: Reply to the selected message without mention. -- `R`: Reply to the currently selected message with mention. - -Input: - -- `Esc`: Unselect the currently selected message. This is useful for canceling an inline reply message. +Discordo aims to be highly configurable, it may be easily customized via a configuration file. It creates a default configuration file on the first start-up. The newly created configuration file is located at `$HOME/.config/discordo/config.json`. ### Clipboard support diff --git a/config.go b/config.go index d71e79e..f2cd188 100644 --- a/config.go +++ b/config.go @@ -7,6 +7,20 @@ import ( "github.com/rivo/tview" ) +type keybindings struct { + GuildsTreeViewFocus string + + MessagesTextViewFocus string + MessagesTextViewSelectPrevious string + MessagesTextViewSelectNext string + MessagesTextViewSelectFirst string + MessagesTextViewSelectLast string + MessagesTextViewReplySelected string + MessagesTextViewMentionReplySelected string + + MessageInputFieldFocus string +} + type config struct { Token string Mouse bool @@ -14,6 +28,7 @@ type config struct { UserAgent string GetMessagesLimit int Theme tview.Theme + Keybindings keybindings } func loadConfig() *config { @@ -23,7 +38,7 @@ func loadConfig() *config { } configPath := u + "/.config/discordo/config.json" - if _, err := os.Stat(configPath); os.IsNotExist(err) { + if _, err = os.Stat(configPath); os.IsNotExist(err) { f, err := os.Create(configPath) if err != nil { panic(err) @@ -38,6 +53,19 @@ func loadConfig() *config { "Chrome/92.0.4515.131 Safari/537.36", GetMessagesLimit: 50, Theme: tview.Styles, + Keybindings: keybindings{ + GuildsTreeViewFocus: "Alt+Rune[1]", + + MessagesTextViewFocus: "Alt+Rune[2]", + MessagesTextViewSelectPrevious: "Up", + MessagesTextViewSelectNext: "Down", + MessagesTextViewSelectFirst: "Home", + MessagesTextViewSelectLast: "End", + MessagesTextViewReplySelected: "r", + MessagesTextViewMentionReplySelected: "R", + + MessageInputFieldFocus: "Alt+Rune[3]", + }, } d, err := json.MarshalIndent(c, "", "\t") if err != nil { diff --git a/main.go b/main.go index 5f00420..b4d321e 100644 --- a/main.go +++ b/main.go @@ -72,15 +72,13 @@ func main() { } func onAppInputCapture(e *tcell.EventKey) *tcell.EventKey { - if e.Modifiers() == tcell.ModAlt { - switch e.Rune() { - case '1': - app.SetFocus(guildsTreeView) - case '2': - app.SetFocus(messagesTextView) - case '3': - app.SetFocus(messageInputField) - } + switch e.Name() { + case conf.Keybindings.GuildsTreeViewFocus: + app.SetFocus(guildsTreeView) + case conf.Keybindings.MessagesTextViewFocus: + app.SetFocus(messagesTextView) + case conf.Keybindings.MessageInputFieldFocus: + app.SetFocus(messageInputField) } return e diff --git a/ui.go b/ui.go index 8cb2732..bd7c0e5 100644 --- a/ui.go +++ b/ui.go @@ -200,8 +200,8 @@ func onMessagesTextViewInputCapture(e *tcell.EventKey) *tcell.EventKey { return nil } - switch { - case e.Key() == tcell.KeyUp || e.Rune() == 'k': // Up + switch e.Name() { + case conf.Keybindings.MessagesTextViewSelectPrevious: ms := selectedChannel.Messages if len(ms) == 0 { return nil @@ -224,7 +224,7 @@ func onMessagesTextViewInputCapture(e *tcell.EventKey) *tcell.EventKey { } return nil - case e.Key() == tcell.KeyDown || e.Rune() == 'j': // Down + case conf.Keybindings.MessagesTextViewSelectNext: ms := selectedChannel.Messages if len(ms) == 0 { return nil @@ -247,7 +247,7 @@ func onMessagesTextViewInputCapture(e *tcell.EventKey) *tcell.EventKey { } return nil - case e.Key() == tcell.KeyHome || e.Rune() == 'g': // Top + case conf.Keybindings.MessagesTextViewSelectFirst: ms := selectedChannel.Messages if len(ms) == 0 { return nil @@ -256,7 +256,7 @@ func onMessagesTextViewInputCapture(e *tcell.EventKey) *tcell.EventKey { messagesTextView. Highlight(ms[0].ID). ScrollToHighlight() - case e.Key() == tcell.KeyEnd || e.Rune() == 'G': // Bottom + case conf.Keybindings.MessagesTextViewSelectLast: ms := selectedChannel.Messages if len(ms) == 0 { return nil @@ -265,7 +265,7 @@ func onMessagesTextViewInputCapture(e *tcell.EventKey) *tcell.EventKey { messagesTextView. Highlight(ms[len(ms)-1].ID). ScrollToHighlight() - case e.Rune() == 'r': // Inline reply + case conf.Keybindings.MessagesTextViewReplySelected: ms := selectedChannel.Messages if len(ms) == 0 { return nil @@ -281,7 +281,7 @@ func onMessagesTextViewInputCapture(e *tcell.EventKey) *tcell.EventKey { "Replying to " + selectedMessage.Author.Username, ) app.SetFocus(messageInputField) - case e.Rune() == 'R': + case conf.Keybindings.MessagesTextViewMentionReplySelected: ms := selectedChannel.Messages if len(ms) == 0 { return nil