From 0a042709afb5e51db90ddd88b4e7377fd2fd0ce6 Mon Sep 17 00:00:00 2001 From: Peter Vyboch Date: Wed, 29 Mar 2023 13:14:10 +0200 Subject: [PATCH] fix: stepping through options works --- beaupy/_beaupy.py | 8 +++++-- pyproject.toml | 2 +- test/beaupy_select_multiple_test.py | 33 ++++++++++++++++++++++++++--- test/beaupy_select_test.py | 17 +++++++++++---- 4 files changed, 50 insertions(+), 10 deletions(-) diff --git a/beaupy/_beaupy.py b/beaupy/_beaupy.py index e7f556e..88cf016 100755 --- a/beaupy/_beaupy.py +++ b/beaupy/_beaupy.py @@ -264,7 +264,11 @@ def select( '\n'.join( [ _format_option_select( - i=i, cursor_index=index % page_size, option=preprocessor(option), cursor_style=cursor_style, cursor=cursor + i=i, + cursor_index=index % page_size if pagination else index, + option=preprocessor(option), + cursor_style=cursor_style, + cursor=cursor, ) for i, option in enumerate(options[show_from:show_to] if pagination else options) ] @@ -364,7 +368,7 @@ def select_multiple( ticked=i + show_from in ticked_indices, tick_character=tick_character, tick_style=tick_style, - selected=i == index % page_size, + selected=i == (index % page_size if pagination else index), cursor_style=cursor_style, ) for i, option in enumerate(options[show_from:show_to] if pagination else options) diff --git a/pyproject.toml b/pyproject.toml index 79fb271..4add74d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = 'beaupy' -version = '3.5.1' +version = '3.5.2' description = 'A library of elements for interactive TUIs in Python' authors = ['Peter Vyboch '] license = 'MIT' diff --git a/test/beaupy_select_multiple_test.py b/test/beaupy_select_multiple_test.py index 325f2ce..f9471d2 100644 --- a/test/beaupy_select_multiple_test.py +++ b/test/beaupy_select_multiple_test.py @@ -78,9 +78,9 @@ def _(): assert res == [] -@test("`select_multiple` with 10 options pressing escape") +@test("`select_multiple` with 10 options stepping through them") def _(): - steps = iter([Keys.ESC]) + steps = iter([Keys.DOWN_ARROW, Keys.DOWN_ARROW, Keys.DOWN_ARROW, Keys.DOWN_ARROW, Keys.DOWN_ARROW, Keys.DOWN_ARROW ,Keys.DOWN_ARROW, Keys.DOWN_ARROW, Keys.DOWN_ARROW, Keys.ESC]) b.get_key = lambda: next(steps) Live.update = mock.MagicMock() @@ -89,8 +89,35 @@ def _(): mock.call( renderable="\\[ ] [pink1]test1[/pink1]\n\\[ ] test2\n\\[ ] test3\n\\[ ] test4\n\\[ ] test5\n\\[ ] test6\n\\[ ] test7\n\\[ ] test8\n\\[ ] test9\n\\[ ] test10\n\n(Mark with [bold]space[/bold], confirm with [bold]enter[/bold])" ), + mock.call( + renderable="\\[ ] test1\n\\[ ] [pink1]test2[/pink1]\n\\[ ] test3\n\\[ ] test4\n\\[ ] test5\n\\[ ] test6\n\\[ ] test7\n\\[ ] test8\n\\[ ] test9\n\\[ ] test10\n\n(Mark with [bold]space[/bold], confirm with [bold]enter[/bold])" + ), + mock.call( + renderable="\\[ ] test1\n\\[ ] test2\n\\[ ] [pink1]test3[/pink1]\n\\[ ] test4\n\\[ ] test5\n\\[ ] test6\n\\[ ] test7\n\\[ ] test8\n\\[ ] test9\n\\[ ] test10\n\n(Mark with [bold]space[/bold], confirm with [bold]enter[/bold])" + ), + mock.call( + renderable="\\[ ] test1\n\\[ ] test2\n\\[ ] test3\n\\[ ] [pink1]test4[/pink1]\n\\[ ] test5\n\\[ ] test6\n\\[ ] test7\n\\[ ] test8\n\\[ ] test9\n\\[ ] test10\n\n(Mark with [bold]space[/bold], confirm with [bold]enter[/bold])" + ), + mock.call( + renderable="\\[ ] test1\n\\[ ] test2\n\\[ ] test3\n\\[ ] test4\n\\[ ] [pink1]test5[/pink1]\n\\[ ] test6\n\\[ ] test7\n\\[ ] test8\n\\[ ] test9\n\\[ ] test10\n\n(Mark with [bold]space[/bold], confirm with [bold]enter[/bold])" + ), + mock.call( + renderable="\\[ ] test1\n\\[ ] test2\n\\[ ] test3\n\\[ ] test4\n\\[ ] test5\n\\[ ] [pink1]test6[/pink1]\n\\[ ] test7\n\\[ ] test8\n\\[ ] test9\n\\[ ] test10\n\n(Mark with [bold]space[/bold], confirm with [bold]enter[/bold])" + ), + mock.call( + renderable="\\[ ] test1\n\\[ ] test2\n\\[ ] test3\n\\[ ] test4\n\\[ ] test5\n\\[ ] test6\n\\[ ] [pink1]test7[/pink1]\n\\[ ] test8\n\\[ ] test9\n\\[ ] test10\n\n(Mark with [bold]space[/bold], confirm with [bold]enter[/bold])" + ), + mock.call( + renderable="\\[ ] test1\n\\[ ] test2\n\\[ ] test3\n\\[ ] test4\n\\[ ] test5\n\\[ ] test6\n\\[ ] test7\n\\[ ] [pink1]test8[/pink1]\n\\[ ] test9\n\\[ ] test10\n\n(Mark with [bold]space[/bold], confirm with [bold]enter[/bold])" + ), + mock.call( + renderable="\\[ ] test1\n\\[ ] test2\n\\[ ] test3\n\\[ ] test4\n\\[ ] test5\n\\[ ] test6\n\\[ ] test7\n\\[ ] test8\n\\[ ] [pink1]test9[/pink1]\n\\[ ] test10\n\n(Mark with [bold]space[/bold], confirm with [bold]enter[/bold])" + ), + mock.call( + renderable="\\[ ] test1\n\\[ ] test2\n\\[ ] test3\n\\[ ] test4\n\\[ ] test5\n\\[ ] test6\n\\[ ] test7\n\\[ ] test8\n\\[ ] test9\n\\[ ] [pink1]test10[/pink1]\n\n(Mark with [bold]space[/bold], confirm with [bold]enter[/bold])" + ), ] - assert Live.update.call_count == 1 + assert Live.update.call_count == 10 assert res == [] diff --git a/test/beaupy_select_test.py b/test/beaupy_select_test.py index a5e4766..0f03652 100644 --- a/test/beaupy_select_test.py +++ b/test/beaupy_select_test.py @@ -80,19 +80,28 @@ def _(): assert res == "test4" -@test("`select` with 10 options") +@test("`select` with 10 options, stepping through them") def _(): - steps = iter([Keys.ENTER]) + steps = iter([Keys.DOWN_ARROW, Keys.DOWN_ARROW, Keys.DOWN_ARROW, Keys.DOWN_ARROW, Keys.DOWN_ARROW, Keys.DOWN_ARROW ,Keys.DOWN_ARROW, Keys.DOWN_ARROW, Keys.DOWN_ARROW, Keys.ENTER]) b.get_key = lambda: next(steps) Live.update = mock.MagicMock() res = select(options=["test1", "test2", "test3", "test4", "test5", "test6", "test7", "test8", "test9", "test10"]) assert Live.update.call_args_list == [ mock.call(renderable="[pink1]>[/pink1] test1\n test2\n test3\n test4\n test5\n test6\n test7\n test8\n test9\n test10\n\n(Confirm with [bold]enter[/bold])"), + mock.call(renderable=" test1\n[pink1]>[/pink1] test2\n test3\n test4\n test5\n test6\n test7\n test8\n test9\n test10\n\n(Confirm with [bold]enter[/bold])"), + mock.call(renderable=" test1\n test2\n[pink1]>[/pink1] test3\n test4\n test5\n test6\n test7\n test8\n test9\n test10\n\n(Confirm with [bold]enter[/bold])"), + mock.call(renderable=" test1\n test2\n test3\n[pink1]>[/pink1] test4\n test5\n test6\n test7\n test8\n test9\n test10\n\n(Confirm with [bold]enter[/bold])"), + mock.call(renderable=" test1\n test2\n test3\n test4\n[pink1]>[/pink1] test5\n test6\n test7\n test8\n test9\n test10\n\n(Confirm with [bold]enter[/bold])"), + mock.call(renderable=" test1\n test2\n test3\n test4\n test5\n[pink1]>[/pink1] test6\n test7\n test8\n test9\n test10\n\n(Confirm with [bold]enter[/bold])"), + mock.call(renderable=" test1\n test2\n test3\n test4\n test5\n test6\n[pink1]>[/pink1] test7\n test8\n test9\n test10\n\n(Confirm with [bold]enter[/bold])"), + mock.call(renderable=" test1\n test2\n test3\n test4\n test5\n test6\n test7\n[pink1]>[/pink1] test8\n test9\n test10\n\n(Confirm with [bold]enter[/bold])"), + mock.call(renderable=" test1\n test2\n test3\n test4\n test5\n test6\n test7\n test8\n[pink1]>[/pink1] test9\n test10\n\n(Confirm with [bold]enter[/bold])"), + mock.call(renderable=" test1\n test2\n test3\n test4\n test5\n test6\n test7\n test8\n test9\n[pink1]>[/pink1] test10\n\n(Confirm with [bold]enter[/bold])"), ] - assert Live.update.call_count == 1 - assert res == "test1" + assert Live.update.call_count == 10 + assert res == "test10"