Skip to content

Commit a03db8e

Browse files
committed
Reinstate tabulate, but make configurable
1 parent 59f31f2 commit a03db8e

File tree

4 files changed

+30
-13
lines changed

4 files changed

+30
-13
lines changed

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ dependencies = [
3333
"parsedatetime",
3434
"python-dateutil",
3535
"pyxdg",
36+
"tabulate",
3637
"urwid",
3738
]
3839
dynamic = ["version"]

todoman/cli.py

+2
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ def __init__(self):
211211
@cached_property
212212
def ui_formatter(self):
213213
return formatters.DefaultFormatter(
214+
self.config["tableformat"],
214215
self.config["date_format"],
215216
self.config["time_format"],
216217
self.config["dt_separator"],
@@ -219,6 +220,7 @@ def ui_formatter(self):
219220
@cached_property
220221
def formatter(self):
221222
return self.formatter_class(
223+
self.config["tableformat"],
222224
self.config["date_format"],
223225
self.config["time_format"],
224226
self.config["dt_separator"],

todoman/configuration.py

+12
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ def validate_cache_path(path: str) -> str:
2424
return expand_path(path)
2525

2626

27+
def validate_tableformat(tablefmt: str) -> str:
28+
return tablefmt
29+
2730
def validate_date_format(fmt: str) -> str:
2831
if any(x in fmt for x in ("%H", "%M", "%S", "%X")):
2932
raise ConfigurationError(
@@ -74,6 +77,15 @@ class ConfigEntry(NamedTuple):
7477
files) will be treated as a list.""",
7578
expand_path,
7679
),
80+
ConfigEntry(
81+
"tableformat",
82+
str,
83+
"plain",
84+
"""
85+
The name of a TableFormat a la tabulate which will be used to display
86+
any tabular data.""",
87+
validate_tableformat,
88+
),
7789
ConfigEntry(
7890
"color",
7991
str,

todoman/formatters.py

+15-13
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from datetime import datetime
99
from datetime import timedelta
1010
from datetime import tzinfo
11+
from tabulate import tabulate
1112
from time import mktime
1213
from typing import Iterable
1314

@@ -73,11 +74,13 @@ def format_database(self, database: TodoList) -> str:
7374
class DefaultFormatter(Formatter):
7475
def __init__(
7576
self,
77+
tablefmt: str = "plain",
7678
date_format: str = "%Y-%m-%d",
7779
time_format: str = "%H:%M",
7880
dt_separator: str = " ",
7981
tz_override: tzinfo | None = None,
8082
) -> None:
83+
self.tablefmt = tablefmt
8184
self.date_format = date_format
8285
self.time_format = time_format
8386
self.dt_separator = dt_separator
@@ -126,24 +129,23 @@ def compact_multiple(self, todos: Iterable[Todo], hide_list: bool = False) -> st
126129

127130
recurring = "⟳" if todo.is_recurring else ""
128131

129-
if hide_list:
130-
summary = f"{todo.summary} {percent}"
131-
else:
132+
summary = todo.summary
133+
if not hide_list:
132134
if not todo.list:
133135
raise ValueError("Cannot format todo without a list")
134136

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)}"
138138

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+
])
145147

146-
return "\n".join(table)
148+
return tabulate(table, tablefmt=self.tablefmt)
147149

148150
def _due_colour(self, todo: Todo) -> str:
149151
now = self.now if isinstance(todo.due, datetime) else self.now.date()

0 commit comments

Comments
 (0)