Skip to content

Commit

Permalink
use textwrap to get description lines (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamalisalehi authored May 17, 2024
1 parent 8939610 commit ddafd7c
Showing 1 changed file with 3 additions and 21 deletions.
24 changes: 3 additions & 21 deletions src/pick/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import curses
import textwrap
from collections import namedtuple
from dataclasses import dataclass, field
from typing import Any, Generic, List, Optional, Sequence, Tuple, TypeVar, Union
Expand Down Expand Up @@ -116,25 +117,6 @@ def get_lines(self) -> Tuple[List[str], int]:
current_line = self.index + len(title_lines) + 1
return lines, current_line

def get_description_lines(self, description: str, length: int) -> List[str]:
description_words = description.split(" ")
description_lines: List[str] = []

line = ""
for i, word in enumerate(description_words):
if len(line + " " + word) <= length:
if i == 0:
line += word
else:
line += " " + word
else:
description_lines.append(line)
line = word

description_lines.append(line)

return description_lines

def draw(self, screen: "curses._CursesWindow") -> None:
"""draw the curses ui on the screen, handle scroll if needed"""
if self.clear_screen:
Expand Down Expand Up @@ -169,10 +151,10 @@ def draw(self, screen: "curses._CursesWindow") -> None:

option = self.options[self.index]
if isinstance(option, Option) and option.description is not None:
description_lines = self.get_description_lines(option.description, max_x // 2)
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, 2 * max_x // 2 - 2)
screen.addnstr(i + 3, max_x // 2, line, max_x - 2)

screen.refresh()

Expand Down

0 comments on commit ddafd7c

Please sign in to comment.