diff --git a/changelog.yaml b/changelog.yaml index 0816d17..5713612 100644 --- a/changelog.yaml +++ b/changelog.yaml @@ -5,6 +5,7 @@ - "Migrate to `click` for commandline arguments. BC should be mostly preserved, please report any issues." - "Add shell completion, see: https://toot.bezdomni.net/shell_completion.html" - "Add `--json` option to tag commands" + - "Add `tags info`, `tags featured`, `tags feature`, and `tags unfeature` commands" 0.39.0: date: 2023-11-23 diff --git a/toot/api.py b/toot/api.py index 4775aa3..1a7d3e5 100644 --- a/toot/api.py +++ b/toot/api.py @@ -543,6 +543,20 @@ def unfeature_tag(app, user, tag_id: str) -> Response: return http.delete(app, user, f"/api/v1/featured_tags/{tag_id}") +def find_tag(app, user, tag) -> Optional[dict]: + """Find a hashtag by tag name or ID""" + tag = tag.lstrip("#") + results = search(app, user, tag, type="hashtags").json() + + return next( + ( + t for t in results["hashtags"] + if t["name"].lower() == tag.lstrip("#").lower() or t["id"] == tag + ), + None + ) + + def find_featured_tag(app, user, tag) -> Optional[dict]: """Find a featured tag by tag name or ID""" return next( diff --git a/toot/cli/tags.py b/toot/cli/tags.py index 1621d66..2e8d40a 100644 --- a/toot/cli/tags.py +++ b/toot/cli/tags.py @@ -3,6 +3,7 @@ import json as pyjson from toot import api from toot.cli.base import cli, pass_context, json_option, Context +from toot.entities import Tag, from_dict from toot.output import print_tag_list, print_warning @@ -11,6 +12,29 @@ def tags(): """List, follow, and unfollow tags""" +@tags.command() +@click.argument("tag") +@json_option +@pass_context +def info(ctx: Context, tag, json: bool): + """Show a hashtag and its associated information""" + tag = api.find_tag(ctx.app, ctx.user, tag) + + if not tag: + raise click.ClickException("Tag not found") + + if json: + click.echo(pyjson.dumps(tag)) + else: + tag = from_dict(Tag, tag) + click.secho(f"#{tag.name}", fg="yellow") + click.secho(tag.url, italic=True) + if tag.following: + click.echo("Followed") + else: + click.echo("Not followed") + + @tags.command() @json_option @pass_context