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

fixed long title wrapping #129

Merged
merged 2 commits into from
May 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions src/pick/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ def get_selected(self) -> Union[List[PICK_RETURN_T], PICK_RETURN_T]:
else:
return self.options[self.index], self.index

def get_title_lines(self) -> List[str]:
def get_title_lines(self, *, max_width: int = 80) -> List[str]:
if self.title:
return self.title.split("\n") + [""]
return textwrap.fill(self.title, max_width - 2, drop_whitespace=False).split("\n") + [""]
return []

def get_option_lines(self) -> List[str]:
Expand All @@ -110,8 +110,8 @@ def get_option_lines(self) -> List[str]:

return lines

def get_lines(self) -> Tuple[List[str], int]:
title_lines = self.get_title_lines()
def get_lines(self, *, max_width: int = 80) -> Tuple[List[str], int]:
title_lines = self.get_title_lines(max_width=max_width)
option_lines = self.get_option_lines()
lines = title_lines + option_lines
current_line = self.index + len(title_lines) + 1
Expand All @@ -127,7 +127,7 @@ def draw(self, screen: "curses._CursesWindow") -> None:
max_y, max_x = screen.getmaxyx()
max_rows = max_y - y # the max rows we can draw

lines, current_line = self.get_lines()
lines, current_line = self.get_lines(max_width=max_x)

# calculate how many lines we should scroll, relative to the top
scroll_top = 0
Expand All @@ -138,12 +138,14 @@ def draw(self, screen: "curses._CursesWindow") -> None:

description_present = False
for option in self.options:
if not isinstance(option, Option) or option.description is not None:
if isinstance(option, Option) and option.description is not None:
description_present = True
break

for line in lines_to_draw:
if description_present:
title_length = len(self.get_title_lines(max_width=max_x))

for i, line in enumerate(lines_to_draw):
if description_present and i > title_length:
screen.addnstr(y, x, line, max_x // 2 - 2)
else:
screen.addnstr(y, x, line, max_x - 2)
Expand All @@ -154,7 +156,7 @@ def draw(self, screen: "curses._CursesWindow") -> None:
description_lines = textwrap.fill(option.description, max_x // 2 - 2).split('\n')

for i, line in enumerate(description_lines):
screen.addnstr(i + 3, max_x // 2, line, max_x - 2)
screen.addnstr(i + title_length, max_x // 2, line, max_x - 2)

screen.refresh()

Expand Down