Forráskód Böngészése

Implement custom keybindings (#52)

ayntgl 4 éve
szülő
commit
d262054baf
4 módosított fájl, 45 hozzáadás és 37 törlés
  1. 2 20
      README.md
  2. 29 1
      config.go
  3. 7 9
      main.go
  4. 7 7
      ui.go

+ 2 - 20
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
 

+ 29 - 1
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 {

+ 7 - 9
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

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