diff --git a/example/position.py b/example/position.py new file mode 100644 index 0000000..c91d434 --- /dev/null +++ b/example/position.py @@ -0,0 +1,12 @@ +import pick + +title = "Please choose your favorite programming language: " +options = ["Java", "JavaScript", "Python", "PHP", "C++", "Erlang", "Haskell"] +option, index = pick.pick( + options, + title, + indicator="=>", + default_index=2, + position=pick.Position(1,4) +) +print(f"You choosed {option} at index {index}") diff --git a/src/pick/__init__.py b/src/pick/__init__.py index 435e850..01b48c6 100644 --- a/src/pick/__init__.py +++ b/src/pick/__init__.py @@ -1,4 +1,5 @@ import curses +from collections import namedtuple from dataclasses import dataclass, field from typing import Any, List, Optional, Sequence, Tuple, TypeVar, Union, Generic @@ -22,6 +23,8 @@ class Option: OPTION_T = TypeVar("OPTION_T", str, Option) PICK_RETURN_T = Tuple[OPTION_T, int] +Position = namedtuple('Position', ['x', 'y']) + @dataclass class Picker(Generic[OPTION_T]): @@ -34,7 +37,7 @@ class Picker(Generic[OPTION_T]): selected_indexes: List[int] = field(init=False, default_factory=list) index: int = field(init=False, default=0) screen: Optional["curses._CursesWindow"] = None - position: dict = field(default_factory={"y0": 1, "x0": 1}) + position: Position = field(default_factory=Position(1, 1)) def __post_init__(self) -> None: if len(self.options) == 0: @@ -114,7 +117,7 @@ def get_lines(self) -> Tuple[List, int]: def draw(self, screen: "curses._CursesWindow", position: dict) -> None: """draw the curses ui on the screen, handle scroll if needed""" - x, y = position["x0"], position["y0"] # start point + x, y = position[0], position[1] # start point max_y, max_x = screen.getmaxyx() max_rows = max_y - y # the max rows we can draw @@ -188,7 +191,7 @@ def pick( multiselect: bool = False, min_selection_count: int = 0, screen: Optional["curses._CursesWindow"] = None, - position: dict = {"y0": 1, "x0": 1} + position: Position = Position(1, 1) ): picker: Picker = Picker( options,