2022-02-24 04:29:48 +02:00
|
|
|
package custom_commands
|
|
|
|
|
|
|
|
import (
|
2023-05-29 06:24:49 +02:00
|
|
|
"fmt"
|
2022-07-29 08:12:36 +02:00
|
|
|
"strings"
|
2022-09-30 14:10:56 +02:00
|
|
|
"text/template"
|
2022-07-29 08:12:36 +02:00
|
|
|
|
2023-07-09 03:32:27 +02:00
|
|
|
"github.com/jesseduffield/gocui"
|
2022-02-24 04:29:48 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/config"
|
2023-03-23 03:35:07 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers"
|
2022-02-24 04:29:48 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/style"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
2023-05-29 14:46:18 +02:00
|
|
|
"github.com/samber/lo"
|
2022-02-24 04:29:48 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// takes a custom command and returns a function that will be called when the corresponding user-defined keybinding is pressed
|
|
|
|
type HandlerCreator struct {
|
2023-07-13 10:36:39 +02:00
|
|
|
c *helpers.HelperCommon
|
|
|
|
sessionStateLoader *SessionStateLoader
|
|
|
|
resolver *Resolver
|
|
|
|
menuGenerator *MenuGenerator
|
|
|
|
suggestionsHelper *helpers.SuggestionsHelper
|
|
|
|
mergeAndRebaseHelper *helpers.MergeAndRebaseHelper
|
2022-02-24 04:29:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewHandlerCreator(
|
2023-03-23 03:35:07 +02:00
|
|
|
c *helpers.HelperCommon,
|
2022-02-24 04:29:48 +02:00
|
|
|
sessionStateLoader *SessionStateLoader,
|
2023-05-29 06:24:49 +02:00
|
|
|
suggestionsHelper *helpers.SuggestionsHelper,
|
2023-07-13 10:36:39 +02:00
|
|
|
mergeAndRebaseHelper *helpers.MergeAndRebaseHelper,
|
2022-02-24 04:29:48 +02:00
|
|
|
) *HandlerCreator {
|
|
|
|
resolver := NewResolver(c.Common)
|
|
|
|
menuGenerator := NewMenuGenerator(c.Common)
|
|
|
|
|
|
|
|
return &HandlerCreator{
|
2023-07-13 10:36:39 +02:00
|
|
|
c: c,
|
|
|
|
sessionStateLoader: sessionStateLoader,
|
|
|
|
resolver: resolver,
|
|
|
|
menuGenerator: menuGenerator,
|
|
|
|
suggestionsHelper: suggestionsHelper,
|
|
|
|
mergeAndRebaseHelper: mergeAndRebaseHelper,
|
2022-02-24 04:29:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *HandlerCreator) call(customCommand config.CustomCommand) func() error {
|
|
|
|
return func() error {
|
|
|
|
sessionState := self.sessionStateLoader.call()
|
|
|
|
promptResponses := make([]string, len(customCommand.Prompts))
|
2022-08-09 18:52:19 +02:00
|
|
|
form := make(map[string]string)
|
2022-02-24 04:29:48 +02:00
|
|
|
|
2022-08-09 18:52:19 +02:00
|
|
|
f := func() error { return self.finalHandler(customCommand, sessionState, promptResponses, form) }
|
2022-02-24 04:29:48 +02:00
|
|
|
|
|
|
|
// if we have prompts we'll recursively wrap our confirm handlers with more prompts
|
|
|
|
// until we reach the actual command
|
|
|
|
for reverseIdx := range customCommand.Prompts {
|
|
|
|
// reassigning so that we don't end up with an infinite recursion
|
|
|
|
g := f
|
|
|
|
idx := len(customCommand.Prompts) - 1 - reverseIdx
|
|
|
|
|
|
|
|
// going backwards so the outermost prompt is the first one
|
|
|
|
prompt := customCommand.Prompts[idx]
|
|
|
|
|
|
|
|
wrappedF := func(response string) error {
|
|
|
|
promptResponses[idx] = response
|
2022-08-09 18:52:19 +02:00
|
|
|
form[prompt.Key] = response
|
2022-02-24 04:29:48 +02:00
|
|
|
return g()
|
|
|
|
}
|
|
|
|
|
2022-08-09 18:52:19 +02:00
|
|
|
resolveTemplate := self.getResolveTemplateFn(form, promptResponses, sessionState)
|
2022-02-24 04:29:48 +02:00
|
|
|
|
|
|
|
switch prompt.Type {
|
|
|
|
case "input":
|
|
|
|
f = func() error {
|
2022-11-25 23:09:02 +02:00
|
|
|
resolvedPrompt, err := self.resolver.resolvePrompt(&prompt, resolveTemplate)
|
|
|
|
if err != nil {
|
|
|
|
return self.c.Error(err)
|
|
|
|
}
|
2022-02-24 04:29:48 +02:00
|
|
|
return self.inputPrompt(resolvedPrompt, wrappedF)
|
|
|
|
}
|
|
|
|
case "menu":
|
|
|
|
f = func() error {
|
2022-11-25 23:09:02 +02:00
|
|
|
resolvedPrompt, err := self.resolver.resolvePrompt(&prompt, resolveTemplate)
|
|
|
|
if err != nil {
|
|
|
|
return self.c.Error(err)
|
|
|
|
}
|
2022-02-24 04:29:48 +02:00
|
|
|
return self.menuPrompt(resolvedPrompt, wrappedF)
|
|
|
|
}
|
|
|
|
case "menuFromCommand":
|
|
|
|
f = func() error {
|
2022-11-25 23:09:02 +02:00
|
|
|
resolvedPrompt, err := self.resolver.resolvePrompt(&prompt, resolveTemplate)
|
|
|
|
if err != nil {
|
|
|
|
return self.c.Error(err)
|
|
|
|
}
|
2022-02-24 04:29:48 +02:00
|
|
|
return self.menuPromptFromCommand(resolvedPrompt, wrappedF)
|
|
|
|
}
|
2022-06-25 07:37:10 +02:00
|
|
|
case "confirm":
|
|
|
|
f = func() error {
|
2022-11-25 23:09:02 +02:00
|
|
|
resolvedPrompt, err := self.resolver.resolvePrompt(&prompt, resolveTemplate)
|
|
|
|
if err != nil {
|
|
|
|
return self.c.Error(err)
|
|
|
|
}
|
2022-06-25 07:37:10 +02:00
|
|
|
return self.confirmPrompt(resolvedPrompt, g)
|
|
|
|
}
|
2022-02-24 04:29:48 +02:00
|
|
|
default:
|
2022-06-25 07:37:10 +02:00
|
|
|
return self.c.ErrorMsg("custom command prompt must have a type of 'input', 'menu', 'menuFromCommand', or 'confirm'")
|
2022-02-24 04:29:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return f()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *HandlerCreator) inputPrompt(prompt *config.CustomCommandPrompt, wrappedF func(string) error) error {
|
2023-05-29 14:46:18 +02:00
|
|
|
findSuggestionsFn, err := self.generateFindSuggestionsFunc(prompt)
|
|
|
|
if err != nil {
|
|
|
|
return self.c.Error(err)
|
2023-05-29 06:24:49 +02:00
|
|
|
}
|
|
|
|
|
2022-02-24 04:29:48 +02:00
|
|
|
return self.c.Prompt(types.PromptOpts{
|
2023-05-29 06:24:49 +02:00
|
|
|
Title: prompt.Title,
|
|
|
|
InitialContent: prompt.InitialValue,
|
|
|
|
FindSuggestionsFunc: findSuggestionsFn,
|
2022-02-24 04:29:48 +02:00
|
|
|
HandleConfirm: func(str string) error {
|
|
|
|
return wrappedF(str)
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-05-29 14:46:18 +02:00
|
|
|
func (self *HandlerCreator) generateFindSuggestionsFunc(prompt *config.CustomCommandPrompt) (func(string) []*types.Suggestion, error) {
|
|
|
|
if prompt.Suggestions.Preset != "" && prompt.Suggestions.Command != "" {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
fmt.Sprintf(
|
|
|
|
"Custom command prompt cannot have both a preset and a command for suggestions. Preset: '%s', Command: '%s'",
|
|
|
|
prompt.Suggestions.Preset,
|
|
|
|
prompt.Suggestions.Command,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
} else if prompt.Suggestions.Preset != "" {
|
|
|
|
return self.getPresetSuggestionsFn(prompt.Suggestions.Preset)
|
|
|
|
} else if prompt.Suggestions.Command != "" {
|
|
|
|
return self.getCommandSuggestionsFn(prompt.Suggestions.Command)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *HandlerCreator) getCommandSuggestionsFn(command string) (func(string) []*types.Suggestion, error) {
|
|
|
|
lines := []*types.Suggestion{}
|
|
|
|
err := self.c.OS().Cmd.NewShell(command).RunAndProcessLines(func(line string) (bool, error) {
|
|
|
|
lines = append(lines, &types.Suggestion{Value: line, Label: line})
|
|
|
|
return false, nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return func(currentWord string) []*types.Suggestion {
|
|
|
|
return lo.Filter(lines, func(suggestion *types.Suggestion, _ int) bool {
|
|
|
|
return strings.Contains(strings.ToLower(suggestion.Value), strings.ToLower(currentWord))
|
|
|
|
})
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-05-29 06:24:49 +02:00
|
|
|
func (self *HandlerCreator) getPresetSuggestionsFn(preset string) (func(string) []*types.Suggestion, error) {
|
|
|
|
switch preset {
|
2023-06-07 02:15:49 +02:00
|
|
|
case "authors":
|
|
|
|
return self.suggestionsHelper.GetAuthorsSuggestionsFunc(), nil
|
2023-05-29 06:24:49 +02:00
|
|
|
case "branches":
|
|
|
|
return self.suggestionsHelper.GetBranchNameSuggestionsFunc(), nil
|
2023-06-07 02:15:49 +02:00
|
|
|
case "files":
|
|
|
|
return self.suggestionsHelper.GetFilePathSuggestionsFunc(), nil
|
|
|
|
case "refs":
|
|
|
|
return self.suggestionsHelper.GetRefsSuggestionsFunc(), nil
|
2023-05-29 06:24:49 +02:00
|
|
|
case "remotes":
|
|
|
|
return self.suggestionsHelper.GetRemoteSuggestionsFunc(), nil
|
|
|
|
case "remoteBranches":
|
|
|
|
return self.suggestionsHelper.GetRemoteBranchesSuggestionsFunc("/"), nil
|
2023-06-07 02:15:49 +02:00
|
|
|
case "tags":
|
|
|
|
return self.suggestionsHelper.GetTagsSuggestionsFunc(), nil
|
2023-05-29 06:24:49 +02:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("Unknown value for suggestionsPreset in custom command: %s. Valid values: files, branches, remotes, remoteBranches, refs", preset)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-29 14:46:18 +02:00
|
|
|
func (self *HandlerCreator) confirmPrompt(prompt *config.CustomCommandPrompt, handleConfirm func() error) error {
|
|
|
|
return self.c.Confirm(types.ConfirmOpts{
|
|
|
|
Title: prompt.Title,
|
|
|
|
Prompt: prompt.Body,
|
|
|
|
HandleConfirm: handleConfirm,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-24 04:29:48 +02:00
|
|
|
func (self *HandlerCreator) menuPrompt(prompt *config.CustomCommandPrompt, wrappedF func(string) error) error {
|
2023-07-24 05:06:42 +02:00
|
|
|
menuItems := lo.Map(prompt.Options, func(option config.CustomCommandMenuOption, _ int) *types.MenuItem {
|
2022-03-19 10:12:58 +02:00
|
|
|
return &types.MenuItem{
|
2022-05-08 06:23:32 +02:00
|
|
|
LabelColumns: []string{option.Name, style.FgYellow.Sprint(option.Description)},
|
2022-02-24 04:29:48 +02:00
|
|
|
OnPress: func() error {
|
|
|
|
return wrappedF(option.Value)
|
|
|
|
},
|
|
|
|
}
|
2022-03-19 10:12:58 +02:00
|
|
|
})
|
2022-02-24 04:29:48 +02:00
|
|
|
|
|
|
|
return self.c.Menu(types.CreateMenuOptions{Title: prompt.Title, Items: menuItems})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *HandlerCreator) menuPromptFromCommand(prompt *config.CustomCommandPrompt, wrappedF func(string) error) error {
|
|
|
|
// Run and save output
|
2023-03-23 09:47:29 +02:00
|
|
|
message, err := self.c.Git().Custom.RunWithOutput(prompt.Command)
|
2022-02-24 04:29:48 +02:00
|
|
|
if err != nil {
|
|
|
|
return self.c.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Need to make a menu out of what the cmd has displayed
|
|
|
|
candidates, err := self.menuGenerator.call(message, prompt.Filter, prompt.ValueFormat, prompt.LabelFormat)
|
|
|
|
if err != nil {
|
|
|
|
return self.c.Error(err)
|
|
|
|
}
|
|
|
|
|
2023-07-24 05:06:42 +02:00
|
|
|
menuItems := lo.Map(candidates, func(candidate *commandMenuItem, _ int) *types.MenuItem {
|
2022-03-19 10:12:58 +02:00
|
|
|
return &types.MenuItem{
|
2022-05-08 06:23:32 +02:00
|
|
|
LabelColumns: []string{candidate.label},
|
2022-02-24 04:29:48 +02:00
|
|
|
OnPress: func() error {
|
2022-03-19 10:12:58 +02:00
|
|
|
return wrappedF(candidate.value)
|
2022-02-24 04:29:48 +02:00
|
|
|
},
|
|
|
|
}
|
2022-03-19 10:12:58 +02:00
|
|
|
})
|
2022-02-24 04:29:48 +02:00
|
|
|
|
|
|
|
return self.c.Menu(types.CreateMenuOptions{Title: prompt.Title, Items: menuItems})
|
|
|
|
}
|
|
|
|
|
|
|
|
type CustomCommandObjects struct {
|
|
|
|
*SessionState
|
|
|
|
PromptResponses []string
|
2022-08-09 18:52:19 +02:00
|
|
|
Form map[string]string
|
2022-02-24 04:29:48 +02:00
|
|
|
}
|
|
|
|
|
2022-08-09 18:52:19 +02:00
|
|
|
func (self *HandlerCreator) getResolveTemplateFn(form map[string]string, promptResponses []string, sessionState *SessionState) func(string) (string, error) {
|
2022-02-24 04:29:48 +02:00
|
|
|
objects := CustomCommandObjects{
|
|
|
|
SessionState: sessionState,
|
|
|
|
PromptResponses: promptResponses,
|
2022-08-09 18:52:19 +02:00
|
|
|
Form: form,
|
2022-02-24 04:29:48 +02:00
|
|
|
}
|
|
|
|
|
2022-09-30 14:10:56 +02:00
|
|
|
funcs := template.FuncMap{
|
2023-03-23 09:47:29 +02:00
|
|
|
"quote": self.c.OS().Quote,
|
2022-09-30 14:10:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return func(templateStr string) (string, error) { return utils.ResolveTemplate(templateStr, objects, funcs) }
|
2022-02-24 04:29:48 +02:00
|
|
|
}
|
|
|
|
|
2022-08-09 18:52:19 +02:00
|
|
|
func (self *HandlerCreator) finalHandler(customCommand config.CustomCommand, sessionState *SessionState, promptResponses []string, form map[string]string) error {
|
|
|
|
resolveTemplate := self.getResolveTemplateFn(form, promptResponses, sessionState)
|
2022-02-24 04:29:48 +02:00
|
|
|
cmdStr, err := resolveTemplate(customCommand.Command)
|
|
|
|
if err != nil {
|
|
|
|
return self.c.Error(err)
|
|
|
|
}
|
|
|
|
|
2023-03-23 09:47:29 +02:00
|
|
|
cmdObj := self.c.OS().Cmd.NewShell(cmdStr)
|
2022-02-24 04:29:48 +02:00
|
|
|
|
|
|
|
if customCommand.Subprocess {
|
|
|
|
return self.c.RunSubprocessAndRefresh(cmdObj)
|
|
|
|
}
|
|
|
|
|
|
|
|
loadingText := customCommand.LoadingText
|
|
|
|
if loadingText == "" {
|
2023-05-25 13:11:51 +02:00
|
|
|
loadingText = self.c.Tr.RunningCustomCommandStatus
|
2022-02-24 04:29:48 +02:00
|
|
|
}
|
|
|
|
|
2023-07-09 13:09:52 +02:00
|
|
|
return self.c.WithWaitingStatus(loadingText, func(gocui.Task) error {
|
2022-02-24 04:29:48 +02:00
|
|
|
self.c.LogAction(self.c.Tr.Actions.CustomCommand)
|
|
|
|
|
|
|
|
if customCommand.Stream {
|
|
|
|
cmdObj.StreamOutput()
|
|
|
|
}
|
2022-07-28 12:35:58 +02:00
|
|
|
output, err := cmdObj.RunWithOutput()
|
2023-07-13 10:36:39 +02:00
|
|
|
|
|
|
|
if refreshErr := self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC}); err != nil {
|
|
|
|
self.c.Log.Error(refreshErr)
|
|
|
|
}
|
|
|
|
|
2022-02-24 04:29:48 +02:00
|
|
|
if err != nil {
|
2023-07-13 10:36:39 +02:00
|
|
|
if customCommand.After.CheckForConflicts {
|
|
|
|
return self.mergeAndRebaseHelper.CheckForConflicts(err)
|
|
|
|
}
|
|
|
|
|
2022-02-24 04:29:48 +02:00
|
|
|
return self.c.Error(err)
|
|
|
|
}
|
2022-07-29 08:12:36 +02:00
|
|
|
|
2022-07-28 12:35:58 +02:00
|
|
|
if customCommand.ShowOutput {
|
2022-07-29 08:12:36 +02:00
|
|
|
if strings.TrimSpace(output) == "" {
|
|
|
|
output = self.c.Tr.EmptyOutput
|
|
|
|
}
|
2023-07-13 10:36:39 +02:00
|
|
|
return self.c.Alert(cmdStr, output)
|
2022-07-28 12:35:58 +02:00
|
|
|
}
|
2023-07-13 10:36:39 +02:00
|
|
|
|
|
|
|
return nil
|
2022-02-24 04:29:48 +02:00
|
|
|
})
|
|
|
|
}
|