Skip to content

Commit e92fed5

Browse files
committed
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.
1 parent 1b620f6 commit e92fed5

File tree

3 files changed

+58
-7
lines changed

3 files changed

+58
-7
lines changed

pkg/gui/controllers/global_controller.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,12 @@ func (self *GlobalController) GetKeybindings(opts types.KeybindingsOpts) []*type
115115
Handler: self.quitWithoutChangingDirectory,
116116
},
117117
{
118-
Key: opts.GetKey(opts.Config.Universal.Return),
119-
Modifier: gocui.ModNone,
120-
Handler: self.escape,
121-
Description: self.c.Tr.Cancel,
122-
DisplayOnScreen: true,
118+
Key: opts.GetKey(opts.Config.Universal.Return),
119+
Modifier: gocui.ModNone,
120+
Handler: self.escape,
121+
Description: self.c.Tr.Cancel,
122+
GetDisabledReason: self.escapeEnabled,
123+
DisplayOnScreen: true,
123124
},
124125
{
125126
Key: opts.GetKey(opts.Config.Universal.ToggleWhitespaceInDiffView),
@@ -190,6 +191,16 @@ func (self *GlobalController) escape() error {
190191
return (&QuitActions{c: self.c}).Escape()
191192
}
192193

194+
func (self *GlobalController) escapeEnabled() *types.DisabledReason {
195+
if (&QuitActions{c: self.c}).EscapeEnabled() {
196+
return nil
197+
}
198+
199+
// The empty error text is intentional. We don't want to show an error
200+
// toast for this, but only hide it from the options map.
201+
return &types.DisabledReason{Text: ""}
202+
}
203+
193204
func (self *GlobalController) toggleWhitespace() error {
194205
return (&ToggleWhitespaceAction{c: self.c}).Call()
195206
}

pkg/gui/controllers/quit_actions.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ func (self *QuitActions) confirmQuitDuringUpdate() error {
4848
}
4949

5050
func (self *QuitActions) Escape() error {
51+
// If you make changes to this function, be sure to update EscapeEnabled accordingly.
52+
5153
currentContext := self.c.Context().Current()
5254

5355
if listContext, ok := currentContext.(types.IListContext); ok {
@@ -90,3 +92,41 @@ func (self *QuitActions) Escape() error {
9092

9193
return nil
9294
}
95+
96+
func (self *QuitActions) EscapeEnabled() bool {
97+
currentContext := self.c.Context().Current()
98+
99+
if listContext, ok := currentContext.(types.IListContext); ok {
100+
if listContext.GetList().IsSelectingRange() {
101+
return true
102+
}
103+
}
104+
105+
if ctx, ok := currentContext.(types.IFilterableContext); ok {
106+
if ctx.IsFiltering() {
107+
return true
108+
}
109+
}
110+
111+
parentContext := currentContext.GetParentContext()
112+
if parentContext != nil {
113+
return true
114+
}
115+
116+
for _, mode := range self.c.Helpers().Mode.Statuses() {
117+
if mode.IsActive() {
118+
return true
119+
}
120+
}
121+
122+
repoPathStack := self.c.State().GetRepoPathStack()
123+
if !repoPathStack.IsEmpty() {
124+
return true
125+
}
126+
127+
if self.c.UserConfig().QuitOnTopLevelReturn {
128+
return true
129+
}
130+
131+
return false
132+
}

pkg/integration/tests/ui/keybinding_suggestions_when_switching_repos.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ var KeybindingSuggestionsWhenSwitchingRepos = NewIntegrationTest(NewIntegrationT
3131

3232
t.Views().Files().Focus()
3333
t.Views().Options().Content(
34-
Equals("Commit: c | Stash: s | Reset: D | Keybindings: ? | Cancel: <esc>"))
34+
Equals("Commit: c | Stash: s | Reset: D | Keybindings: ?"))
3535

3636
switchToRepo("other")
3737
switchToRepo("repo")
3838

3939
t.Views().Options().Content(
40-
Equals("Commit: c | Stash: s | Reset: D | Keybindings: ? | Cancel: <esc>"))
40+
Equals("Commit: c | Stash: s | Reset: D | Keybindings: ?"))
4141
},
4242
})

0 commit comments

Comments
 (0)