From 321278c531226b0fc6a87cc99841553e03f9dfa7 Mon Sep 17 00:00:00 2001 From: Ronnie Smith Date: Sat, 15 Jun 2024 11:51:38 -0700 Subject: [PATCH] handle the pr and issue keys in different cases * remove universal switch view for per view binds --- ui/keys/issueKeys.go | 2 + ui/keys/keys.go | 2 - ui/keys/prKeys.go | 13 +-- ui/ui.go | 209 +++++++++++++++++++++++++------------------ 4 files changed, 132 insertions(+), 94 deletions(-) diff --git a/ui/keys/issueKeys.go b/ui/keys/issueKeys.go index a44f49d3..c960ec98 100644 --- a/ui/keys/issueKeys.go +++ b/ui/keys/issueKeys.go @@ -76,6 +76,8 @@ func rebindIssueKeys(keys []config.Keybinding) error { key = &IssueKeys.Close case "reopen": key = &IssueKeys.Reopen + case "viewPrs": + key = &IssueKeys.ViewPRs default: return fmt.Errorf("unknown built-in issue key: '%s'", issueKey.Builtin) } diff --git a/ui/keys/keys.go b/ui/keys/keys.go index 20ad3178..9f0167f2 100644 --- a/ui/keys/keys.go +++ b/ui/keys/keys.go @@ -207,8 +207,6 @@ func rebindUniversal(universal []config.Keybinding) error { key = &Keys.NextSection case "prevSection": key = &Keys.PrevSection - case "switchView": - key = &Keys.SwitchView case "search": key = &Keys.Search case "copyurl": diff --git a/ui/keys/prKeys.go b/ui/keys/prKeys.go index d3c6ae24..c721fd5e 100644 --- a/ui/keys/prKeys.go +++ b/ui/keys/prKeys.go @@ -93,7 +93,7 @@ func rebindPRKeys(keys []config.Keybinding) error { log.Debug("Rebinding PR key", "builtin", prKey.Builtin, "key", prKey.Key) - var key *key.Binding + var key *key.Binding switch prKey.Builtin { case "assign": @@ -101,8 +101,7 @@ func rebindPRKeys(keys []config.Keybinding) error { case "unassign": key = &PRKeys.Unassign case "comment": - PRKeys.Comment = prKey.NewBinding(&PRKeys.Comment) - continue + key = &PRKeys.Comment case "diff": key = &PRKeys.Diff case "checkout": @@ -117,12 +116,14 @@ func rebindPRKeys(keys []config.Keybinding) error { key = &PRKeys.Merge case "watchChecks": key = &PRKeys.WatchChecks + case "viewIssues": + key = &PRKeys.ViewIssues default: - return fmt.Errorf("unknown built-in pr key: '%s'", prKey.Builtin) + return fmt.Errorf("unknown built-in pr key: '%s'", prKey.Builtin) } - key.SetKeys(prKey.Key) - key.SetHelp(prKey.Key, key.Help().Desc) + key.SetKeys(prKey.Key) + key.SetHelp(prKey.Key, key.Help().Desc) } return nil diff --git a/ui/ui.go b/ui/ui.go index ed5c2825..9cfa90c9 100644 --- a/ui/ui.go +++ b/ui/ui.go @@ -228,96 +228,12 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.setCurrentViewSections(newSections) cmds = append(cmds, fetchSectionsCmds) - case key.Matches(msg, keys.PRKeys.ViewIssues, keys.IssueKeys.ViewPRs): - m.ctx.View = m.switchSelectedView() - m.syncMainContentWidth() - m.setCurrSectionId(1) - - currSections := m.getCurrentViewSections() - if len(currSections) == 0 { - newSections, fetchSectionsCmds := m.fetchAllViewSections() - m.setCurrentViewSections(newSections) - cmd = fetchSectionsCmds - } - m.onViewedRowChanged() - case key.Matches(msg, m.keys.Search): if currSection != nil { cmd = currSection.SetIsSearching(true) return m, cmd } - case key.Matches(msg, keys.PRKeys.Comment, keys.IssueKeys.Comment): - log.Debug("pr comment key", "key", msg.String(), "comment keybind", keys.PRKeys.Comment.Keys()) - m.sidebar.IsOpen = true - if m.ctx.View == config.PRsView { - cmd = m.prSidebar.SetIsCommenting(true) - } else { - cmd = m.issueSidebar.SetIsCommenting(true) - } - m.syncMainContentWidth() - m.syncSidebar() - m.sidebar.ScrollToBottom() - return m, cmd - - case key.Matches( - msg, - keys.PRKeys.Close, - keys.PRKeys.Reopen, - keys.PRKeys.Ready, - keys.PRKeys.Merge, - keys.IssueKeys.Close, - keys.IssueKeys.Reopen, - ): - - var action string - switch { - case key.Matches(msg, keys.PRKeys.Close, keys.IssueKeys.Close): - action = "close" - - case key.Matches(msg, keys.PRKeys.Reopen, keys.IssueKeys.Reopen): - action = "reopen" - - case key.Matches(msg, keys.PRKeys.Ready): - action = "ready" - - case key.Matches(msg, keys.PRKeys.Merge): - action = "merge" - } - - if currSection != nil { - currSection.SetPromptConfirmationAction(action) - cmd = currSection.SetIsPromptConfirmationShown(true) - } - return m, cmd - - // I do not think this state machine works quite well enough with custom keybinds - // as it kinda breaks down if you use two different keys across the views. - // Is it worth refactoring this to first check the "view" before the key? - case key.Matches(msg, keys.IssueKeys.Assign), key.Matches(msg, keys.PRKeys.Assign): - m.sidebar.IsOpen = true - if m.ctx.View == config.PRsView { - cmd = m.prSidebar.SetIsAssigning(true) - } else { - cmd = m.issueSidebar.SetIsAssigning(true) - } - m.syncMainContentWidth() - m.syncSidebar() - m.sidebar.ScrollToBottom() - return m, cmd - - case key.Matches(msg, keys.IssueKeys.Unassign), key.Matches(msg, keys.PRKeys.Unassign): - m.sidebar.IsOpen = true - if m.ctx.View == config.PRsView { - cmd = m.prSidebar.SetIsUnassigning(true) - } else { - cmd = m.issueSidebar.SetIsUnassigning(true) - } - m.syncMainContentWidth() - m.syncSidebar() - m.sidebar.ScrollToBottom() - return m, cmd - case key.Matches(msg, m.keys.Help): if !m.footer.ShowAll { m.ctx.MainContentHeight = m.ctx.MainContentHeight + common.FooterHeight - common.ExpandedHelpHeight @@ -353,6 +269,127 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { return m, cmd } cmd = tea.Quit + + case m.ctx.View == config.PRsView: + switch { + case key.Matches(msg, keys.PRKeys.Assign): + m.sidebar.IsOpen = true + cmd = m.prSidebar.SetIsAssigning(true) + m.syncMainContentWidth() + m.syncSidebar() + m.sidebar.ScrollToBottom() + return m, cmd + + case key.Matches(msg, keys.PRKeys.Unassign): + m.sidebar.IsOpen = true + cmd = m.prSidebar.SetIsUnassigning(true) + m.syncMainContentWidth() + m.syncSidebar() + m.sidebar.ScrollToBottom() + return m, cmd + + case key.Matches(msg, keys.PRKeys.Comment): + m.sidebar.IsOpen = true + cmd = m.prSidebar.SetIsCommenting(true) + m.syncMainContentWidth() + m.syncSidebar() + m.sidebar.ScrollToBottom() + return m, cmd + + case key.Matches(msg, keys.PRKeys.Close): + if currSection != nil { + currSection.SetPromptConfirmationAction("close") + cmd = currSection.SetIsPromptConfirmationShown(true) + } + return m, cmd + + case key.Matches(msg, keys.PRKeys.Ready): + if currSection != nil { + currSection.SetPromptConfirmationAction("ready") + cmd = currSection.SetIsPromptConfirmationShown(true) + } + return m, cmd + + case key.Matches(msg, keys.PRKeys.Reopen): + if currSection != nil { + currSection.SetPromptConfirmationAction("reopen") + cmd = currSection.SetIsPromptConfirmationShown(true) + } + return m, cmd + + case key.Matches(msg, keys.PRKeys.Merge): + if currSection != nil { + currSection.SetPromptConfirmationAction("merge") + cmd = currSection.SetIsPromptConfirmationShown(true) + } + return m, cmd + + case key.Matches(msg, keys.PRKeys.ViewIssues): + m.ctx.View = m.switchSelectedView() + m.syncMainContentWidth() + m.setCurrSectionId(1) + + currSections := m.getCurrentViewSections() + if len(currSections) == 0 { + newSections, fetchSectionsCmds := m.fetchAllViewSections() + m.setCurrentViewSections(newSections) + cmd = fetchSectionsCmds + } + m.onViewedRowChanged() + } + case m.ctx.View == config.IssuesView: + switch { + case key.Matches(msg, keys.IssueKeys.Assign): + m.sidebar.IsOpen = true + cmd = m.issueSidebar.SetIsAssigning(true) + m.syncMainContentWidth() + m.syncSidebar() + m.sidebar.ScrollToBottom() + return m, cmd + + case key.Matches(msg, keys.IssueKeys.Unassign): + m.sidebar.IsOpen = true + cmd = m.issueSidebar.SetIsUnassigning(true) + m.syncMainContentWidth() + m.syncSidebar() + m.sidebar.ScrollToBottom() + return m, cmd + + case key.Matches(msg, keys.IssueKeys.Comment): + m.sidebar.IsOpen = true + cmd = m.issueSidebar.SetIsCommenting(true) + m.syncMainContentWidth() + m.syncSidebar() + m.sidebar.ScrollToBottom() + return m, cmd + + case key.Matches(msg, keys.IssueKeys.Close): + if currSection != nil { + currSection.SetPromptConfirmationAction("close") + cmd = currSection.SetIsPromptConfirmationShown(true) + } + return m, cmd + + case key.Matches(msg, keys.IssueKeys.Reopen): + if currSection != nil { + currSection.SetPromptConfirmationAction("reopen") + cmd = currSection.SetIsPromptConfirmationShown(true) + } + return m, cmd + + case key.Matches(msg, keys.IssueKeys.ViewPRs): + m.ctx.View = m.switchSelectedView() + m.syncMainContentWidth() + m.setCurrSectionId(1) + + currSections := m.getCurrentViewSections() + if len(currSections) == 0 { + newSections, fetchSectionsCmds := m.fetchAllViewSections() + m.setCurrentViewSections(newSections) + cmd = fetchSectionsCmds + } + m.onViewedRowChanged() + } } case initMsg: @@ -650,7 +687,7 @@ func (m *Model) switchSelectedView() config.ViewType { func (m *Model) isUserDefinedKeybinding(msg tea.KeyMsg) bool { if m.ctx.View == config.IssuesView { for _, keybinding := range m.ctx.Config.Keybindings.Issues { - if keybinding.Key == msg.String() { + if keybinding.Builtin == "" && keybinding.Key == msg.String() { return true } } @@ -658,7 +695,7 @@ func (m *Model) isUserDefinedKeybinding(msg tea.KeyMsg) bool { if m.ctx.View == config.PRsView { for _, keybinding := range m.ctx.Config.Keybindings.Prs { - if keybinding.Key == msg.String() { + if keybinding.Builtin == "" && keybinding.Key == msg.String() { return true } }