feat: implement multiple keybindings for a command (#101)

This commit is contained in:
ayntgl 2021-11-17 12:28:34 +04:00 • committed by GitHub
parent fc25402615
commit e8a2f33e07
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 35 deletions

26
ui.go
View file

@ -17,14 +17,13 @@ var (
)
func onAppInputCapture(e *tcell.EventKey) *tcell.EventKey {
switch e.Name() {
case conf.Keybindings.FocusChannelsTree:
if util.HasKeybinding(conf.Keybindings.FocusChannelsTree, e.Name()) {
app.SetFocus(channelsTree)
return nil
case conf.Keybindings.FocusMessagesView:
} else if util.HasKeybinding(conf.Keybindings.FocusMessagesView, e.Name()) {
app.SetFocus(messagesView)
return nil
case conf.Keybindings.FocusMessageInputField:
} else if util.HasKeybinding(conf.Keybindings.FocusMessageInputField, e.Name()) {
app.SetFocus(messageInputField)
return nil
}
@ -139,8 +138,7 @@ func onMessagesViewInputCapture(e *tcell.EventKey) *tcell.EventKey {
return nil
}
switch e.Name() {
case conf.Keybindings.SelectPreviousMessage:
if util.HasKeybinding(conf.Keybindings.SelectPreviousMessage, e.Name()) {
if len(messagesView.GetHighlights()) == 0 {
selectedMessage = len(ms) - 1
} else {
@ -154,7 +152,7 @@ func onMessagesViewInputCapture(e *tcell.EventKey) *tcell.EventKey {
Highlight(ms[selectedMessage].ID).
ScrollToHighlight()
return nil
case conf.Keybindings.SelectNextMessage:
} else if util.HasKeybinding(conf.Keybindings.SelectNextMessage, e.Name()) {
if len(messagesView.GetHighlights()) == 0 {
selectedMessage = len(ms) - 1
} else {
@ -168,19 +166,19 @@ func onMessagesViewInputCapture(e *tcell.EventKey) *tcell.EventKey {
Highlight(ms[selectedMessage].ID).
ScrollToHighlight()
return nil
case conf.Keybindings.SelectFirstMessage:
} else if util.HasKeybinding(conf.Keybindings.SelectFirstMessage, e.Name()) {
selectedMessage = 0
messagesView.
Highlight(ms[selectedMessage].ID).
ScrollToHighlight()
return nil
case conf.Keybindings.SelectLastMessage:
} else if util.HasKeybinding(conf.Keybindings.SelectLastMessage, e.Name()) {
selectedMessage = len(ms) - 1
messagesView.
Highlight(ms[selectedMessage].ID).
ScrollToHighlight()
return nil
case conf.Keybindings.SelectMessageReference:
} else if util.HasKeybinding(conf.Keybindings.SelectMessageReference, e.Name()) {
hs := messagesView.GetHighlights()
if len(hs) == 0 {
return nil
@ -195,7 +193,7 @@ func onMessagesViewInputCapture(e *tcell.EventKey) *tcell.EventKey {
}
return nil
case conf.Keybindings.ReplySelectedMessage:
} else if util.HasKeybinding(conf.Keybindings.ReplySelectedMessage, e.Name()) {
hs := messagesView.GetHighlights()
if len(hs) == 0 {
return nil
@ -205,7 +203,7 @@ func onMessagesViewInputCapture(e *tcell.EventKey) *tcell.EventKey {
messageInputField.SetTitle("Replying to " + m.Author.String())
app.SetFocus(messageInputField)
return nil
case conf.Keybindings.MentionReplySelectedMessage:
} else if util.HasKeybinding(conf.Keybindings.ReplySelectedMessage, e.Name()) {
hs := messagesView.GetHighlights()
if len(hs) == 0 {
return nil
@ -215,7 +213,7 @@ func onMessagesViewInputCapture(e *tcell.EventKey) *tcell.EventKey {
messageInputField.SetTitle("[@] Replying to " + m.Author.String())
app.SetFocus(messageInputField)
return nil
case conf.Keybindings.CopySelectedMessage:
} else if util.HasKeybinding(conf.Keybindings.CopySelectedMessage, e.Name()) {
hs := messagesView.GetHighlights()
if len(hs) == 0 {
return nil
@ -226,6 +224,8 @@ func onMessagesViewInputCapture(e *tcell.EventKey) *tcell.EventKey {
if err != nil {
return nil
}
return nil
}
return e

View file

@ -13,18 +13,18 @@ const userAgent = "" +
"Chrome/92.0.4515.131 Safari/537.36"
type keybindings struct {
FocusChannelsTree string `toml:"focus_channels_tree"`
FocusMessagesView string `toml:"focus_messages_view"`
FocusMessageInputField string `toml:"focus_message_input_field"`
FocusChannelsTree []string `toml:"focus_channels_tree"`
FocusMessagesView []string `toml:"focus_messages_view"`
FocusMessageInputField []string `toml:"focus_message_input_field"`
SelectPreviousMessage string `toml:"select_previous_message"`
SelectNextMessage string `toml:"select_next_message"`
SelectFirstMessage string `toml:"select_first_message"`
SelectLastMessage string `toml:"select_last_message"`
SelectMessageReference string `toml:"select_message_reference"`
ReplySelectedMessage string `toml:"reply_selected_message"`
MentionReplySelectedMessage string `toml:"mention_reply_selected_message"`
CopySelectedMessage string `toml:"copy_selected_message"`
SelectPreviousMessage []string `toml:"select_previous_message"`
SelectNextMessage []string `toml:"select_next_message"`
SelectFirstMessage []string `toml:"select_first_message"`
SelectLastMessage []string `toml:"select_last_message"`
SelectMessageReference []string `toml:"select_message_reference"`
ReplySelectedMessage []string `toml:"reply_selected_message"`
MentionReplySelectedMessage []string `toml:"mention_reply_selected_message"`
CopySelectedMessage []string `toml:"copy_selected_message"`
}
type theme struct {
@ -101,18 +101,18 @@ func NewConfig() *Config {
Text: "white",
},
Keybindings: keybindings{
FocusChannelsTree: "Alt+Left",
FocusMessagesView: "Alt+Right",
FocusMessageInputField: "Alt+Down",
FocusChannelsTree: []string{"Alt+Left"},
FocusMessagesView: []string{"Alt+Right"},
FocusMessageInputField: []string{"Alt+Down"},
SelectPreviousMessage: "Up",
SelectNextMessage: "Down",
SelectFirstMessage: "Home",
SelectLastMessage: "End",
ReplySelectedMessage: "Rune[r]",
MentionReplySelectedMessage: "Rune[R]",
CopySelectedMessage: "Rune[c]",
SelectMessageReference: "Rune[m]",
SelectPreviousMessage: []string{"Up"},
SelectNextMessage: []string{"Down"},
SelectFirstMessage: []string{"Home"},
SelectLastMessage: []string{"End"},
ReplySelectedMessage: []string{"Rune[r]"},
MentionReplySelectedMessage: []string{"Rune[R]"},
CopySelectedMessage: []string{"Rune[c]"},
SelectMessageReference: []string{"Rune[m]"},
},
}
err = toml.NewEncoder(f).Encode(c)

View file

@ -101,3 +101,13 @@ func HasPermission(s *discordgo.State, cID string, p int64) bool {
return perm&p == p
}
func HasKeybinding(sl []string, s string) bool {
for _, str := range sl {
if str == s {
return true
}
}
return false
}