Implement custom keybindings (#52)

This commit is contained in:
ayntgl 2021-09-20 22:11:41 +04:00 • committed by GitHub
parent 5c64b0fb8b
commit d262054baf
4 changed files with 45 additions and 37 deletions

View file

@ -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

View file

@ -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 {

16
main.go
View file

@ -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

14
ui.go
View file

@ -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