diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2020-09-26 17:15:13 +1000 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2020-09-27 09:49:30 +1000 |
commit | da8eac5538b236e4b79e3bab6465e8d5666c48e0 (patch) | |
tree | 95b8332360e10990dd8ea7047d53a89510e91471 /pkg/gui/custom_commands.go | |
parent | 67bbeb195b52121976840de0cd1a20cf1462b5e0 (diff) |
better interface
Diffstat (limited to 'pkg/gui/custom_commands.go')
-rw-r--r-- | pkg/gui/custom_commands.go | 57 |
1 files changed, 55 insertions, 2 deletions
diff --git a/pkg/gui/custom_commands.go b/pkg/gui/custom_commands.go index 8dcff163d..190fc5586 100644 --- a/pkg/gui/custom_commands.go +++ b/pkg/gui/custom_commands.go @@ -2,8 +2,10 @@ package gui import ( "bytes" + "log" "text/template" + "github.com/jesseduffield/gocui" "github.com/jesseduffield/lazygit/pkg/commands" ) @@ -21,7 +23,7 @@ type CustomCommandObjects struct { CurrentBranch *commands.Branch } -func (gui *Gui) handleCustomCommandKeybinding(templateStr string) func() error { +func (gui *Gui) handleCustomCommandKeybinding(customCommand CustomCommand) func() error { return func() error { objects := CustomCommandObjects{ SelectedFile: gui.getSelectedFile(), @@ -37,7 +39,7 @@ func (gui *Gui) handleCustomCommandKeybinding(templateStr string) func() error { CurrentBranch: gui.currentBranch(), } - tmpl, err := template.New("custom command template").Parse(templateStr) + tmpl, err := template.New("custom command template").Parse(customCommand.Command) if err != nil { return gui.surfaceError(err) } @@ -49,7 +51,14 @@ func (gui *Gui) handleCustomCommandKeybinding(templateStr string) func() error { cmdStr := buf.String() + if customCommand.Subprocess { + gui.PrepareSubProcess(cmdStr) + return nil + } + return gui.WithWaitingStatus(gui.Tr.SLocalize("runningCustomCommandStatus"), func() error { + gui.OSCommand.PrepareSubProcess(cmdStr) + if err := gui.OSCommand.RunCommand(cmdStr); err != nil { return gui.surfaceError(err) } @@ -57,3 +66,47 @@ func (gui *Gui) handleCustomCommandKeybinding(templateStr string) func() error { }) } } + +type CustomCommand struct { + Key string `yaml:"key"` + Context string `yaml:"context"` + Command string `yaml:"command"` + Subprocess bool `yaml:"subprocess"` +} + +func (gui *Gui) GetCustomCommandKeybindings() []*Binding { + bindings := []*Binding{} + + var customCommands []CustomCommand + + if err := gui.Config.GetUserConfig().UnmarshalKey("customCommands", &customCommands); err != nil { + log.Fatalf("Error parsing custom command keybindings: %v", err) + } + + for _, customCommand := range customCommands { + var viewName string + if customCommand.Context == "global" || customCommand.Context == "" { + viewName = "" + } else { + context := gui.contextForContextKey(customCommand.Context) + if context == nil { + log.Fatalf("Error when setting custom command keybindings: unknown context: %s", customCommand.Context) + } + // here we assume that a given context will always belong to the same view. + // Currently this is a safe bet but it's by no means guaranteed in the long term + // and we might need to make some changes in the future to support it. + viewName = context.GetViewName() + } + + bindings = append(bindings, &Binding{ + ViewName: viewName, + Contexts: []string{customCommand.Context}, + Key: gui.getKey(customCommand.Key), + Modifier: gocui.ModNone, + Handler: gui.wrappedHandler(gui.handleCustomCommandKeybinding(customCommand)), + Description: customCommand.Command, + }) + } + + return bindings +} |