1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-08-24 19:39:16 +02:00

Avoid showing <esc> in options map when it doesn't do anything

The code duplication between Escape and EscapeEnabled is unfortunate, but I
don't see a better way to solve this.
This commit is contained in:
Stefan Haller
2025-08-12 14:54:49 +02:00
parent 0af439ddf5
commit 7bf05dfca4
3 changed files with 58 additions and 7 deletions

View File

@@ -115,11 +115,12 @@ func (self *GlobalController) GetKeybindings(opts types.KeybindingsOpts) []*type
Handler: self.quitWithoutChangingDirectory,
},
{
Key: opts.GetKey(opts.Config.Universal.Return),
Modifier: gocui.ModNone,
Handler: self.escape,
Description: self.c.Tr.Cancel,
DisplayOnScreen: true,
Key: opts.GetKey(opts.Config.Universal.Return),
Modifier: gocui.ModNone,
Handler: self.escape,
Description: self.c.Tr.Cancel,
GetDisabledReason: self.escapeEnabled,
DisplayOnScreen: true,
},
{
Key: opts.GetKey(opts.Config.Universal.ToggleWhitespaceInDiffView),
@@ -190,6 +191,16 @@ func (self *GlobalController) escape() error {
return (&QuitActions{c: self.c}).Escape()
}
func (self *GlobalController) escapeEnabled() *types.DisabledReason {
if (&QuitActions{c: self.c}).EscapeEnabled() {
return nil
}
// The empty error text is intentional. We don't want to show an error
// toast for this, but only hide it from the options map.
return &types.DisabledReason{Text: ""}
}
func (self *GlobalController) toggleWhitespace() error {
return (&ToggleWhitespaceAction{c: self.c}).Call()
}

View File

@@ -48,6 +48,8 @@ func (self *QuitActions) confirmQuitDuringUpdate() error {
}
func (self *QuitActions) Escape() error {
// If you make changes to this function, be sure to update EscapeEnabled accordingly.
currentContext := self.c.Context().Current()
if listContext, ok := currentContext.(types.IListContext); ok {
@@ -90,3 +92,41 @@ func (self *QuitActions) Escape() error {
return nil
}
func (self *QuitActions) EscapeEnabled() bool {
currentContext := self.c.Context().Current()
if listContext, ok := currentContext.(types.IListContext); ok {
if listContext.GetList().IsSelectingRange() {
return true
}
}
if ctx, ok := currentContext.(types.IFilterableContext); ok {
if ctx.IsFiltering() {
return true
}
}
parentContext := currentContext.GetParentContext()
if parentContext != nil {
return true
}
for _, mode := range self.c.Helpers().Mode.Statuses() {
if mode.IsActive() {
return true
}
}
repoPathStack := self.c.State().GetRepoPathStack()
if !repoPathStack.IsEmpty() {
return true
}
if self.c.UserConfig().QuitOnTopLevelReturn {
return true
}
return false
}

View File

@@ -31,12 +31,12 @@ var KeybindingSuggestionsWhenSwitchingRepos = NewIntegrationTest(NewIntegrationT
t.Views().Files().Focus()
t.Views().Options().Content(
Equals("Commit: c | Stash: s | Reset: D | Keybindings: ? | Cancel: <esc>"))
Equals("Commit: c | Stash: s | Reset: D | Keybindings: ?"))
switchToRepo("other")
switchToRepo("repo")
t.Views().Options().Content(
Equals("Commit: c | Stash: s | Reset: D | Keybindings: ? | Cancel: <esc>"))
Equals("Commit: c | Stash: s | Reset: D | Keybindings: ?"))
},
})