From 373f24c80f2a99f7b4e1efd3a4c1f5fdb375e3bf Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Thu, 20 Jul 2023 21:05:52 +1000 Subject: [PATCH 1/2] Fix crash on empty menu When a menu is empty (e.g. due to filtering) we shouldn't crash on focus or selection --- pkg/gui/context/menu_context.go | 4 ++++ pkg/gui/controllers/helpers/confirmation_helper.go | 7 ++++++- pkg/gui/controllers/menu_controller.go | 4 +++- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/pkg/gui/context/menu_context.go b/pkg/gui/context/menu_context.go index 088640ea0..a353a4e0d 100644 --- a/pkg/gui/context/menu_context.go +++ b/pkg/gui/context/menu_context.go @@ -135,6 +135,10 @@ func (self *MenuContext) OnMenuPress(selectedItem *types.MenuItem) error { return err } + if selectedItem == nil { + return nil + } + if err := selectedItem.OnPress(); err != nil { return err } diff --git a/pkg/gui/controllers/helpers/confirmation_helper.go b/pkg/gui/controllers/helpers/confirmation_helper.go index c721310b2..fe18dd638 100644 --- a/pkg/gui/controllers/helpers/confirmation_helper.go +++ b/pkg/gui/controllers/helpers/confirmation_helper.go @@ -302,7 +302,12 @@ func (self *ConfirmationHelper) resizeMenu() { _, _ = self.c.GocuiGui().SetView(self.c.Views().Menu.Name(), x0, y0, x1, menuBottom, 0) tooltipTop := menuBottom + 1 - tooltipHeight := getMessageHeight(true, self.c.Contexts().Menu.GetSelected().Tooltip, panelWidth) + 2 // plus 2 for the frame + tooltip := "" + selectedItem := self.c.Contexts().Menu.GetSelected() + if selectedItem != nil { + tooltip = selectedItem.Tooltip + } + tooltipHeight := getMessageHeight(true, tooltip, panelWidth) + 2 // plus 2 for the frame _, _ = self.c.GocuiGui().SetView(self.c.Views().Tooltip.Name(), x0, tooltipTop, x1, tooltipTop+tooltipHeight-1, 0) } diff --git a/pkg/gui/controllers/menu_controller.go b/pkg/gui/controllers/menu_controller.go index 3b04f01f2..c9ba2c701 100644 --- a/pkg/gui/controllers/menu_controller.go +++ b/pkg/gui/controllers/menu_controller.go @@ -53,7 +53,9 @@ func (self *MenuController) GetOnClick() func() error { func (self *MenuController) GetOnFocus() func(types.OnFocusOpts) error { return func(types.OnFocusOpts) error { selectedMenuItem := self.context().GetSelected() - self.c.Views().Tooltip.SetContent(selectedMenuItem.Tooltip) + if selectedMenuItem != nil { + self.c.Views().Tooltip.SetContent(selectedMenuItem.Tooltip) + } return nil } } From 932e01b41ac3203b43ec786b8d9c25334e3e1623 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Thu, 20 Jul 2023 21:08:56 +1000 Subject: [PATCH 2/2] Add test for crashing on empty menu --- pkg/integration/tests/test_list.go | 1 + pkg/integration/tests/ui/empty_menu.go | 31 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 pkg/integration/tests/ui/empty_menu.go diff --git a/pkg/integration/tests/test_list.go b/pkg/integration/tests/test_list.go index caaf2039e..5eab827b7 100644 --- a/pkg/integration/tests/test_list.go +++ b/pkg/integration/tests/test_list.go @@ -211,6 +211,7 @@ var tests = []*components.IntegrationTest{ tag.Reset, ui.Accordion, ui.DoublePopup, + ui.EmptyMenu, ui.SwitchTabFromMenu, undo.UndoCheckoutAndDrop, undo.UndoDrop, diff --git a/pkg/integration/tests/ui/empty_menu.go b/pkg/integration/tests/ui/empty_menu.go new file mode 100644 index 000000000..d6b3b42f0 --- /dev/null +++ b/pkg/integration/tests/ui/empty_menu.go @@ -0,0 +1,31 @@ +package ui + +import ( + "github.com/jesseduffield/lazygit/pkg/config" + . "github.com/jesseduffield/lazygit/pkg/integration/components" +) + +var EmptyMenu = NewIntegrationTest(NewIntegrationTestArgs{ + Description: "Verify that we don't crash on an empty menu", + ExtraCmdArgs: []string{}, + Skip: false, + SetupConfig: func(config *config.AppConfig) {}, + SetupRepo: func(shell *Shell) { + }, + Run: func(t *TestDriver, keys config.KeybindingConfig) { + t.Views().Files(). + IsFocused(). + Press(keys.Universal.OptionMenu) + + t.Views().Menu(). + IsFocused(). + // a string that filters everything out + FilterOrSearch("ljasldkjaslkdjalskdjalsdjaslkd"). + IsEmpty(). + Press(keys.Universal.Select) + + // back in the files view, selecting the non-existing menu item was a no-op + t.Views().Files(). + IsFocused() + }, +})