From ea42275f0619499fed921f4a7260594cfb282f4c Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Mon, 29 Jan 2024 22:06:26 +0100 Subject: [PATCH 1/2] Simplify saving app state --- pkg/gui/controllers/custom_command_action.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/pkg/gui/controllers/custom_command_action.go b/pkg/gui/controllers/custom_command_action.go index bd24cda7d..3cc7776bd 100644 --- a/pkg/gui/controllers/custom_command_action.go +++ b/pkg/gui/controllers/custom_command_action.go @@ -25,10 +25,7 @@ func (self *CustomCommandAction) Call() error { ) } - err := self.c.SaveAppState() - if err != nil { - self.c.Log.Error(err) - } + self.c.SaveAppStateAndLogError() self.c.LogAction(self.c.Tr.Actions.CustomCommand) return self.c.RunSubprocessAndRefresh( From 6c6201ab044687cf276946f4a3578c9fd76fda34 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Tue, 30 Jan 2024 08:24:15 +0100 Subject: [PATCH 2/2] Fix order problems when saving custom commands history This fixes two problems: - each time the custom commands panel was opened, the history of commands would be shown in reversed order compared to last time. (The reason is that lo.Reverse modifies the slice in place rather than just returning a new, reversed slice.) - when executing a previous command again (either by typing it in again, or by picking it from the history), it should move to the beginning of the history, but didn't. We fix this by storing the history in reversed order (as the user sees it in the panel), this makes the logic simpler. We just have to prepend rather than append newly added commands now. While this is theoretically a breaking change, it's not worth bothering because the order was wrong for existing users in 50% of the cases anyway. --- pkg/gui/controllers/custom_command_action.go | 5 +- .../tests/custom_commands/history.go | 60 +++++++++++++++++++ pkg/integration/tests/test_list.go | 1 + 3 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 pkg/integration/tests/custom_commands/history.go diff --git a/pkg/gui/controllers/custom_command_action.go b/pkg/gui/controllers/custom_command_action.go index 3cc7776bd..bc595934d 100644 --- a/pkg/gui/controllers/custom_command_action.go +++ b/pkg/gui/controllers/custom_command_action.go @@ -20,7 +20,7 @@ func (self *CustomCommandAction) Call() error { HandleConfirm: func(command string) error { if self.shouldSaveCommand(command) { self.c.GetAppState().CustomCommandsHistory = utils.Limit( - lo.Uniq(append(self.c.GetAppState().CustomCommandsHistory, command)), + lo.Uniq(append([]string{command}, self.c.GetAppState().CustomCommandsHistory...)), 1000, ) } @@ -36,8 +36,7 @@ func (self *CustomCommandAction) Call() error { } func (self *CustomCommandAction) GetCustomCommandsHistorySuggestionsFunc() func(string) []*types.Suggestion { - // reversing so that we display the latest command first - history := lo.Reverse(self.c.GetAppState().CustomCommandsHistory) + history := self.c.GetAppState().CustomCommandsHistory return helpers.FuzzySearchFunc(history) } diff --git a/pkg/integration/tests/custom_commands/history.go b/pkg/integration/tests/custom_commands/history.go new file mode 100644 index 000000000..834892564 --- /dev/null +++ b/pkg/integration/tests/custom_commands/history.go @@ -0,0 +1,60 @@ +package custom_commands + +import ( + "github.com/jesseduffield/lazygit/pkg/config" + . "github.com/jesseduffield/lazygit/pkg/integration/components" +) + +var History = NewIntegrationTest(NewIntegrationTestArgs{ + Description: "Test that the custom commands history is saved correctly", + ExtraCmdArgs: []string{}, + Skip: false, + SetupRepo: func(shell *Shell) {}, + SetupConfig: func(cfg *config.AppConfig) {}, + Run: func(t *TestDriver, keys config.KeybindingConfig) { + t.GlobalPress(keys.Universal.ExecuteCustomCommand) + t.ExpectPopup().Prompt(). + Title(Equals("Custom command:")). + Type("echo 1"). + Confirm() + + t.GlobalPress(keys.Universal.ExecuteCustomCommand) + t.ExpectPopup().Prompt(). + Title(Equals("Custom command:")). + SuggestionLines(Contains("1")). + Type("echo 2"). + Confirm() + + t.GlobalPress(keys.Universal.ExecuteCustomCommand) + t.ExpectPopup().Prompt(). + Title(Equals("Custom command:")). + SuggestionLines( + // "echo 2" was typed last, so it should come first + Contains("2"), + Contains("1"), + ). + Type("echo 3"). + Confirm() + + t.GlobalPress(keys.Universal.ExecuteCustomCommand) + t.ExpectPopup().Prompt(). + Title(Equals("Custom command:")). + SuggestionLines( + Contains("3"), + Contains("2"), + Contains("1"), + ). + Type("echo 1"). + Confirm() + + // Executing a command again should move it to the front: + t.GlobalPress(keys.Universal.ExecuteCustomCommand) + t.ExpectPopup().Prompt(). + Title(Equals("Custom command:")). + SuggestionLines( + Contains("1"), + Contains("3"), + Contains("2"), + ) + }, +}) diff --git a/pkg/integration/tests/test_list.go b/pkg/integration/tests/test_list.go index a5e17c470..3abc41853 100644 --- a/pkg/integration/tests/test_list.go +++ b/pkg/integration/tests/test_list.go @@ -98,6 +98,7 @@ var tests = []*components.IntegrationTest{ custom_commands.CheckForConflicts, custom_commands.ComplexCmdAtRuntime, custom_commands.FormPrompts, + custom_commands.History, custom_commands.MenuFromCommand, custom_commands.MenuFromCommandsOutput, custom_commands.MultiplePrompts,