Skip to content

Commit

Permalink
Add option to pass in existing screen.
Browse files Browse the repository at this point in the history
Fixes #91
  • Loading branch information
snail-coupe authored and wong2 committed Nov 21, 2022
1 parent f38290c commit 14963d6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
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
- `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
19 changes: 16 additions & 3 deletions src/pick/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Picker(Generic[OPTION_T]):
min_selection_count: int = 0
selected_indexes: List[int] = field(init=False, default_factory=list)
index: int = field(init=False, default=0)
screen: Optional["curses._CursesWindow"] = None

def __post_init__(self) -> None:
if len(self.options) == 0:
Expand Down Expand Up @@ -110,7 +111,7 @@ def get_lines(self) -> Tuple[List, int]:
current_line = self.index + len(title_lines) + 1
return lines, current_line

def draw(self, screen) -> None:
def draw(self, screen: "curses._CursesWindow") -> None:
"""draw the curses ui on the screen, handle scroll if needed"""
screen.clear()

Expand All @@ -133,7 +134,9 @@ def draw(self, screen) -> None:

screen.refresh()

def run_loop(self, screen) -> Union[List[PICK_RETURN_T], PICK_RETURN_T]:
def run_loop(
self, screen: "curses._CursesWindow"
) -> Union[List[PICK_RETURN_T], PICK_RETURN_T]:
while True:
self.draw(screen)
c = screen.getch()
Expand Down Expand Up @@ -161,11 +164,19 @@ def config_curses(self) -> None:
# Curses failed to initialize color support, eg. when TERM=vt100
curses.initscr()

def _start(self, screen):
def _start(self, screen: "curses._CursesWindow"):
self.config_curses()
return self.run_loop(screen)

def start(self):
if self.screen:
# Given an existing screen
# don't make any lasting changes
last_cur = curses.curs_set(0)
ret = self.run_loop(self.screen)
if last_cur:
curses.curs_set(last_cur)
return ret
return curses.wrapper(self._start)


Expand All @@ -176,6 +187,7 @@ def pick(
default_index: int = 0,
multiselect: bool = False,
min_selection_count: int = 0,
screen: Optional["curses._CursesWindow"] = None,
):
picker: Picker = Picker(
options,
Expand All @@ -184,5 +196,6 @@ def pick(
default_index,
multiselect,
min_selection_count,
screen,
)
return picker.start()

0 comments on commit 14963d6

Please sign in to comment.