From be404068ffeda8eb43246db77f67573aae3079f3 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Sat, 28 Nov 2020 19:53:39 +1100 Subject: [PATCH] support labels for suggestions which are distinct from values --- pkg/gui/confirmation_panel.go | 5 +++-- pkg/gui/editors.go | 13 ++++++++++++- pkg/gui/gui.go | 3 ++- pkg/gui/presentation/suggestions.go | 10 +++++++--- pkg/gui/suggestions_panel.go | 18 +++++++++++++++--- pkg/gui/types/suggestion.go | 8 ++++++++ 6 files changed, 47 insertions(+), 10 deletions(-) create mode 100644 pkg/gui/types/suggestion.go diff --git a/pkg/gui/confirmation_panel.go b/pkg/gui/confirmation_panel.go index 0446fb919..fe17cc3f7 100644 --- a/pkg/gui/confirmation_panel.go +++ b/pkg/gui/confirmation_panel.go @@ -12,6 +12,7 @@ import ( "github.com/fatih/color" "github.com/jesseduffield/gocui" + "github.com/jesseduffield/lazygit/pkg/gui/types" "github.com/jesseduffield/lazygit/pkg/theme" "github.com/jesseduffield/lazygit/pkg/utils" ) @@ -206,7 +207,7 @@ func (gui *Gui) prepareConfirmationPanel(title, prompt string, hasLoader bool, s suggestionsView.Wrap = true suggestionsView.FgColor = theme.GocuiDefaultTextColor } - gui.setSuggestions([]string{}) + gui.setSuggestions([]*types.Suggestion{}) _, _ = gui.g.SetViewOnTop("suggestions") } @@ -279,7 +280,7 @@ func (gui *Gui) setKeyBindings(opts createPopupPanelOpts) error { return err } - onSuggestionConfirm := gui.wrappedPromptConfirmationFunction(opts.handlersManageFocus, opts.handleConfirmPrompt, func() string { return gui.getSelectedSuggestion() }) + onSuggestionConfirm := gui.wrappedPromptConfirmationFunction(opts.handlersManageFocus, opts.handleConfirmPrompt, func() string { return gui.getSelectedSuggestionValue() }) if err := gui.g.SetKeybinding("suggestions", nil, gui.getKey(keybindingConfig.Universal.Confirm), gocui.ModNone, onSuggestionConfirm); err != nil { return err } diff --git a/pkg/gui/editors.go b/pkg/gui/editors.go index baffd19a3..2e4cdd78c 100644 --- a/pkg/gui/editors.go +++ b/pkg/gui/editors.go @@ -2,6 +2,8 @@ package gui import ( "github.com/jesseduffield/gocui" + "github.com/jesseduffield/lazygit/pkg/gui/presentation" + "github.com/jesseduffield/lazygit/pkg/gui/types" "github.com/jesseduffield/lazygit/pkg/utils" ) @@ -78,7 +80,16 @@ func (gui *Gui) editorWithCallback(v *gocui.View, key gocui.Key, ch rune, mod go input := v.Buffer() branchNames := gui.getBranchNames() - suggestions := utils.FuzzySearch(input, branchNames) + matchingBranchNames := utils.FuzzySearch(input, branchNames) + + suggestions := make([]*types.Suggestion, len(matchingBranchNames)) + for i, branchName := range matchingBranchNames { + suggestions[i] = &types.Suggestion{ + Value: branchName, + Label: utils.ColoredString(branchName, presentation.GetBranchColor(branchName)), + } + } + gui.setSuggestions(suggestions) } diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index a90770ca9..f4e6f5b7f 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -21,6 +21,7 @@ import ( "github.com/jesseduffield/lazygit/pkg/commands/oscommands" "github.com/jesseduffield/lazygit/pkg/commands/patch" "github.com/jesseduffield/lazygit/pkg/config" + "github.com/jesseduffield/lazygit/pkg/gui/types" "github.com/jesseduffield/lazygit/pkg/i18n" "github.com/jesseduffield/lazygit/pkg/tasks" "github.com/jesseduffield/lazygit/pkg/theme" @@ -305,7 +306,7 @@ type guiState struct { StashEntries []*models.StashEntry CommitFiles []*models.CommitFile // Suggestions will sometimes appear when typing into a prompt - Suggestions []string + Suggestions []*types.Suggestion // FilteredReflogCommits are the ones that appear in the reflog panel. // when in filtering mode we only include the ones that match the given path FilteredReflogCommits []*models.Commit diff --git a/pkg/gui/presentation/suggestions.go b/pkg/gui/presentation/suggestions.go index 2e408e675..81c6a3a3d 100644 --- a/pkg/gui/presentation/suggestions.go +++ b/pkg/gui/presentation/suggestions.go @@ -1,6 +1,10 @@ package presentation -func GetSuggestionListDisplayStrings(suggestions []string) [][]string { +import ( + "github.com/jesseduffield/lazygit/pkg/gui/types" +) + +func GetSuggestionListDisplayStrings(suggestions []*types.Suggestion) [][]string { lines := make([][]string, len(suggestions)) for i := range suggestions { @@ -10,6 +14,6 @@ func GetSuggestionListDisplayStrings(suggestions []string) [][]string { return lines } -func getSuggestionDisplayStrings(suggestion string) []string { - return []string{suggestion} +func getSuggestionDisplayStrings(suggestion *types.Suggestion) []string { + return []string{suggestion.Label} } diff --git a/pkg/gui/suggestions_panel.go b/pkg/gui/suggestions_panel.go index eb3b7808a..9f5469496 100644 --- a/pkg/gui/suggestions_panel.go +++ b/pkg/gui/suggestions_panel.go @@ -1,15 +1,27 @@ package gui -func (gui *Gui) getSelectedSuggestion() string { +import "github.com/jesseduffield/lazygit/pkg/gui/types" + +func (gui *Gui) getSelectedSuggestionValue() string { + selectedSuggestion := gui.getSelectedSuggestion() + + if selectedSuggestion != nil { + return selectedSuggestion.Value + } + + return "" +} + +func (gui *Gui) getSelectedSuggestion() *types.Suggestion { selectedLine := gui.State.Panels.Suggestions.SelectedLineIdx if selectedLine == -1 { - return "" + return nil } return gui.State.Suggestions[selectedLine] } -func (gui *Gui) setSuggestions(suggestions []string) { +func (gui *Gui) setSuggestions(suggestions []*types.Suggestion) { view := gui.getSuggestionsView() if view == nil { return diff --git a/pkg/gui/types/suggestion.go b/pkg/gui/types/suggestion.go new file mode 100644 index 000000000..ed8b6ef44 --- /dev/null +++ b/pkg/gui/types/suggestion.go @@ -0,0 +1,8 @@ +package types + +type Suggestion struct { + // value is the thing that we're matching on and the thing that will be submitted if you select the suggestion + Value string + // label is what is actually displayed so it can e.g. contain color + Label string +}