Skip to content

Commit

Permalink
feat: universal keybinds (#435)
Browse files Browse the repository at this point in the history
* feat: universal keybinds

* fix: check repo name exists
  • Loading branch information
dlvhdr authored Sep 2, 2024
1 parent d7e563b commit 4d4f61e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
2 changes: 1 addition & 1 deletion ui/components/reposection/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ func (m *Model) newBranch(name string) tea.Cmd {
startCmd := m.Ctx.StartTask(task)
return tea.Batch(startCmd, func() tea.Msg {
// TODO: find out what the default branch is / use the currently selected branch
err := gitm.Checkout(*m.Ctx.RepoPath, name, gitm.CheckoutOptions{BaseBranch: "main"})
err := gitm.Checkout(*m.Ctx.RepoPath, name, gitm.CheckoutOptions{BaseBranch: "master"})
if err != nil {
return constants.TaskFinishedMsg{TaskId: taskId, Err: err}
}
Expand Down
40 changes: 31 additions & 9 deletions ui/modelUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ type IssueCommandTemplateInput struct {
func (m *Model) executeKeybinding(key string) tea.Cmd {
currRowData := m.getCurrRowData()

for _, keybinding := range m.ctx.Config.Keybindings.Universal {
if keybinding.Key != key {
continue
}

log.Debug("executing keybind", "key", keybinding.Key, "command", keybinding.Command)
return m.runCustomUniversalCommand(keybinding.Command)
}

switch m.ctx.View {
case config.IssuesView:
for _, keybinding := range m.ctx.Config.Keybindings.Issues {
Expand Down Expand Up @@ -132,8 +141,10 @@ func (m *Model) runCustomCommand(commandTemplate string, contextData *map[string
}

// Append in the local RepoPath only if it can be found
if repoPath, ok := common.GetRepoLocalPath(input["RepoName"].(string), m.ctx.Config.RepoPaths); ok {
input["RepoPath"] = repoPath
if input["RepoName"] != nil {
if repoPath, ok := common.GetRepoLocalPath(input["RepoName"].(string), m.ctx.Config.RepoPaths); ok {
input["RepoPath"] = repoPath
}
}

cmd, err := template.New("keybinding_command").Parse(commandTemplate)
Expand Down Expand Up @@ -177,13 +188,24 @@ func (m *Model) runCustomBranchCommand(commandTemplate string, branchData *data.
if reflect.ValueOf(branchData).IsNil() {
return m.executeCustomCommand(commandTemplate)
}
return m.runCustomCommand(commandTemplate,
&map[string]any{
"RepoName": branchData.GetRepoNameWithOwner(),
"PrNumber": branchData.Number,
"HeadRefName": branchData.HeadRefName,
"BaseRefName": branchData.BaseRefName,
})
input := map[string]any{
"RepoPath": m.ctx.RepoPath,
}
if branchData != nil {
maps.Copy(input,
map[string]any{
"RepoName": branchData.GetRepoNameWithOwner(),
"PrNumber": branchData.Number,
"HeadRefName": branchData.HeadRefName,
"BaseRefName": branchData.BaseRefName,
})
}
return m.runCustomCommand(commandTemplate, &input)
}

func (m *Model) runCustomUniversalCommand(commandTemplate string) tea.Cmd {
input := map[string]any{"RepoPath": m.ctx.RepoPath}
return m.runCustomCommand(commandTemplate, &input)
}

func (m *Model) executeCustomCommand(cmd string) tea.Cmd {
Expand Down
6 changes: 6 additions & 0 deletions ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,12 @@ func (m *Model) switchSelectedView() config.ViewType {
}

func (m *Model) isUserDefinedKeybinding(msg tea.KeyMsg) bool {
for _, keybinding := range m.ctx.Config.Keybindings.Universal {
if keybinding.Builtin == "" && keybinding.Key == msg.String() {
return true
}
}

if m.ctx.View == config.IssuesView {
for _, keybinding := range m.ctx.Config.Keybindings.Issues {
if keybinding.Builtin == "" && keybinding.Key == msg.String() {
Expand Down

0 comments on commit 4d4f61e

Please sign in to comment.