Skip to content

Commit

Permalink
Fixed long title wrapping (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamalisalehi committed May 26, 2024
1 parent ddafd7c commit c933aa0
Showing 1 changed file with 11 additions and 9 deletions.
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

0 comments on commit c933aa0

Please sign in to comment.