Skip to content

Commit 1f920ae

Browse files
Fix crash on empty menu (#2799)
2 parents a548b28 + 932e01b commit 1f920ae

File tree

5 files changed

+45
-2
lines changed

5 files changed

+45
-2
lines changed

pkg/gui/context/menu_context.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ func (self *MenuContext) OnMenuPress(selectedItem *types.MenuItem) error {
135135
return err
136136
}
137137

138+
if selectedItem == nil {
139+
return nil
140+
}
141+
138142
if err := selectedItem.OnPress(); err != nil {
139143
return err
140144
}

pkg/gui/controllers/helpers/confirmation_helper.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,12 @@ func (self *ConfirmationHelper) resizeMenu() {
302302
_, _ = self.c.GocuiGui().SetView(self.c.Views().Menu.Name(), x0, y0, x1, menuBottom, 0)
303303

304304
tooltipTop := menuBottom + 1
305-
tooltipHeight := getMessageHeight(true, self.c.Contexts().Menu.GetSelected().Tooltip, panelWidth) + 2 // plus 2 for the frame
305+
tooltip := ""
306+
selectedItem := self.c.Contexts().Menu.GetSelected()
307+
if selectedItem != nil {
308+
tooltip = selectedItem.Tooltip
309+
}
310+
tooltipHeight := getMessageHeight(true, tooltip, panelWidth) + 2 // plus 2 for the frame
306311
_, _ = self.c.GocuiGui().SetView(self.c.Views().Tooltip.Name(), x0, tooltipTop, x1, tooltipTop+tooltipHeight-1, 0)
307312
}
308313

pkg/gui/controllers/menu_controller.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ func (self *MenuController) GetOnClick() func() error {
5353
func (self *MenuController) GetOnFocus() func(types.OnFocusOpts) error {
5454
return func(types.OnFocusOpts) error {
5555
selectedMenuItem := self.context().GetSelected()
56-
self.c.Views().Tooltip.SetContent(selectedMenuItem.Tooltip)
56+
if selectedMenuItem != nil {
57+
self.c.Views().Tooltip.SetContent(selectedMenuItem.Tooltip)
58+
}
5759
return nil
5860
}
5961
}

pkg/integration/tests/test_list.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ var tests = []*components.IntegrationTest{
211211
tag.Reset,
212212
ui.Accordion,
213213
ui.DoublePopup,
214+
ui.EmptyMenu,
214215
ui.SwitchTabFromMenu,
215216
undo.UndoCheckoutAndDrop,
216217
undo.UndoDrop,
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package ui
2+
3+
import (
4+
"github.com/jesseduffield/lazygit/pkg/config"
5+
. "github.com/jesseduffield/lazygit/pkg/integration/components"
6+
)
7+
8+
var EmptyMenu = NewIntegrationTest(NewIntegrationTestArgs{
9+
Description: "Verify that we don't crash on an empty menu",
10+
ExtraCmdArgs: []string{},
11+
Skip: false,
12+
SetupConfig: func(config *config.AppConfig) {},
13+
SetupRepo: func(shell *Shell) {
14+
},
15+
Run: func(t *TestDriver, keys config.KeybindingConfig) {
16+
t.Views().Files().
17+
IsFocused().
18+
Press(keys.Universal.OptionMenu)
19+
20+
t.Views().Menu().
21+
IsFocused().
22+
// a string that filters everything out
23+
FilterOrSearch("ljasldkjaslkdjalskdjalsdjaslkd").
24+
IsEmpty().
25+
Press(keys.Universal.Select)
26+
27+
// back in the files view, selecting the non-existing menu item was a no-op
28+
t.Views().Files().
29+
IsFocused()
30+
},
31+
})

0 commit comments

Comments
 (0)