diff --git a/src/class_todo.py b/src/class_todo.py index 3f013c4..13210fb 100644 --- a/src/class_todo.py +++ b/src/class_todo.py @@ -181,7 +181,7 @@ def copy(self) -> "Todo": return Todo(repr(self)) def __repr__(self) -> str: - chunks: tuple[Chunk, ...] = ( + return Chunk.join( Chunk(True, self._indent_level * " "), Chunk( self._box_char != BoxChar.NONE and not self.is_empty(), @@ -195,7 +195,6 @@ def __repr__(self) -> str: ), Chunk(True, self._display_text), ) - return "".join([item for condition, item in chunks if condition]) class Todos(list[Todo]): diff --git a/src/print_todos.py b/src/print_todos.py index 24fdac1..4c4f8e8 100644 --- a/src/print_todos.py +++ b/src/print_todos.py @@ -138,21 +138,19 @@ def _get_display_string( todo = todos[position] if position in highlight and todo.is_empty(): return "⎯" * 8 - chunks: tuple[Chunk, ...] = ( + return Chunk.join( Chunk(True, todo.get_indent_level() * " "), Chunk(not todo.is_empty() and not SIMPLE_BOXES, todo.get_box()), Chunk(not todo.is_empty() and SIMPLE_BOXES, todo.get_simple_box()), Chunk( - not todo.has_box() and BULLETS, f"{_get_bullet(todo.get_indent_level())} " + not todo.has_box() and BULLETS, + f"{_get_bullet(todo.get_indent_level())} ", ), Chunk(ENUMERATE and not RELATIVE_ENUMERATE, f"{todos.index(todo) + 1}. "), Chunk(RELATIVE_ENUMERATE, f"{relative + 1}. "), Chunk(True, todo.get_display_text()), Chunk(width == 0, " "), - ) - return "".join([item for condition, item in chunks if condition])[ - : width - 1 - ].ljust(width - 1, " ") + )[: width - 1].ljust(width - 1, " ") def _is_within_strikethrough_range( @@ -209,7 +207,7 @@ def print_todos( stdscr: curses.window | None, todos: Todos, selected: Cursor, prev_start: int = 0 ) -> int: """ - Output list of Todo objects to a curses stdscr. + Output list of Todo objects to a curses stdscr or stdout. Returns a prev_start to be used in the next call to print_todos. (Interally calls make_printable_sublist with that value). diff --git a/src/utils.py b/src/utils.py index 067b06b..2b5790e 100644 --- a/src/utils.py +++ b/src/utils.py @@ -21,6 +21,11 @@ class Chunk(NamedTuple): condition: bool text: str + @staticmethod + def join(*chunks: "Chunk") -> str: + """Join chunks into one string""" + return "".join([item for condition, item in chunks if condition]) + class Color(Enum): """ @@ -77,7 +82,9 @@ def set_header(stdscr: curses.window, message: str) -> None: """ Set the header to a specific message. """ - stdscr.addstr(0, 0, message.ljust(stdscr.getmaxyx()[1]), curses.A_BOLD | curses.color_pair(2)) + stdscr.addstr( + 0, 0, message.ljust(stdscr.getmaxyx()[1]), curses.A_BOLD | curses.color_pair(2) + ) def overflow(counter: int, minimum: int, maximum: int) -> int: