1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-11-24 08:52:21 +02:00

Fix crash when filtering the keybindings menu

It would crash when some keybindings are set to null, and the filter string is
such that only those keybindings remain visible.

The reason for the crash is that when inserting non-model items (menu section
headers in this case) you specify a column to align them to. This works on the
assumption that the number of columns is always the same. It can cope with the
case that columns are removed because they are empty for all items; but it can't
cope with the case that the getDisplayStrings function returns a lower number of
columns.

And this is what happened here: MenuViewModel.GetDisplayStrings would omit the
keybinding column when none of the entries have a keybinding. This logic is
unnecessary, the generic list rendering mechanism takes care of this, so
removing this logic fixes the crash.

We do have to make sure though that the column is really empty when there's no
keybinding, so change the logic to use FgCyan only when there's a keybinding.
This commit is contained in:
Stefan Haller 2024-03-28 09:13:52 +01:00
parent 38876ba141
commit 0b70dfbf46
3 changed files with 37 additions and 7 deletions

View File

@ -74,9 +74,6 @@ func (self *MenuViewModel) SetMenuItems(items []*types.MenuItem, columnAlignment
// TODO: move into presentation package
func (self *MenuViewModel) GetDisplayStrings(_ int, _ int) [][]string {
menuItems := self.FilteredListViewModel.GetItems()
showKeys := lo.SomeBy(menuItems, func(item *types.MenuItem) bool {
return item.Key != nil
})
return lo.Map(menuItems, func(item *types.MenuItem, _ int) []string {
displayStrings := item.LabelColumns
@ -84,12 +81,12 @@ func (self *MenuViewModel) GetDisplayStrings(_ int, _ int) [][]string {
displayStrings[0] = style.FgDefault.SetStrikethrough().Sprint(displayStrings[0])
}
if !showKeys {
return displayStrings
keyLabel := ""
if item.Key != nil {
keyLabel = style.FgCyan.Sprint(keybindings.LabelFromKey(item.Key))
}
keyLabel := keybindings.LabelFromKey(item.Key)
displayStrings = utils.Prepend(displayStrings, style.FgCyan.Sprint(keyLabel))
displayStrings = utils.Prepend(displayStrings, keyLabel)
return displayStrings
})
}

View File

@ -0,0 +1,32 @@
package filter_and_search
import (
"github.com/jesseduffield/lazygit/pkg/config"
. "github.com/jesseduffield/lazygit/pkg/integration/components"
)
var FilterMenuWithNoKeybindings = NewIntegrationTest(NewIntegrationTestArgs{
Description: "Filtering the keybindings menu so that only entries without keybinding are left",
ExtraCmdArgs: []string{},
Skip: false,
SetupConfig: func(config *config.AppConfig) {
config.UserConfig.Keybinding.Universal.ToggleWhitespaceInDiffView = "<disabled>"
},
SetupRepo: func(shell *Shell) {
},
Run: func(t *TestDriver, keys config.KeybindingConfig) {
t.Views().Files().
IsFocused().
Press(keys.Universal.OptionMenu)
t.ExpectPopup().Menu().
Title(Equals("Keybindings")).
Filter("whitespace").
Lines(
// menu has filtered down to the one item that matches the
// filter, and it doesn't have a keybinding
Equals("--- Global ---"),
Equals("Toggle whitespace").IsSelected(),
)
},
})

View File

@ -146,6 +146,7 @@ var tests = []*components.IntegrationTest{
filter_and_search.FilterFuzzy,
filter_and_search.FilterMenu,
filter_and_search.FilterMenuCancelFilterWithEscape,
filter_and_search.FilterMenuWithNoKeybindings,
filter_and_search.FilterRemoteBranches,
filter_and_search.FilterRemotes,
filter_and_search.FilterSearchHistory,