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 anafvana committed Dec 24, 2022
1 parent 3c0df54 commit ed0cefe
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
- `indicator`: (optional) custom the selection indicator, defaults to `*`
- `default_index`: (optional) index of item where cursor starts at by default
- `handle_all`: (optional) define whether it is mandatory to assign all options to groups, defaults to `False`
- `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
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 = "groupick"
version = "1.0.0"
version = "1.1.0"
description = "Assign options to groups in the terminal with a simple GUI. Based on wong2's pick"
authors = ["Ana <[email protected]>", "wong2 <[email protected]>"]
license = "MIT"
Expand Down
17 changes: 14 additions & 3 deletions src/groupick/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Picker(Generic[OPTION_T]):
handle_all: bool = False
selected_indexes: Dict[str,List[int]] = field(init=False, default_factory=dict)
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 @@ -118,7 +119,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 @@ -141,7 +142,7 @@ def draw(self, screen) -> None:

screen.refresh()

def run_loop(self, screen) -> Optional[Dict[str, List[PICK_RETURN_T]]]:
def run_loop(self, screen: "curses._CursesWindow") -> Optional[Dict[str, List[PICK_RETURN_T]]]:
KEYS_ENTER = (curses.KEY_ENTER, ord("\n"), ord("\r"))
KEYS_UP = (curses.KEY_UP, ord("k"))
KEYS_DOWN = (curses.KEY_DOWN, ord("j"))
Expand Down Expand Up @@ -179,11 +180,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 @@ -194,6 +203,7 @@ def groupick(
indicator: str = "*",
default_index: int = 0,
handle_all: bool = False,
screen: Optional["curses._CursesWindow"] = None,
):
picker: Picker = Picker(
options,
Expand All @@ -202,5 +212,6 @@ def groupick(
indicator,
default_index,
handle_all,
screen,
)
return picker.start()

0 comments on commit ed0cefe

Please sign in to comment.