Kaynağa Gözat

Add customizable keybindings

ayn2op 3 yıl önce
ebeveyn
işleme
726e7a4f01
3 değiştirilmiş dosya ile 94 ekleme ve 23 silme
  1. 55 23
      config.go
  2. 1 0
      message_input.go
  3. 38 0
      messages_text.go

+ 55 - 23
config.go

@@ -9,35 +9,54 @@ import (
 
 const name = "discordo"
 
-type CommonThemeConfig struct {
-	Border        bool   `yaml:"border"`
-	BorderPadding [4]int `yaml:"border_padding,flow"`
-}
+type (
+	CommonKeysConfig struct {
+		Cancel string `yaml:"cancel"`
+	}
 
-type GuildsTreeThemeConfig struct {
-	CommonThemeConfig `yaml:",inline"`
-	Graphics          bool `yaml:"graphics"`
-}
+	MessagesTextKeysConfig struct {
+		CommonKeysConfig `yaml:",inline"`
+		Reply            string `yaml:"reply"`
+		ReplyMention     string `yaml:"reply_mention"`
+	}
 
-type MessagesTextThemeConfig struct {
-	CommonThemeConfig `yaml:",inline"`
-}
+	KeysConfig struct {
+		MessagesText MessagesTextKeysConfig `yaml:"messages_text"`
+	}
+)
 
-type MessageInputThemeConfig struct {
-	CommonThemeConfig `yaml:",inline"`
-}
+type (
+	CommonThemeConfig struct {
+		Border        bool   `yaml:"border"`
+		BorderPadding [4]int `yaml:"border_padding,flow"`
+	}
 
-type ThemeConfig struct {
-	GuildsTree   GuildsTreeThemeConfig   `yaml:"guilds_tree"`
-	MessagesText MessagesTextThemeConfig `yaml:"messages_text"`
-	MessageInput MessageInputThemeConfig `yaml:"message_input"`
-}
+	GuildsTreeThemeConfig struct {
+		CommonThemeConfig `yaml:",inline"`
+		Graphics          bool `yaml:"graphics"`
+	}
+
+	MessagesTextThemeConfig struct {
+		CommonThemeConfig `yaml:",inline"`
+	}
+
+	MessageInputThemeConfig struct {
+		CommonThemeConfig `yaml:",inline"`
+	}
+
+	ThemeConfig struct {
+		GuildsTree   GuildsTreeThemeConfig   `yaml:"guilds_tree"`
+		MessagesText MessagesTextThemeConfig `yaml:"messages_text"`
+		MessageInput MessageInputThemeConfig `yaml:"message_input"`
+	}
+)
 
 type Config struct {
 	Mouse         bool `yaml:"mouse"`
 	MessagesLimit uint `yaml:"messages_limit"`
 	Timestamps    bool `yaml:"timestamps"`
 
+	Keys  KeysConfig  `yaml:"keys"`
 	Theme ThemeConfig `yaml:"theme"`
 }
 
@@ -52,25 +71,38 @@ func newConfig() (*Config, error) {
 		return nil, err
 	}
 
-	common := CommonThemeConfig{
+	commonTheme := CommonThemeConfig{
 		Border:        true,
 		BorderPadding: [...]int{0, 0, 1, 1},
 	}
 
+	commonKeys := CommonKeysConfig{
+		Cancel: "Esc",
+	}
+
 	c := Config{
 		Mouse:         true,
+		Timestamps:    false,
 		MessagesLimit: 50,
 
+		Keys: KeysConfig{
+			MessagesText: MessagesTextKeysConfig{
+				CommonKeysConfig: commonKeys,
+				Reply:            "Rune[r]",
+				ReplyMention:     "Rune[R]",
+			},
+		},
+
 		Theme: ThemeConfig{
 			GuildsTree: GuildsTreeThemeConfig{
-				CommonThemeConfig: common,
+				CommonThemeConfig: commonTheme,
 				Graphics:          true,
 			},
 			MessagesText: MessagesTextThemeConfig{
-				CommonThemeConfig: common,
+				CommonThemeConfig: commonTheme,
 			},
 			MessageInput: MessageInputThemeConfig{
-				CommonThemeConfig: common,
+				CommonThemeConfig: commonTheme,
 			},
 		},
 	}

+ 1 - 0
message_input.go

@@ -16,6 +16,7 @@ func newMessageInput() *MessageInput {
 
 	mi.SetDoneFunc(mi.onDone)
 
+	mi.SetTitleAlign(tview.AlignLeft)
 	mi.SetFieldBackgroundColor(tview.Styles.PrimitiveBackgroundColor)
 	mi.SetBorder(cfg.Theme.MessageInput.Border)
 	padding := cfg.Theme.MessageInput.BorderPadding

+ 38 - 0
messages_text.go

@@ -6,6 +6,7 @@ import (
 	"time"
 
 	"github.com/diamondburned/arikawa/v3/discord"
+	"github.com/gdamore/tcell/v2"
 	"github.com/rivo/tview"
 )
 
@@ -22,10 +23,13 @@ func newMessagesText() *MessagesText {
 
 	mt.SetDynamicColors(true)
 	mt.SetRegions(true)
+	mt.SetToggleHighlights(true)
 	mt.SetWordWrap(true)
 	mt.ScrollToEnd()
 	mt.SetHighlightedFunc(mt.onHighlighted)
+	mt.SetInputCapture(mt.onInputCapture)
 
+	mt.SetTitleAlign(tview.AlignLeft)
 	mt.SetBorder(cfg.Theme.MessagesText.Border)
 
 	padding := cfg.Theme.MessagesText.BorderPadding
@@ -104,3 +108,37 @@ func (mt *MessagesText) onHighlighted(added, removed, remaining []string) {
 
 	mt.selectedMessage = m
 }
+
+func (mt *MessagesText) onInputCapture(event *tcell.EventKey) *tcell.EventKey {
+	switch event.Name() {
+	case cfg.Keys.MessagesText.Reply:
+		mt.replyAction(false)
+		return nil
+	case cfg.Keys.MessagesText.ReplyMention:
+		mt.replyAction(true)
+		return nil
+	case cfg.Keys.MessagesText.Cancel:
+		// TODO
+		return nil
+	}
+
+	return event
+}
+
+func (mt *MessagesText) replyAction(mention bool) {
+	if mt.selectedMessage == nil {
+		return
+	}
+
+	var title string
+	if mention {
+		title += "[@] Replying to "
+	} else {
+		title += "Replying to "
+	}
+
+	title += mt.selectedMessage.Author.Tag()
+	messageInput.SetTitle(title)
+
+	app.SetFocus(messageInput)
+}