Skip to content

Commit

Permalink
Add position argument so pick doesn't clear the screen every time (
Browse files Browse the repository at this point in the history
…#95)

* added starting positions to print at

* added `Optional[]` to class def

* `field(default_factory=)` in class?

* fixed quote issues

* added option to README

* implemented changes however function will not run

* changed type hints of `dict` to `Position`, code still not functional

* minor grammatical correction @coderabbitai

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* arguemtn is now passed as

* Apply suggestions from code review

* resolved conflicts

* Update example/position.py

* Update README.md

---------

Co-authored-by: AN Long <[email protected]>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
  • Loading branch information
3 people committed Apr 22, 2024
1 parent a7ee138 commit c72f339
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ interactive selection list in the terminal.
- `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)`)
- `position`: (optional), if you are using `pick` within an existing curses application use this to set the first position to write to. e.g., `position=pick.Position(y=1, x=1)`

## Community Projects

Expand Down
24 changes: 24 additions & 0 deletions example/position.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import curses
import pick

def main(stdscr):
stdscr.addstr("hello world?\n")
stdscr.get_wch()

y, x = stdscr.getyx()

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,
screen=stdscr,
position=pick.Position(y=y, x=0) # comment this to demonstrate the issue it solves
)

stdscr.addstr(f"\nYou choosed {option} at index {index}\n")
stdscr.get_wch()

curses.wrapper(main)
15 changes: 10 additions & 5 deletions src/pick/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import curses
from collections import namedtuple
from dataclasses import dataclass, field
from typing import Any, Generic, List, Optional, Sequence, Tuple, TypeVar, Union

Expand All @@ -23,6 +24,8 @@ class Option:
OPTION_T = TypeVar("OPTION_T", str, Option)
PICK_RETURN_T = Tuple[OPTION_T, int]

Position = namedtuple('Position', ['y', 'x'])

@dataclass
class Picker(Generic[OPTION_T]):
options: Sequence[OPTION_T]
Expand All @@ -34,6 +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: Position = Position(0, 0)

def __post_init__(self) -> None:
if len(self.options) == 0:
Expand Down Expand Up @@ -132,9 +136,8 @@ def get_description_lines(self, description: str, length: int) -> List[str]:

def draw(self, screen: "curses._CursesWindow") -> None:
"""draw the curses ui on the screen, handle scroll if needed"""
screen.clear()
y, x = self.position # start point

x, y = 1, 1 # start point
max_y, max_x = screen.getmaxyx()
max_rows = max_y - y # the max rows we can draw

Expand Down Expand Up @@ -170,7 +173,7 @@ def draw(self, screen: "curses._CursesWindow") -> None:
screen.refresh()

def run_loop(
self, screen: "curses._CursesWindow"
self, screen: "curses._CursesWindow", position: Position
) -> Union[List[PICK_RETURN_T], PICK_RETURN_T]:
while True:
self.draw(screen)
Expand Down Expand Up @@ -201,14 +204,14 @@ def config_curses(self) -> None:

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

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)
ret = self.run_loop(self.screen, self.position)
if last_cur:
curses.curs_set(last_cur)
return ret
Expand All @@ -223,6 +226,7 @@ def pick(
multiselect: bool = False,
min_selection_count: int = 0,
screen: Optional["curses._CursesWindow"] = None,
position: Position = Position(0, 0)
):
picker: Picker = Picker(
options,
Expand All @@ -232,5 +236,6 @@ def pick(
multiselect,
min_selection_count,
screen,
position,
)
return picker.start()

0 comments on commit c72f339

Please sign in to comment.