mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-02-13 13:59:06 +02:00
Don't add custom command to history if it starts with space
Add tests for custom command with leading space
This commit is contained in:
parent
caedf57484
commit
2b4ac986a2
@ -1,6 +1,8 @@
|
|||||||
package controllers
|
package controllers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/jesseduffield/generics/slices"
|
"github.com/jesseduffield/generics/slices"
|
||||||
"github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers"
|
"github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers"
|
||||||
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
||||||
@ -37,10 +39,12 @@ func (self *GlobalController) customCommand() error {
|
|||||||
Title: self.c.Tr.CustomCommand,
|
Title: self.c.Tr.CustomCommand,
|
||||||
FindSuggestionsFunc: self.GetCustomCommandsHistorySuggestionsFunc(),
|
FindSuggestionsFunc: self.GetCustomCommandsHistorySuggestionsFunc(),
|
||||||
HandleConfirm: func(command string) error {
|
HandleConfirm: func(command string) error {
|
||||||
self.c.GetAppState().CustomCommandsHistory = utils.Limit(
|
if self.shouldSaveCommand(command) {
|
||||||
lo.Uniq(append(self.c.GetAppState().CustomCommandsHistory, command)),
|
self.c.GetAppState().CustomCommandsHistory = utils.Limit(
|
||||||
1000,
|
lo.Uniq(append(self.c.GetAppState().CustomCommandsHistory, command)),
|
||||||
)
|
1000,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
err := self.c.SaveAppState()
|
err := self.c.SaveAppState()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -55,6 +59,12 @@ func (self *GlobalController) customCommand() error {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// this mimics the shell functionality `ignorespace`
|
||||||
|
// which doesn't save a command to history if it starts with a space
|
||||||
|
func (self *GlobalController) shouldSaveCommand(command string) bool {
|
||||||
|
return !strings.HasPrefix(command, " ")
|
||||||
|
}
|
||||||
|
|
||||||
func (self *GlobalController) GetCustomCommandsHistorySuggestionsFunc() func(string) []*types.Suggestion {
|
func (self *GlobalController) GetCustomCommandsHistorySuggestionsFunc() func(string) []*types.Suggestion {
|
||||||
// reversing so that we display the latest command first
|
// reversing so that we display the latest command first
|
||||||
history := slices.Reverse(self.c.GetAppState().CustomCommandsHistory)
|
history := slices.Reverse(self.c.GetAppState().CustomCommandsHistory)
|
||||||
|
35
pkg/integration/tests/custom_commands/basic_at_runtime.go
Normal file
35
pkg/integration/tests/custom_commands/basic_at_runtime.go
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
package custom_commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/jesseduffield/lazygit/pkg/config"
|
||||||
|
. "github.com/jesseduffield/lazygit/pkg/integration/components"
|
||||||
|
)
|
||||||
|
|
||||||
|
var BasicCmdAtRuntime = NewIntegrationTest(NewIntegrationTestArgs{
|
||||||
|
Description: "Using a custom command provided at runtime to create a new file",
|
||||||
|
ExtraCmdArgs: "",
|
||||||
|
Skip: false,
|
||||||
|
SetupRepo: func(shell *Shell) {
|
||||||
|
shell.EmptyCommit("blah")
|
||||||
|
},
|
||||||
|
SetupConfig: func(cfg *config.AppConfig) {},
|
||||||
|
Run: func(t *TestDriver, keys config.KeybindingConfig) {
|
||||||
|
t.Views().Files().
|
||||||
|
IsEmpty().
|
||||||
|
IsFocused().
|
||||||
|
Press(keys.Universal.ExecuteCustomCommand)
|
||||||
|
|
||||||
|
t.ExpectPopup().Prompt().
|
||||||
|
Title(Equals("Custom Command:")).
|
||||||
|
Type("touch file.txt").
|
||||||
|
Confirm()
|
||||||
|
|
||||||
|
t.GlobalPress(keys.Files.RefreshFiles)
|
||||||
|
|
||||||
|
t.Views().Files().
|
||||||
|
IsFocused().
|
||||||
|
Lines(
|
||||||
|
Contains("file.txt"),
|
||||||
|
)
|
||||||
|
},
|
||||||
|
})
|
@ -0,0 +1,38 @@
|
|||||||
|
package custom_commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/jesseduffield/lazygit/pkg/config"
|
||||||
|
. "github.com/jesseduffield/lazygit/pkg/integration/components"
|
||||||
|
)
|
||||||
|
|
||||||
|
var OmitFromHistory = NewIntegrationTest(NewIntegrationTestArgs{
|
||||||
|
Description: "Omitting a runtime custom command from history if it begins with space",
|
||||||
|
ExtraCmdArgs: "",
|
||||||
|
Skip: false,
|
||||||
|
SetupRepo: func(shell *Shell) {
|
||||||
|
shell.EmptyCommit("blah")
|
||||||
|
},
|
||||||
|
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 aubergine").
|
||||||
|
Confirm()
|
||||||
|
|
||||||
|
t.GlobalPress(keys.Universal.ExecuteCustomCommand)
|
||||||
|
t.ExpectPopup().Prompt().
|
||||||
|
Title(Equals("Custom Command:")).
|
||||||
|
SuggestionLines(Contains("aubergine")).
|
||||||
|
SuggestionLines(DoesNotContain("tangerine")).
|
||||||
|
Type(" echo tangerine").
|
||||||
|
Confirm()
|
||||||
|
|
||||||
|
t.GlobalPress(keys.Universal.ExecuteCustomCommand)
|
||||||
|
t.ExpectPopup().Prompt().
|
||||||
|
Title(Equals("Custom Command:")).
|
||||||
|
SuggestionLines(Contains("aubergine")).
|
||||||
|
SuggestionLines(DoesNotContain("tangerine")).
|
||||||
|
Cancel()
|
||||||
|
},
|
||||||
|
})
|
@ -5,7 +5,7 @@ import (
|
|||||||
. "github.com/jesseduffield/lazygit/pkg/integration/components"
|
. "github.com/jesseduffield/lazygit/pkg/integration/components"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Basic = NewIntegrationTest(NewIntegrationTestArgs{
|
var BasicCmdFromConfig = NewIntegrationTest(NewIntegrationTestArgs{
|
||||||
Description: "Using a custom command to create a new file",
|
Description: "Using a custom command to create a new file",
|
||||||
ExtraCmdArgs: "",
|
ExtraCmdArgs: "",
|
||||||
Skip: false,
|
Skip: false,
|
@ -64,7 +64,9 @@ var tests = []*components.IntegrationTest{
|
|||||||
conflicts.ResolveExternally,
|
conflicts.ResolveExternally,
|
||||||
conflicts.ResolveMultipleFiles,
|
conflicts.ResolveMultipleFiles,
|
||||||
conflicts.UndoChooseHunk,
|
conflicts.UndoChooseHunk,
|
||||||
custom_commands.Basic,
|
custom_commands.BasicCmdFromConfig,
|
||||||
|
custom_commands.BasicCmdAtRuntime,
|
||||||
|
custom_commands.OmitFromHistory,
|
||||||
custom_commands.FormPrompts,
|
custom_commands.FormPrompts,
|
||||||
custom_commands.MenuFromCommand,
|
custom_commands.MenuFromCommand,
|
||||||
custom_commands.MenuFromCommandsOutput,
|
custom_commands.MenuFromCommandsOutput,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user