From 25957fd3fd9d381b22d3436b448eae407d33e550 Mon Sep 17 00:00:00 2001 From: ayn2op Date: Fri, 16 Oct 2026 18:52:15 -0400 Subject: [PATCH] refactor(ui): alias the keybinds config in key switches --- internal/ui/chat/composer/model.go | 39 ++++++++++++------------ internal/ui/chat/guildstree/model.go | 13 ++++---- internal/ui/chat/messageslist/model.go | 41 +++++++++++++------------- internal/ui/chat/model.go | 23 ++++++++------- internal/ui/root/model.go | 10 ++++--- 5 files changed, 66 insertions(+), 60 deletions(-) diff --git a/internal/ui/chat/composer/model.go b/internal/ui/chat/composer/model.go index c4ba002..1ab8396 100644 --- a/internal/ui/chat/composer/model.go +++ b/internal/ui/chat/composer/model.go @@ -186,20 +186,20 @@ func (m Model) footer() string { // isBound reports whether key is one of the composer's keybinds. func (m Model) isBound(key tview.KeyMsg) bool { - k := m.cfg.Keybinds.Composer + kbs := m.cfg.Keybinds.Composer if keybind.Matches(key, - k.Paste.Keybind, - k.Send.Keybind, - k.OpenEditor.Keybind, - k.OpenFilePicker.Keybind, - k.Cancel.Keybind, - k.TabComplete.Keybind, - k.ToggleReplyMention.Keybind, - k.Undo.Keybind, + kbs.Paste.Keybind, + kbs.Send.Keybind, + kbs.OpenEditor.Keybind, + kbs.OpenFilePicker.Keybind, + kbs.Cancel.Keybind, + kbs.TabComplete.Keybind, + kbs.ToggleReplyMention.Keybind, + kbs.Undo.Keybind, ) { return true } - return keybind.Matches(key, k.EditLast.Keybind) && m.canEditLastMessage() + return keybind.Matches(key, kbs.EditLast.Keybind) && m.canEditLastMessage() } // setText replaces the text and moves the cursor, remembering what they were for undo if the text changed. @@ -280,40 +280,41 @@ func (m Model) Update(msg tview.Msg) (Model, tview.Cmd) { return m, typingCmd case tview.KeyMsg: + kbs := m.cfg.Keybinds.Composer switch { - case keybind.Matches(msg, m.cfg.Keybinds.Composer.EditLast.Keybind) && m.canEditLastMessage(): + case keybind.Matches(msg, kbs.EditLast.Keybind) && m.canEditLastMessage(): return m, m.editLastMessage() - case keybind.Matches(msg, m.cfg.Keybinds.Composer.Paste.Keybind): + case keybind.Matches(msg, kbs.Paste.Keybind): m.pasteText() return m, pasteImage() - case keybind.Matches(msg, m.cfg.Keybinds.Composer.Send.Keybind): + case keybind.Matches(msg, kbs.Send.Keybind): if m.mentionsListVisible() { return m, m.tabComplete() } return m, m.send() - case keybind.Matches(msg, m.cfg.Keybinds.Composer.OpenEditor.Keybind): + case keybind.Matches(msg, kbs.OpenEditor.Keybind): m.stopTabCompletion() return m, m.openEditor() - case keybind.Matches(msg, m.cfg.Keybinds.Composer.OpenFilePicker.Keybind): + case keybind.Matches(msg, kbs.OpenFilePicker.Keybind): m.stopTabCompletion() return m, m.pickFiles() - case keybind.Matches(msg, m.cfg.Keybinds.Composer.Cancel.Keybind): + case keybind.Matches(msg, kbs.Cancel.Keybind): if m.mentionsListVisible() { m.stopTabCompletion() return m, nil } m.reset() return m, nil - case keybind.Matches(msg, m.cfg.Keybinds.Composer.TabComplete.Keybind): + case keybind.Matches(msg, kbs.TabComplete.Keybind): if m.mentionsListVisible() { return m, m.tabComplete() } m.insert("\t") return m, nil - case keybind.Matches(msg, m.cfg.Keybinds.Composer.ToggleReplyMention.Keybind): + case keybind.Matches(msg, kbs.ToggleReplyMention.Keybind): m.toggleReplyMention() return m, nil - case keybind.Matches(msg, m.cfg.Keybinds.Composer.Undo.Keybind): + case keybind.Matches(msg, kbs.Undo.Keybind): m.undo() return m, nil } diff --git a/internal/ui/chat/guildstree/model.go b/internal/ui/chat/guildstree/model.go index 14ce9f3..bca8c9c 100644 --- a/internal/ui/chat/guildstree/model.go +++ b/internal/ui/chat/guildstree/model.go @@ -549,17 +549,18 @@ func (m Model) Update(msg tview.Msg) (Model, tview.Cmd) { m.setCurrentNode(msg.Node) return m, m.selectNode(msg.Node) case tview.KeyMsg: + kbs := m.cfg.Keybinds.GuildsTree switch { - case keybind.Matches(msg, m.cfg.Keybinds.GuildsTree.CollapseAll.Keybind): + case keybind.Matches(msg, kbs.CollapseAll.Keybind): for _, node := range m.root.Children() { node.CollapseAll() } return m, nil - case keybind.Matches(msg, m.cfg.Keybinds.GuildsTree.CollapseParentNode.Keybind): + case keybind.Matches(msg, kbs.CollapseParentNode.Keybind): m.collapseParentNode(m.current) return m, nil - case keybind.Matches(msg, m.cfg.Keybinds.GuildsTree.SelectPreviousUnread.Keybind, m.cfg.Keybinds.GuildsTree.SelectNextUnread.Keybind): - previous := keybind.Matches(msg, m.cfg.Keybinds.GuildsTree.SelectPreviousUnread.Keybind) + case keybind.Matches(msg, kbs.SelectPreviousUnread.Keybind, kbs.SelectNextUnread.Keybind): + previous := keybind.Matches(msg, kbs.SelectPreviousUnread.Keybind) if node := adjacentNode(m.root, m.current, m.isUnread, previous); node != nil { // A guild or the direct messages without channels yet: load them and move on to the unread one. if _, ok := node.Reference().(discord.ChannelID); !ok { @@ -572,9 +573,9 @@ func (m Model) Update(msg tview.Msg) (Model, tview.Cmd) { m.setCurrentNode(node) } return m, nil - case keybind.Matches(msg, m.cfg.Keybinds.GuildsTree.YankID.Keybind): + case keybind.Matches(msg, kbs.YankID.Keybind): return m, uitree.YankID(m.current) - case keybind.Matches(msg, m.cfg.Keybinds.GuildsTree.ToggleVoice.Keybind): + case keybind.Matches(msg, kbs.ToggleVoice.Keybind): return m, m.toggleVoice(m.current) case keybind.Matches(msg, m.cfg.Keybinds.ToggleMute.Keybind): return m, m.toggleMute() diff --git a/internal/ui/chat/messageslist/model.go b/internal/ui/chat/messageslist/model.go index dcf5f3f..9bb905a 100644 --- a/internal/ui/chat/messageslist/model.go +++ b/internal/ui/chat/messageslist/model.go @@ -1045,48 +1045,49 @@ func (m Model) selectedMessage() (*discord.Message, bool) { func (m Model) Update(msg tview.Msg) (Model, tview.Cmd) { switch msg := msg.(type) { case tview.KeyMsg: + kbs := m.cfg.Keybinds.MessagesList switch { - case keybind.Matches(msg, m.cfg.Keybinds.MessagesList.Cancel.Keybind): + case keybind.Matches(msg, kbs.Cancel.Keybind): m.clearSelection() - case keybind.Matches(msg, m.cfg.Keybinds.MessagesList.SelectUp.Keybind): + case keybind.Matches(msg, kbs.SelectUp.Keybind): return m, m.selectUp() - case keybind.Matches(msg, m.cfg.Keybinds.MessagesList.SelectDown.Keybind): + case keybind.Matches(msg, kbs.SelectDown.Keybind): m.selectDown() - case keybind.Matches(msg, m.cfg.Keybinds.MessagesList.SelectTop.Keybind): + case keybind.Matches(msg, kbs.SelectTop.Keybind): return m, m.selectTop() - case keybind.Matches(msg, m.cfg.Keybinds.MessagesList.SelectBottom.Keybind): + case keybind.Matches(msg, kbs.SelectBottom.Keybind): m.selectBottom() - case keybind.Matches(msg, m.cfg.Keybinds.MessagesList.SelectReply.Keybind): + case keybind.Matches(msg, kbs.SelectReply.Keybind): m.selectReply() - case keybind.Matches(msg, m.cfg.Keybinds.MessagesList.YankID.Keybind): + case keybind.Matches(msg, kbs.YankID.Keybind): return m, m.yankMessageID() - case keybind.Matches(msg, m.cfg.Keybinds.MessagesList.YankContent.Keybind): + case keybind.Matches(msg, kbs.YankContent.Keybind): return m, m.yankContent() - case keybind.Matches(msg, m.cfg.Keybinds.MessagesList.YankURL.Keybind): + case keybind.Matches(msg, kbs.YankURL.Keybind): return m, m.yankURL() - case keybind.Matches(msg, m.cfg.Keybinds.MessagesList.Open.Keybind): + case keybind.Matches(msg, kbs.Open.Keybind): return m, m.open() - case keybind.Matches(msg, m.cfg.Keybinds.MessagesList.OpenInBrowser.Keybind): + case keybind.Matches(msg, kbs.OpenInBrowser.Keybind): return m, m.openInBrowser() - case keybind.Matches(msg, m.cfg.Keybinds.MessagesList.Play.Keybind): + case keybind.Matches(msg, kbs.Play.Keybind): if message, ok := m.selectedMessage(); ok && isVoiceMessage(*message) { return m, m.play(message.Attachments[0]) } - case keybind.Matches(msg, m.cfg.Keybinds.MessagesList.SeekBackward.Keybind): + case keybind.Matches(msg, kbs.SeekBackward.Keybind): return m, m.seek(m.voiceStatus.Position - seekStep) - case keybind.Matches(msg, m.cfg.Keybinds.MessagesList.SeekForward.Keybind): + case keybind.Matches(msg, kbs.SeekForward.Keybind): return m, m.seek(m.voiceStatus.Position + seekStep) - case keybind.Matches(msg, m.cfg.Keybinds.MessagesList.Download.Keybind): + case keybind.Matches(msg, kbs.Download.Keybind): return m, m.download() - case keybind.Matches(msg, m.cfg.Keybinds.MessagesList.Reply.Keybind): + case keybind.Matches(msg, kbs.Reply.Keybind): return m, m.reply(false) - case keybind.Matches(msg, m.cfg.Keybinds.MessagesList.ReplyMention.Keybind): + case keybind.Matches(msg, kbs.ReplyMention.Keybind): return m, m.reply(true) - case keybind.Matches(msg, m.cfg.Keybinds.MessagesList.Edit.Keybind): + case keybind.Matches(msg, kbs.Edit.Keybind): return m, m.editSelectedMessage() - case keybind.Matches(msg, m.cfg.Keybinds.MessagesList.Delete.Keybind): + case keybind.Matches(msg, kbs.Delete.Keybind): return m, m.deleteSelectedMessage() - case keybind.Matches(msg, m.cfg.Keybinds.MessagesList.DeleteConfirm.Keybind): + case keybind.Matches(msg, kbs.DeleteConfirm.Keybind): return m, m.confirmDelete() } case olderMessagesLoadedMsg: diff --git a/internal/ui/chat/model.go b/internal/ui/chat/model.go index df29ef1..7bd03ec 100644 --- a/internal/ui/chat/model.go +++ b/internal/ui/chat/model.go @@ -262,41 +262,42 @@ func (m Model) Update(msg tview.Msg) (Model, tview.Cmd) { case QuitMsg: return m, closeState(m.state) case tview.KeyMsg: + kbs := m.cfg.Keybinds switch { - case keybind.Matches(msg, m.cfg.Keybinds.FocusGuildsTree.Keybind): + case keybind.Matches(msg, kbs.FocusGuildsTree.Keybind): m.setFocus(paneGuildsTree) return m, nil - case keybind.Matches(msg, m.cfg.Keybinds.FocusMembersTree.Keybind): + case keybind.Matches(msg, kbs.FocusMembersTree.Keybind): m.setFocus(paneMembersTree) return m, nil - case keybind.Matches(msg, m.cfg.Keybinds.FocusMessagesList.Keybind): + case keybind.Matches(msg, kbs.FocusMessagesList.Keybind): m.setFocus(paneMessagesList) return m, nil - case keybind.Matches(msg, m.cfg.Keybinds.FocusComposer.Keybind): + case keybind.Matches(msg, kbs.FocusComposer.Keybind): m.setFocus(paneComposer) return m, nil - case keybind.Matches(msg, m.cfg.Keybinds.FocusPrevious.Keybind): + case keybind.Matches(msg, kbs.FocusPrevious.Keybind): m.cycleFocus(-1) return m, nil - case keybind.Matches(msg, m.cfg.Keybinds.FocusNext.Keybind): + case keybind.Matches(msg, kbs.FocusNext.Keybind): m.cycleFocus(1) return m, nil - case keybind.Matches(msg, m.cfg.Keybinds.ToggleGuildsTree.Keybind): + case keybind.Matches(msg, kbs.ToggleGuildsTree.Keybind): m.toggle(&m.guildsTreeVisible, paneGuildsTree) return m, nil - case keybind.Matches(msg, m.cfg.Keybinds.ToggleMembersTree.Keybind): + case keybind.Matches(msg, kbs.ToggleMembersTree.Keybind): m.toggle(&m.membersTreeVisible, paneMembersTree) return m, nil - case keybind.Matches(msg, m.cfg.Keybinds.ToggleChannelsPicker.Keybind): + case keybind.Matches(msg, kbs.ToggleChannelsPicker.Keybind): m.togglePicker() return m, nil // The guilds tree is what the voice channel joined is in. - case keybind.Matches(msg, m.cfg.Keybinds.ToggleMute.Keybind, m.cfg.Keybinds.ToggleDeafen.Keybind, m.cfg.Keybinds.LeaveVoice.Keybind): + case keybind.Matches(msg, kbs.ToggleMute.Keybind, kbs.ToggleDeafen.Keybind, kbs.LeaveVoice.Keybind): return m, m.updatePane(paneGuildsTree, msg) - case keybind.Matches(msg, m.cfg.Keybinds.Logout.Keybind): + case keybind.Matches(msg, kbs.Logout.Keybind): return m, ui.ShowDialog( "Are you sure you want to log out?", ui.DialogButton{Label: "No"}, diff --git a/internal/ui/root/model.go b/internal/ui/root/model.go index e651a9f..b4bb383 100644 --- a/internal/ui/root/model.go +++ b/internal/ui/root/model.go @@ -107,18 +107,20 @@ func (m Model) Update(msg tview.Msg) (Model, tview.Cmd) { if m.dialog != nil { return m, nil } + + kbs := m.cfg.Keybinds switch { - case keybind.Matches(msg, m.cfg.Keybinds.ToggleHelp.Keybind): + case keybind.Matches(msg, kbs.ToggleHelp.Keybind): m.toggleHelp() return m, nil - case keybind.Matches(msg, m.cfg.Keybinds.ToggleFullHelp.Keybind): + case keybind.Matches(msg, kbs.ToggleFullHelp.Keybind): if m.cfg.Help.Enabled { m.helpShowAll = !m.helpShowAll } return m, nil - case keybind.Matches(msg, m.cfg.Keybinds.Suspend.Keybind): + case keybind.Matches(msg, kbs.Suspend.Keybind): return m, suspend() - case keybind.Matches(msg, m.cfg.Keybinds.Quit.Keybind): + case keybind.Matches(msg, kbs.Quit.Keybind): return m, ui.ShowDialog( "Are you sure you want to quit?", ui.DialogButton{Label: "No"},