Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ def test_list(tmpdir: py.path.local, runner: CliRunner, create: Callable) -> Non
assert "harhar" in result.output


def test_categories(runner: CliRunner, create: Callable) -> None:
create(
"one.ics",
"SUMMARY:test\nCATEGORIES:work,home\n",
)
result = runner.invoke(cli, ["categories"])
assert result.exit_code == 0
assert "work" in result.output
assert "home" in result.output


def test_no_default_list(runner: CliRunner) -> None:
result = runner.invoke(cli, ["new", "Configure a default list"])

Expand Down
11 changes: 11 additions & 0 deletions todoman/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,17 @@ def lists(ctx: AppContext) -> None:
click.echo(text)


@cli.command()
@pass_ctx
@catch_errors
def categories(ctx: AppContext) -> None:
"""Returns all the categories"""
categories = ctx.db.categories()
porcelain = ctx.formatter_class == formatters.PorcelainFormatter
text = json.dumps(categories) if porcelain else "\n".join(categories)
click.echo(text)

Comment on lines +696 to +699

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DefaultFormatter and PorcelainFormatter should both implement a format_categories function which does the formatting, and here we just call click.echo(ctx.formatter.format_categories(categories)).

See how the list_command function is implemented below for reference.


@cli.command(name="list")
@pass_ctx
@click.argument("lists", nargs=-1, callback=_validate_lists_param)
Expand Down
14 changes: 14 additions & 0 deletions todoman/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,20 @@ def save(self, todo: Todo) -> None:
todo.id = self.cache.add_vtodo(vtodo, todo.path, todo.id)
self.cache.save_to_disk()

def categories(self) -> list[str]:
cursor = self.cache._conn.cursor()
try:
rows = cursor.execute(
"""
SELECT DISTINCT category
FROM categories
ORDER BY category
"""
).fetchall()
return [row[0] for row in rows]
finally:
cursor.close()


def _getmtime(path: str) -> int:
return os.stat(path).st_mtime_ns