diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2020-09-26 15:23:28 +1000 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2020-09-27 09:49:30 +1000 |
commit | 67bbeb195b52121976840de0cd1a20cf1462b5e0 (patch) | |
tree | bd9cf6649b9040813bf82510692d7503716a28a7 /pkg/gui/custom_commands.go | |
parent | 92183de29edab53ccdfb99ddcfaa6fc2c359d925 (diff) |
support custom keybindings
Diffstat (limited to 'pkg/gui/custom_commands.go')
-rw-r--r-- | pkg/gui/custom_commands.go | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/pkg/gui/custom_commands.go b/pkg/gui/custom_commands.go new file mode 100644 index 000000000..8dcff163d --- /dev/null +++ b/pkg/gui/custom_commands.go @@ -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{}) + }) + } +} |