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

better interface

This commit is contained in:
Jesse Duffield
2020-09-26 17:15:13 +10:00
parent 67bbeb195b
commit da8eac5538
4 changed files with 60 additions and 45 deletions

View File

@ -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
}