From a7f1ce83e93e4be7b31b7f46d4bc2d7ed201beca Mon Sep 17 00:00:00 2001 From: Ivan Habunek Date: Tue, 5 Aug 2025 10:52:32 +0200 Subject: [PATCH] Add `toot polls show` --- toot/api.py | 4 ++++ toot/cli/__init__.py | 1 + toot/cli/polls.py | 28 ++++++++++++++++++++++++++++ toot/output.py | 5 +++++ 4 files changed, 38 insertions(+) create mode 100644 toot/cli/polls.py diff --git a/toot/api.py b/toot/api.py index 7ab55f0..9a34792 100644 --- a/toot/api.py +++ b/toot/api.py @@ -735,6 +735,10 @@ def get_lists(app, user): return http.get(app, user, "/api/v1/lists").json() +def get_poll(app, user, poll_id): + return http.get(app, user, f"/api/v1/polls/{poll_id}").json() + + def get_list_accounts(app, user, list_id): path = f"/api/v1/lists/{list_id}/accounts" return _get_response_list(app, user, path) diff --git a/toot/cli/__init__.py b/toot/cli/__init__.py index 43d52c0..27bb2ec 100644 --- a/toot/cli/__init__.py +++ b/toot/cli/__init__.py @@ -187,6 +187,7 @@ from toot.cli import accounts # noqa from toot.cli import auth # noqa from toot.cli import diag # noqa from toot.cli import lists # noqa +from toot.cli import polls # noqa from toot.cli import post # noqa from toot.cli import read # noqa from toot.cli import statuses # noqa diff --git a/toot/cli/polls.py b/toot/cli/polls.py new file mode 100644 index 0000000..bb29b95 --- /dev/null +++ b/toot/cli/polls.py @@ -0,0 +1,28 @@ +import json as pyjson + +import click + +from toot import api +from toot.cli import Context, cli, json_option, pass_context +from toot.entities import Poll, from_dict +from toot.output import print_poll + + +@cli.group() +def polls(): + """Show and vote on polls""" + pass + +@polls.command() +@click.argument("poll_id") +@json_option +@pass_context +def show(ctx: Context, poll_id: str, json: bool): + """Show a poll by ID""" + data = api.get_poll(ctx.app, ctx.user, poll_id) + + if json: + click.echo(pyjson.dumps(data)) + else: + poll = from_dict(Poll, data) + print_poll(poll) diff --git a/toot/output.py b/toot/output.py index 8d0a120..ba7988e 100644 --- a/toot/output.py +++ b/toot/output.py @@ -245,6 +245,11 @@ def html_lines(html: str, width: int) -> t.Generator[str, None, None]: first = False +def print_poll(poll: Poll): + for line in poll_lines(poll): + print(line) + + def poll_lines(poll: Poll) -> t.Generator[str, None, None]: for idx, option in enumerate(poll.options): perc = (