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

use textwrap to get description lines #128

Merged
merged 2 commits into from
May 17, 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
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