Skip to content

Commit

Permalink
handle the pr and issue keys in different cases
Browse files Browse the repository at this point in the history
* remove universal switch view for per view binds
  • Loading branch information
miniscruff committed Jun 15, 2024
1 parent 112ee62 commit 321278c
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 94 deletions.
2 changes: 2 additions & 0 deletions ui/keys/issueKeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 0 additions & 2 deletions ui/keys/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
13 changes: 7 additions & 6 deletions ui/keys/prKeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,15 @@ 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":
key = &PRKeys.Assign
case "unassign":
key = &PRKeys.Unassign
case "comment":
PRKeys.Comment = prKey.NewBinding(&PRKeys.Comment)
continue
key = &PRKeys.Comment
case "diff":
key = &PRKeys.Diff
case "checkout":
Expand All @@ -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
Expand Down
209 changes: 123 additions & 86 deletions ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -650,15 +687,15 @@ 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
}
}
}

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
}
}
Expand Down

0 comments on commit 321278c

Please sign in to comment.