feat(tui): add reordering to filter_multiselect
Tab switches focus to the selected-order panel; K/J shift the highlighted item up/down; Space/Enter removes it. The filter list dims while the order panel is active. Help line updates per focus mode.
This commit is contained in:
@@ -9,7 +9,7 @@ from __future__ import annotations
|
||||
|
||||
import unittest
|
||||
|
||||
from bot_bottle.cli.tui import _filter_items, filter_multiselect, filter_select
|
||||
from bot_bottle.cli.tui import _filter_items, _multiselect_loop, filter_multiselect, filter_select
|
||||
|
||||
|
||||
class TestFilterItems(unittest.TestCase):
|
||||
@@ -57,5 +57,97 @@ class TestFilterMultiselectEmptyItems(unittest.TestCase):
|
||||
self.assertIsNone(result)
|
||||
|
||||
|
||||
class TestMultiselectLoopReordering(unittest.TestCase):
|
||||
"""Exercise _multiselect_loop key handling without a real curses terminal.
|
||||
|
||||
We drive the loop via a fake screen that feeds a pre-recorded key sequence
|
||||
and records what was drawn — we only need the return value, so the fake
|
||||
screen's getch() raises StopIteration after the key list is exhausted, and
|
||||
the loop is expected to return before that via Ctrl-D.
|
||||
"""
|
||||
|
||||
def _run(self, keys: list[int], items: list[str], initial: list[str]) -> Optional[list[str]]:
|
||||
"""Run _multiselect_loop with a synthetic screen feeding `keys`."""
|
||||
import curses as _curses
|
||||
|
||||
key_iter = iter(keys)
|
||||
|
||||
class FakeScreen:
|
||||
def erase(self): pass
|
||||
def getmaxyx(self): return (40, 80)
|
||||
def refresh(self): pass
|
||||
def getch(self):
|
||||
return next(key_iter)
|
||||
def addstr(self, *a, **kw): pass
|
||||
def keypad(self, *a): pass
|
||||
|
||||
return _multiselect_loop(FakeScreen(), items, title="", initial=initial) # type: ignore[arg-type]
|
||||
|
||||
def test_ctrl_d_confirms_initial_selection(self):
|
||||
result = self._run([_KEY_CTRL_D], ["a", "b", "c"], ["a", "b"])
|
||||
self.assertEqual(["a", "b"], result)
|
||||
|
||||
def test_esc_cancels(self):
|
||||
result = self._run([_KEY_ESC], ["a", "b"], ["a"])
|
||||
self.assertIsNone(result)
|
||||
|
||||
def test_tab_then_K_moves_item_up(self):
|
||||
# Start: selected = ["a", "b", "c"]
|
||||
# Tab → order mode (order_cursor=0 on "a")
|
||||
# ↓ → order_cursor=1 (on "b")
|
||||
# K → swap b and a → ["b", "a", "c"], order_cursor=0
|
||||
# Ctrl-D → confirm
|
||||
DOWN = ord("j")
|
||||
result = self._run(
|
||||
[ord("\t"), DOWN, ord("K"), _KEY_CTRL_D],
|
||||
["a", "b", "c"],
|
||||
["a", "b", "c"],
|
||||
)
|
||||
self.assertEqual(["b", "a", "c"], result)
|
||||
|
||||
def test_tab_then_J_moves_item_down(self):
|
||||
# selected = ["a", "b", "c"], focus order, cursor=0
|
||||
# J → swap a and b → ["b", "a", "c"], cursor=1
|
||||
# Ctrl-D → confirm
|
||||
result = self._run(
|
||||
[ord("\t"), ord("J"), _KEY_CTRL_D],
|
||||
["a", "b", "c"],
|
||||
["a", "b", "c"],
|
||||
)
|
||||
self.assertEqual(["b", "a", "c"], result)
|
||||
|
||||
def test_K_at_top_is_no_op(self):
|
||||
# cursor already at 0, K should not change order
|
||||
result = self._run(
|
||||
[ord("\t"), ord("K"), _KEY_CTRL_D],
|
||||
["a", "b"],
|
||||
["a", "b"],
|
||||
)
|
||||
self.assertEqual(["a", "b"], result)
|
||||
|
||||
def test_J_at_bottom_is_no_op(self):
|
||||
DOWN = ord("j")
|
||||
result = self._run(
|
||||
[ord("\t"), DOWN, ord("J"), _KEY_CTRL_D],
|
||||
["a", "b"],
|
||||
["a", "b"],
|
||||
)
|
||||
self.assertEqual(["a", "b"], result)
|
||||
|
||||
def test_tab_back_to_filter_then_confirm(self):
|
||||
# Tab → order, Tab → filter, Ctrl-D confirms unchanged
|
||||
result = self._run(
|
||||
[ord("\t"), ord("\t"), _KEY_CTRL_D],
|
||||
["a", "b"],
|
||||
["a", "b"],
|
||||
)
|
||||
self.assertEqual(["a", "b"], result)
|
||||
|
||||
|
||||
from typing import Optional
|
||||
_KEY_ESC = 27
|
||||
_KEY_CTRL_D = 4
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user