fix: empty command-palette filter no longer crashes the TUI render (issue #26)
This commit is contained in:
@@ -487,7 +487,7 @@ defmodule BDS.TUI do
|
|||||||
[
|
[
|
||||||
{%List{
|
{%List{
|
||||||
items: items,
|
items: items,
|
||||||
selected: state.selected,
|
selected: list_selected(items, state.selected),
|
||||||
highlight_style:
|
highlight_style:
|
||||||
if(focused?,
|
if(focused?,
|
||||||
do: %Style{fg: :black, bg: :cyan},
|
do: %Style{fg: :black, bg: :cyan},
|
||||||
@@ -498,6 +498,11 @@ defmodule BDS.TUI do
|
|||||||
]
|
]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# The List widget requires selected: nil when the collection is empty
|
||||||
|
# and raises otherwise — clamp every dynamic list through this.
|
||||||
|
defp list_selected([], _selected), do: nil
|
||||||
|
defp list_selected(items, selected), do: min(selected, length(items) - 1)
|
||||||
|
|
||||||
defp main_widgets(%{editor: nil} = state, rect) do
|
defp main_widgets(%{editor: nil} = state, rect) do
|
||||||
text =
|
text =
|
||||||
case selected_item(state) do
|
case selected_item(state) do
|
||||||
@@ -621,7 +626,7 @@ defmodule BDS.TUI do
|
|||||||
{%Clear{}, overlay},
|
{%Clear{}, overlay},
|
||||||
{%List{
|
{%List{
|
||||||
items: items,
|
items: items,
|
||||||
selected: if(command.help?, do: nil, else: command.selected),
|
selected: if(command.help?, do: nil, else: list_selected(items, command.selected)),
|
||||||
highlight_style: %Style{fg: :black, bg: :cyan},
|
highlight_style: %Style{fg: :black, bg: :cyan},
|
||||||
block: %Block{title: title, borders: [:all]}
|
block: %Block{title: title, borders: [:all]}
|
||||||
}, overlay}
|
}, overlay}
|
||||||
@@ -642,7 +647,7 @@ defmodule BDS.TUI do
|
|||||||
{%Clear{}, overlay},
|
{%Clear{}, overlay},
|
||||||
{%List{
|
{%List{
|
||||||
items: report_lines(report),
|
items: report_lines(report),
|
||||||
selected: report.scroll,
|
selected: list_selected(report_lines(report), report.scroll),
|
||||||
scroll_padding: 2,
|
scroll_padding: 2,
|
||||||
highlight_style: %Style{modifiers: [:bold]},
|
highlight_style: %Style{modifiers: [:bold]},
|
||||||
block: %Block{title: report_title(report), borders: [:all]}
|
block: %Block{title: report_title(report), borders: [:all]}
|
||||||
|
|||||||
@@ -230,6 +230,15 @@ defmodule BDS.TUITest do
|
|||||||
assert is_binary(action)
|
assert is_binary(action)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "input that matches no command renders without errors" do
|
||||||
|
state = mount_with_executor!() |> press(":") |> press("j")
|
||||||
|
|
||||||
|
# Filtering down to zero commands must not blow up the render
|
||||||
|
# (List panics on a selected index into an empty item list).
|
||||||
|
text = screen_text(state)
|
||||||
|
assert text =~ ":j"
|
||||||
|
end
|
||||||
|
|
||||||
test "no match reports an unknown command" do
|
test "no match reports an unknown command" do
|
||||||
state =
|
state =
|
||||||
mount_with_executor!()
|
mount_with_executor!()
|
||||||
|
|||||||
Reference in New Issue
Block a user