Skip to content

Commit

Permalink
Excluded indexes (#132)
Browse files Browse the repository at this point in the history
Co-authored-by: J3ldo <[email protected]>
Co-authored-by: AN Long <[email protected]>
  • Loading branch information
3 people authored Jul 28, 2024
1 parent 88c9312 commit 88ba0f3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
12 changes: 12 additions & 0 deletions example/disabled.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from pick import pick, Option


title = "Please choose an option: "
options = [
Option("Option 1", description="All options are `enabled` by default."),
Option("Option 2", description="You can change that by changing the `enabled` attribute of the `Option` object to `False`."),
Option("Option 3", description="This option is disabled!", enabled=False),
Option("Option 4", description="Moving up and down, skips over the disabled options.")
]
option, index = pick(options, title, indicator="=>")
print(f"You chose {option} at index {index}")
29 changes: 23 additions & 6 deletions src/pick/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Option:
label: str
value: Any = None
description: Optional[str] = None
enabled: bool = True


KEYS_ENTER = (curses.KEY_ENTER, ord("\n"), ord("\r"))
Expand Down Expand Up @@ -54,17 +55,33 @@ def __post_init__(self) -> None:
"min_selection_count is bigger than the available options, you will not be able to make any selection"
)

if all(isinstance(option, Option) and not option.enabled for option in self.options):
raise ValueError(
"all given options are disabled, you must at least have one enabled option."
)

self.index = self.default_index
option = self.options[self.index]
if isinstance(option, Option) and not option.enabled:
self.move_down()

def move_up(self) -> None:
self.index -= 1
if self.index < 0:
self.index = len(self.options) - 1
while True:
self.index -= 1
if self.index < 0:
self.index = len(self.options) - 1
option = self.options[self.index]
if not isinstance(option, Option) or option.enabled:
break

def move_down(self) -> None:
self.index += 1
if self.index >= len(self.options):
self.index = 0
while True:
self.index += 1
if self.index >= len(self.options):
self.index = 0
option = self.options[self.index]
if not isinstance(option, Option) or option.enabled:
break

def mark_index(self) -> None:
if self.multiselect:
Expand Down
7 changes: 7 additions & 0 deletions tests/test_pick.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,10 @@ def test_option():
assert option[0].label == "option1"
assert option[0].value == 101
assert option[0].description == "description1"

def test_disabled_option():
options = [Option("option1"), Option("option2", enabled=False), Option("option3")]
picker = Picker(options)
assert picker.get_selected() == (Option("option1"), 0)
picker.move_down()
assert picker.get_selected() == (Option("option3"), 2)

0 comments on commit 88ba0f3

Please sign in to comment.