-
Notifications
You must be signed in to change notification settings - Fork 85
Improve alignment in todo list output #576
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yzx9
wants to merge
2
commits into
pimutils:main
Choose a base branch
from
yzx9:aligned-output
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,8 @@ | |
from datetime import timezone | ||
from datetime import tzinfo | ||
from time import mktime | ||
from typing import Callable | ||
from typing import Literal | ||
|
||
import click | ||
import humanize | ||
|
@@ -36,13 +38,30 @@ def rgb_to_ansi(colour: str | None) -> str | None: | |
return f"\33[38;2;{int(r, 16)!s};{int(g, 16)!s};{int(b, 16)!s}m" | ||
|
||
|
||
class Column: | ||
format: Callable[[Todo], str] | ||
style: Callable[[Todo, str], str] | None | ||
align_direction: Literal["left", "right"] = "left" | ||
|
||
def __init__( | ||
self, | ||
format: Callable[[Todo], str], | ||
style: Callable[[Todo, str], str] | None = None, | ||
align_direction: Literal["left", "right"] = "left", | ||
) -> None: | ||
self.format = format | ||
self.style = style | ||
self.align_direction = align_direction | ||
|
||
|
||
class Formatter(ABC): | ||
@abstractmethod | ||
def __init__( | ||
self, | ||
date_format: str = "%Y-%m-%d", | ||
time_format: str = "%H:%M", | ||
dt_separator: str = " ", | ||
columns: bool = False, | ||
) -> None: | ||
"""Create a new formatter instance.""" | ||
|
||
|
@@ -56,7 +75,7 @@ def compact_multiple(self, todos: Iterable[Todo], hide_list: bool = False) -> st | |
|
||
@abstractmethod | ||
def simple_action(self, action: str, todo: Todo) -> str: | ||
"""Render an action related to a todo (e.g.: compelete, undo, etc).""" | ||
"""Render an action related to a todo (e.g.: complete, undo, etc).""" | ||
|
||
@abstractmethod | ||
def parse_priority(self, priority: str | None) -> int | None: | ||
|
@@ -97,6 +116,7 @@ def __init__( | |
date_format: str = "%Y-%m-%d", | ||
time_format: str = "%H:%M", | ||
dt_separator: str = " ", | ||
columns: bool = False, | ||
tz_override: tzinfo | None = None, | ||
) -> None: | ||
self.date_format = date_format | ||
|
@@ -105,6 +125,7 @@ def __init__( | |
self.datetime_format = dt_separator.join( | ||
filter(bool, (date_format, time_format)) | ||
) | ||
self.columns = columns | ||
|
||
self.tz = tz_override or tzlocal() | ||
self.now = datetime.now().replace(tzinfo=self.tz) | ||
|
@@ -123,48 +144,95 @@ def compact_multiple(self, todos: Iterable[Todo], hide_list: bool = False) -> st | |
# TODO: format lines fuidly and drop the table | ||
# it can end up being more readable when too many columns are empty. | ||
# show dates that are in the future in yellow (in 24hs) or grey (future) | ||
table = [] | ||
for todo in todos: | ||
completed = "X" if todo.is_completed else " " | ||
percent = todo.percent_complete or "" | ||
if percent: | ||
percent = f" ({percent}%)" | ||
|
||
if todo.categories: | ||
categories = " [" + ", ".join(todo.categories) + "]" | ||
else: | ||
categories = "" | ||
columns = { | ||
"completed": Column( | ||
format=lambda todo: "[X]" if todo.is_completed else "[ ]" | ||
), | ||
"id": Column(lambda todo: str(todo.id), align_direction="right"), | ||
"priority": Column( | ||
format=lambda todo: self.format_priority_compact(todo.priority), | ||
style=lambda todo, value: click.style(value, fg="magenta"), | ||
align_direction="right", | ||
), | ||
"due": Column( | ||
format=lambda todo: str( | ||
self.format_datetime(todo.due) or "(no due date)" | ||
), | ||
style=lambda todo, value: click.style(value, fg=c) | ||
if (c := self._due_colour(todo)) | ||
else value, | ||
), | ||
"report": Column(format=self.format_report), | ||
} | ||
|
||
priority = click.style( | ||
self.format_priority_compact(todo.priority), | ||
fg="magenta", | ||
) | ||
table = self.format_rows(columns, todos) | ||
if self.columns: | ||
table = self.columns_aligned_rows(columns, table) | ||
|
||
due = self.format_datetime(todo.due) or "(no due date)" | ||
due_colour = self._due_colour(todo) | ||
if due_colour: | ||
due = click.style(str(due), fg=due_colour) | ||
table = self.style_rows(columns, table) | ||
return "\n".join(table) | ||
|
||
recurring = "⟳" if todo.is_recurring else "" | ||
def format_rows( | ||
self, columns: dict[str, Column], todos: Iterable[Todo] | ||
) -> Iterable[tuple[Todo, list[str]]]: | ||
for todo in todos: | ||
yield (todo, [columns[col].format(todo) for col in columns]) | ||
|
||
if hide_list: | ||
summary = f"{todo.summary} {percent}" | ||
else: | ||
if not todo.list: | ||
raise ValueError("Cannot format todo without a list") | ||
def columns_aligned_rows( | ||
self, | ||
columns: dict[str, Column], | ||
rows: Iterable[tuple[Todo, list[str]]], | ||
) -> Iterable[tuple[Todo, list[str]]]: | ||
rows = list(rows) # materialize the iterator | ||
max_lengths = [0 for _ in columns] | ||
for _, cols in rows: | ||
for i, col in enumerate(cols): | ||
max_lengths[i] = max(max_lengths[i], len(col)) | ||
|
||
for todo, cols in rows: | ||
formatted = [] | ||
for i, (col, conf) in enumerate(zip(cols, columns.values())): | ||
if conf.align_direction == "right": | ||
formatted.append(col.rjust(max_lengths[i])) | ||
elif i < len(cols) - 1: | ||
formatted.append(col.ljust(max_lengths[i])) | ||
else: | ||
# if last column is left-aligned, don't add spaces | ||
formatted.append(col) | ||
|
||
yield todo, formatted | ||
|
||
def style_rows( | ||
self, | ||
columns: dict[str, Column], | ||
rows: Iterable[tuple[Todo, list[str]]], | ||
) -> Iterable[str]: | ||
for todo, cols in rows: | ||
yield " ".join( | ||
conf.style(todo, col) if conf.style else col | ||
for col, conf in zip(cols, columns.values()) | ||
) | ||
|
||
summary = f"{todo.summary} {self.format_database(todo.list)}{percent}" | ||
def format_report(self, todo: Todo, hide_list: bool = False) -> str: | ||
percent = todo.percent_complete or "" | ||
if percent: | ||
percent = f" ({percent}%)" | ||
|
||
# TODO: add spaces on the left based on max todos" | ||
categories = " [" + ", ".join(todo.categories) + "]" if todo.categories else "" | ||
|
||
# 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}" | ||
) | ||
recurring = "⟳" if todo.is_recurring else "" | ||
|
||
return "\n".join(table) | ||
if hide_list: | ||
summary = f"{todo.summary} {percent}" | ||
else: | ||
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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does this mean? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IIRC, I don't modify them. I think it's invalid now? https://github.com/pimutils/todoman/blob/main/todoman%2Fformatters.py#L158 |
||
return f"{recurring}{summary}{categories}" | ||
|
||
def _due_colour(self, todo: Todo) -> str: | ||
now = self.now if isinstance(todo.due, datetime) else self.now.date() | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.