Skip to content

Commit

Permalink
Reinstate tabulate, but make configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
elric1 committed Mar 4, 2024
1 parent 59f31f2 commit a03db8e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ dependencies = [
"parsedatetime",
"python-dateutil",
"pyxdg",
"tabulate",
"urwid",
]
dynamic = ["version"]
Expand Down
2 changes: 2 additions & 0 deletions todoman/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ def __init__(self):
@cached_property
def ui_formatter(self):
return formatters.DefaultFormatter(
self.config["tableformat"],
self.config["date_format"],
self.config["time_format"],
self.config["dt_separator"],
Expand All @@ -219,6 +220,7 @@ def ui_formatter(self):
@cached_property
def formatter(self):
return self.formatter_class(
self.config["tableformat"],
self.config["date_format"],
self.config["time_format"],
self.config["dt_separator"],
Expand Down
12 changes: 12 additions & 0 deletions todoman/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ def validate_cache_path(path: str) -> str:
return expand_path(path)


def validate_tableformat(tablefmt: str) -> str:
return tablefmt

def validate_date_format(fmt: str) -> str:
if any(x in fmt for x in ("%H", "%M", "%S", "%X")):
raise ConfigurationError(
Expand Down Expand Up @@ -74,6 +77,15 @@ class ConfigEntry(NamedTuple):
files) will be treated as a list.""",
expand_path,
),
ConfigEntry(
"tableformat",
str,
"plain",
"""
The name of a TableFormat a la tabulate which will be used to display
any tabular data.""",
validate_tableformat,
),
ConfigEntry(
"color",
str,
Expand Down
28 changes: 15 additions & 13 deletions todoman/formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from datetime import datetime
from datetime import timedelta
from datetime import tzinfo
from tabulate import tabulate
from time import mktime
from typing import Iterable

Expand Down Expand Up @@ -73,11 +74,13 @@ def format_database(self, database: TodoList) -> str:
class DefaultFormatter(Formatter):
def __init__(
self,
tablefmt: str = "plain",
date_format: str = "%Y-%m-%d",
time_format: str = "%H:%M",
dt_separator: str = " ",
tz_override: tzinfo | None = None,
) -> None:
self.tablefmt = tablefmt
self.date_format = date_format
self.time_format = time_format
self.dt_separator = dt_separator
Expand Down Expand Up @@ -126,24 +129,23 @@ def compact_multiple(self, todos: Iterable[Todo], hide_list: bool = False) -> st

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

if hide_list:
summary = f"{todo.summary} {percent}"
else:
summary = todo.summary
if not hide_list:
if not todo.list:
raise ValueError("Cannot format todo without a list")

summary = f"{todo.summary} {self.format_database(todo.list)}{percent}"

# TODO: add spaces on the left based on max todos"
summary = f"{summary} {self.format_database(todo.list)}"

# FIXME: double space when no priority
# split into parts to satisfy linter line too long
table.append(
f"[{completed}] {todo.id} {priority} {due} "
f"{recurring}{summary}{categories}"
)
table.append([
f"[{completed}]",
todo.id,
priority,
f"{due}{recurring}",
percent,
f"{summary}{categories}"
])

return "\n".join(table)
return tabulate(table, tablefmt=self.tablefmt)

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

0 comments on commit a03db8e

Please sign in to comment.