Skip to content

Commit

Permalink
refactor: add Chunk.join to remove redundancy
Browse files Browse the repository at this point in the history
  • Loading branch information
mecaneer23 committed Mar 22, 2024
1 parent 22a3f2e commit 465c272
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
3 changes: 1 addition & 2 deletions src/class_todo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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]):
Expand Down
12 changes: 5 additions & 7 deletions src/print_todos.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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).
Expand Down
9 changes: 8 additions & 1 deletion src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 465c272

Please sign in to comment.