Skip to content

Commit

Permalink
Merge pull request #59 from petereon/bug_57
Browse files Browse the repository at this point in the history
fix: stepping through options works
  • Loading branch information
petereon authored Mar 29, 2023
2 parents 70af950 + 0a04270 commit 6f0679e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 10 deletions.
8 changes: 6 additions & 2 deletions beaupy/_beaupy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
]
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>']
license = 'MIT'
Expand Down
33 changes: 30 additions & 3 deletions test/beaupy_select_multiple_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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 == []


Expand Down
17 changes: 13 additions & 4 deletions test/beaupy_select_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"



Expand Down

0 comments on commit 6f0679e

Please sign in to comment.