|
8 | 8 | from datetime import datetime |
9 | 9 | from datetime import timedelta |
10 | 10 | from datetime import tzinfo |
| 11 | +from tabulate import tabulate |
11 | 12 | from time import mktime |
12 | 13 | from typing import Iterable |
13 | 14 |
|
@@ -73,11 +74,13 @@ def format_database(self, database: TodoList) -> str: |
73 | 74 | class DefaultFormatter(Formatter): |
74 | 75 | def __init__( |
75 | 76 | self, |
| 77 | + tablefmt: str = "plain", |
76 | 78 | date_format: str = "%Y-%m-%d", |
77 | 79 | time_format: str = "%H:%M", |
78 | 80 | dt_separator: str = " ", |
79 | 81 | tz_override: tzinfo | None = None, |
80 | 82 | ) -> None: |
| 83 | + self.tablefmt = tablefmt |
81 | 84 | self.date_format = date_format |
82 | 85 | self.time_format = time_format |
83 | 86 | self.dt_separator = dt_separator |
@@ -126,24 +129,23 @@ def compact_multiple(self, todos: Iterable[Todo], hide_list: bool = False) -> st |
126 | 129 |
|
127 | 130 | recurring = "⟳" if todo.is_recurring else "" |
128 | 131 |
|
129 | | - if hide_list: |
130 | | - summary = f"{todo.summary} {percent}" |
131 | | - else: |
| 132 | + summary = todo.summary |
| 133 | + if not hide_list: |
132 | 134 | if not todo.list: |
133 | 135 | raise ValueError("Cannot format todo without a list") |
134 | 136 |
|
135 | | - summary = f"{todo.summary} {self.format_database(todo.list)}{percent}" |
136 | | - |
137 | | - # TODO: add spaces on the left based on max todos" |
| 137 | + summary = f"{summary} {self.format_database(todo.list)}" |
138 | 138 |
|
139 | | - # FIXME: double space when no priority |
140 | | - # split into parts to satisfy linter line too long |
141 | | - table.append( |
142 | | - f"[{completed}] {todo.id} {priority} {due} " |
143 | | - f"{recurring}{summary}{categories}" |
144 | | - ) |
| 139 | + table.append([ |
| 140 | + f"[{completed}]", |
| 141 | + todo.id, |
| 142 | + priority, |
| 143 | + f"{due}{recurring}", |
| 144 | + percent, |
| 145 | + f"{summary}{categories}" |
| 146 | + ]) |
145 | 147 |
|
146 | | - return "\n".join(table) |
| 148 | + return tabulate(table, tablefmt=self.tablefmt) |
147 | 149 |
|
148 | 150 | def _due_colour(self, todo: Todo) -> str: |
149 | 151 | now = self.now if isinstance(todo.due, datetime) else self.now.date() |
|
0 commit comments