Skip to content

Commit

Permalink
Fix crash when filtering the keybindings menu
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
stefanhaller committed Mar 28, 2024
1 parent 38876ba commit bd91fe0
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions pkg/gui/context/menu_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,19 @@ 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
if item.DisabledReason != nil {
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
})
}
Expand Down

0 comments on commit bd91fe0

Please sign in to comment.