Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add excluded indexes. #105

Closed
wants to merge 12 commits into from
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ interactive selection list in the terminal.
multiple items by hitting SPACE
- `min_selection_count`: (optional) for multi select feature to
dictate a minimum of selected items before continuing
- `excluded_indexes`: (optional) indexes excluded from the selector. Excluded indexes will make it that they wont be able to get selected and just skip over them
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- `excluded_indexes`: (optional) indexes excluded from the selector. Excluded indexes will make it that they wont be able to get selected and just skip over them

- `screen`: (optional), if you are using `pick` within an existing curses application set this to your existing `screen` object. It is assumed this has initialised in the standard way (e.g. via `curses.wrapper()`, or `curses.noecho(); curses.cbreak(); screen.kepad(True)`)

## Community Projects
Expand Down
17 changes: 16 additions & 1 deletion src/pick/__init__.py
Copy link
Contributor

@iamalisalehi iamalisalehi Apr 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To implement @wong2 's suggestion, you can add an extra attribute to the class Option, something like this:

enable: bool = True

Then, if you implement my suggestions, everything should work as intended

Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Picker(Generic[OPTION_T]):
multiselect: bool = False
min_selection_count: int = 0
selected_indexes: List[int] = field(init=False, default_factory=list)
excluded_indexes: List[int] = field(default_factory=list)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
excluded_indexes: List[int] = field(default_factory=list)

index: int = field(init=False, default=0)
screen: Optional["curses._CursesWindow"] = None

Expand All @@ -48,14 +49,23 @@ def __post_init__(self) -> None:
)

self.index = self.default_index
while self.index in self.excluded_indexes:
self.index += 1
Comment on lines +52 to +53
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
while self.index in self.excluded_indexes:
self.index += 1



Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

def move_up(self) -> None:
self.index -= 1

while self.index in self.excluded_indexes:
self.index -= 1
if self.index < 0:
self.index = len(self.options) - 1
Comment on lines +58 to 62
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
while self.index in self.excluded_indexes:
self.index -= 1
if self.index < 0:
self.index = len(self.options) - 1
if self.index < 0:
self.index = len(self.options) - 1
if isinstance(self.options[self.index], Option) and not self.options[self.index].enable:
self.index -= 1


def move_down(self) -> None:
self.index += 1

while self.index in self.excluded_indexes:
self.index += 1
if self.index >= len(self.options):
self.index = 0
Comment on lines +66 to 70
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
while self.index in self.excluded_indexes:
self.index += 1
if self.index >= len(self.options):
self.index = 0
if self.index >= len(self.options):
self.index = 0
if isinstance(self.options[self.index], Option) and not self.options[self.index].enable:
self.index += 1


Expand Down Expand Up @@ -126,7 +136,7 @@ def draw(self, screen: "curses._CursesWindow") -> None:
if current_line > max_rows:
scroll_top = current_line - max_rows

lines_to_draw = lines[scroll_top : scroll_top + max_rows]
lines_to_draw = lines[scroll_top: scroll_top + max_rows]

for line in lines_to_draw:
screen.addnstr(y, x, line, max_x - 2)
Expand Down Expand Up @@ -187,15 +197,20 @@ def pick(
default_index: int = 0,
multiselect: bool = False,
min_selection_count: int = 0,
excluded_indexes = None,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
excluded_indexes = None,

screen: Optional["curses._CursesWindow"] = None,
):
if excluded_indexes is None:
excluded_indexes = []

Comment on lines +203 to +205
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if excluded_indexes is None:
excluded_indexes = []

picker: Picker = Picker(
options,
title,
indicator,
default_index,
multiselect,
min_selection_count,
excluded_indexes,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
excluded_indexes,

screen,
)
return picker.start()