1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-07-30 03:23:08 +03:00

support custom keybindings

This commit is contained in:
Jesse Duffield
2020-09-26 15:23:28 +10:00
parent 92183de29e
commit 67bbeb195b
4 changed files with 109 additions and 6 deletions

View File

@ -0,0 +1,59 @@
package gui
import (
"bytes"
"text/template"
"github.com/jesseduffield/lazygit/pkg/commands"
)
type CustomCommandObjects struct {
SelectedLocalCommit *commands.Commit
SelectedReflogCommit *commands.Commit
SelectedSubCommit *commands.Commit
SelectedFile *commands.File
SelectedLocalBranch *commands.Branch
SelectedRemoteBranch *commands.RemoteBranch
SelectedRemote *commands.Remote
SelectedTag *commands.Tag
SelectedStashEntry *commands.StashEntry
SelectedCommitFile *commands.CommitFile
CurrentBranch *commands.Branch
}
func (gui *Gui) handleCustomCommandKeybinding(templateStr string) func() error {
return func() error {
objects := CustomCommandObjects{
SelectedFile: gui.getSelectedFile(),
SelectedLocalCommit: gui.getSelectedLocalCommit(),
SelectedReflogCommit: gui.getSelectedReflogCommit(),
SelectedLocalBranch: gui.getSelectedBranch(),
SelectedRemoteBranch: gui.getSelectedRemoteBranch(),
SelectedRemote: gui.getSelectedRemote(),
SelectedTag: gui.getSelectedTag(),
SelectedStashEntry: gui.getSelectedStashEntry(),
SelectedCommitFile: gui.getSelectedCommitFile(),
SelectedSubCommit: gui.getSelectedSubCommit(),
CurrentBranch: gui.currentBranch(),
}
tmpl, err := template.New("custom command template").Parse(templateStr)
if err != nil {
return gui.surfaceError(err)
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, objects); err != nil {
return gui.surfaceError(err)
}
cmdStr := buf.String()
return gui.WithWaitingStatus(gui.Tr.SLocalize("runningCustomCommandStatus"), func() error {
if err := gui.OSCommand.RunCommand(cmdStr); err != nil {
return gui.surfaceError(err)
}
return gui.refreshSidePanels(refreshOptions{})
})
}
}