From 240caf2826dbc2a2dc91d24016725491a5ecb9d0 Mon Sep 17 00:00:00 2001 From: ali Date: Tue, 14 May 2024 18:18:39 +0330 Subject: [PATCH 1/2] use textwrap to get description lines --- src/pick/__init__.py | 24 +++--------------------- 1 file changed, 3 insertions(+), 21 deletions(-) diff --git a/src/pick/__init__.py b/src/pick/__init__.py index d730b1d..db27e3a 100644 --- a/src/pick/__init__.py +++ b/src/pick/__init__.py @@ -1,4 +1,5 @@ import curses +from textwrap import fill from collections import namedtuple from dataclasses import dataclass, field from typing import Any, Generic, List, Optional, Sequence, Tuple, TypeVar, Union @@ -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: @@ -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 = 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() From c52f55b4cd3ec5b2b32b56a9a18c2f482cd00dff Mon Sep 17 00:00:00 2001 From: Ali Salehi <49132329+iamalisalehi@users.noreply.github.com> Date: Fri, 17 May 2024 14:01:56 +0330 Subject: [PATCH 2/2] Update __init__.py change `fill` to `textwrap.fill` --- src/pick/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/pick/__init__.py b/src/pick/__init__.py index db27e3a..54b0415 100644 --- a/src/pick/__init__.py +++ b/src/pick/__init__.py @@ -1,5 +1,5 @@ import curses -from textwrap import fill +import textwrap from collections import namedtuple from dataclasses import dataclass, field from typing import Any, Generic, List, Optional, Sequence, Tuple, TypeVar, Union @@ -151,7 +151,7 @@ def draw(self, screen: "curses._CursesWindow") -> None: option = self.options[self.index] if isinstance(option, Option) and option.description is not None: - description_lines = fill(option.description, max_x // 2 - 2).split('\n') + 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)