Skip to content

Commit

Permalink
Merge branch 'ihabunek:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
danschwarz authored Jan 1, 2024
2 parents a67a9f7 + 84e7534 commit 93ddb4f
Show file tree
Hide file tree
Showing 19 changed files with 204 additions and 80 deletions.
34 changes: 22 additions & 12 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,30 @@ Changelog

<!-- Do not edit. This file is automatically generated from changelog.yaml.-->

**0.40.0 (TBA)**
**0.40.2 (2023-12-28)**

This release includes a major rewrite to use
[Click](https://click.palletsprojects.com/) for creating the command line
interface. This allows for some new features like nested commands, setting
parameters via environment variables, and shell completion. See docs for
details. Backward compatibility should be mostly preserved, except for cases
noted below please report any issues.
* Reinstate `toot post --using` option.
* Add shell completion for instances.

**0.40.1 (2023-12-28)**

* Add `toot --as` option to replace `toot post --using`. This now works for all
commands.

**0.40.0 (2023-12-27)**

This release includes a rather extensive change to use the Click library
(https://click.palletsprojects.com/) for creating the command line interface.
This allows for some new features like nested commands, setting parameters via
environment variables, and shell completion. Backward compatibility should be
mostly preserved, except for cases noted below. Please report any issues.

* BREAKING: Remove deprecated `--disable-https` option for `login` and
`login_cli`, pass the base URL instead
* BREAKING: Options `--debug`, `--color`, `--quiet` must be specified after
`toot` but before the command
* Enable passing params via environment variables, see:
* BREAKING: Options `--debug` and `--color` must be specified after `toot` but
before the command
* BREAKING: Option `--quiet` has been removed. Redirect output instead.
* Add passing parameters via environment variables, see:
https://toot.bezdomni.net/environment_variables.html
* Add shell completion, see: https://toot.bezdomni.net/shell_completion.html
* Add `tags info`, `tags featured`, `tags feature`, and `tags unfeature`
Expand All @@ -26,11 +36,11 @@ noted below please report any issues.
* Add `lists accounts`, `lists add`, `lists create`, `lists delete`, `lists
list`, `lists remove` subcommands, deprecate `lists`, `lists_accounts`,
`lists_add`, `lists_create`, `lists_delete`, `lists_remove` commands.
* Add `--json` option to tags commands
* Add `--json` option to lists commands
* Add `--json` option to tags and lists commands
* Add `toot --width` option for setting your prefered terminal width
* Add `--media-viewer` and `--colors` options to `toot tui`. These were
previously accessible only via settings.
* TUI: Fix issue where UI did not render until first input (thanks Urwid devs)

**0.39.0 (2023-11-23)**

Expand Down
33 changes: 23 additions & 10 deletions changelog.yaml
Original file line number Diff line number Diff line change
@@ -1,23 +1,36 @@
0.40.2:
date: 2023-12-28
changes:
- "Reinstate `toot post --using` option."
- "Add shell completion for instances."

0.40.1:
date: 2023-12-28
changes:
- "Add `toot --as` option to replace `toot post --using`. This now works for all commands."

0.40.0:
date: TBA
date: 2023-12-27
description: |
This release includes a major rewrite to use [Click](https://click.palletsprojects.com/) for
creating the command line interface. This allows for some new features like nested commands,
setting parameters via environment variables, and shell completion. See docs for details.
Backward compatibility should be mostly preserved, except for cases noted below please report
any issues.
This release includes a rather extensive change to use the Click library
(https://click.palletsprojects.com/) for creating the command line
interface. This allows for some new features like nested commands, setting
parameters via environment variables, and shell completion. Backward
compatibility should be mostly preserved, except for cases noted below.
Please report any issues.
changes:
- "BREAKING: Remove deprecated `--disable-https` option for `login` and `login_cli`, pass the base URL instead"
- "BREAKING: Options `--debug`, `--color`, `--quiet` must be specified after `toot` but before the command"
- "Enable passing params via environment variables, see: https://toot.bezdomni.net/environment_variables.html"
- "BREAKING: Options `--debug` and `--color` must be specified after `toot` but before the command"
- "BREAKING: Option `--quiet` has been removed. Redirect output instead."
- "Add passing parameters via environment variables, see: https://toot.bezdomni.net/environment_variables.html"
- "Add shell completion, see: https://toot.bezdomni.net/shell_completion.html"
- "Add `tags info`, `tags featured`, `tags feature`, and `tags unfeature` commands"
- "Add `tags followed`, `tags follow`, and `tags unfollow` sub-commands, deprecate `tags_followed`, `tags_follow`, and `tags tags_unfollow`"
- "Add `lists accounts`, `lists add`, `lists create`, `lists delete`, `lists list`, `lists remove` subcommands, deprecate `lists`, `lists_accounts`, `lists_add`, `lists_create`, `lists_delete`, `lists_remove` commands."
- "Add `--json` option to tags commands"
- "Add `--json` option to lists commands"
- "Add `--json` option to tags and lists commands"
- "Add `toot --width` option for setting your prefered terminal width"
- "Add `--media-viewer` and `--colors` options to `toot tui`. These were previously accessible only via settings."
- "TUI: Fix issue where UI did not render until first input (thanks Urwid devs)"

0.39.0:
date: 2023-11-23
Expand Down
34 changes: 22 additions & 12 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,30 @@ Changelog

<!-- Do not edit. This file is automatically generated from changelog.yaml.-->

**0.40.0 (TBA)**
**0.40.2 (2023-12-28)**

This release includes a major rewrite to use
[Click](https://click.palletsprojects.com/) for creating the command line
interface. This allows for some new features like nested commands, setting
parameters via environment variables, and shell completion. See docs for
details. Backward compatibility should be mostly preserved, except for cases
noted below please report any issues.
* Reinstate `toot post --using` option.
* Add shell completion for instances.

**0.40.1 (2023-12-28)**

* Add `toot --as` option to replace `toot post --using`. This now works for all
commands.

**0.40.0 (2023-12-27)**

This release includes a rather extensive change to use the Click library
(https://click.palletsprojects.com/) for creating the command line interface.
This allows for some new features like nested commands, setting parameters via
environment variables, and shell completion. Backward compatibility should be
mostly preserved, except for cases noted below. Please report any issues.

* BREAKING: Remove deprecated `--disable-https` option for `login` and
`login_cli`, pass the base URL instead
* BREAKING: Options `--debug`, `--color`, `--quiet` must be specified after
`toot` but before the command
* Enable passing params via environment variables, see:
* BREAKING: Options `--debug` and `--color` must be specified after `toot` but
before the command
* BREAKING: Option `--quiet` has been removed. Redirect output instead.
* Add passing parameters via environment variables, see:
https://toot.bezdomni.net/environment_variables.html
* Add shell completion, see: https://toot.bezdomni.net/shell_completion.html
* Add `tags info`, `tags featured`, `tags feature`, and `tags unfeature`
Expand All @@ -26,11 +36,11 @@ noted below please report any issues.
* Add `lists accounts`, `lists add`, `lists create`, `lists delete`, `lists
list`, `lists remove` subcommands, deprecate `lists`, `lists_accounts`,
`lists_add`, `lists_create`, `lists_delete`, `lists_remove` commands.
* Add `--json` option to tags commands
* Add `--json` option to lists commands
* Add `--json` option to tags and lists commands
* Add `toot --width` option for setting your prefered terminal width
* Add `--media-viewer` and `--colors` options to `toot tui`. These were
previously accessible only via settings.
* TUI: Fix issue where UI did not render until first input (thanks Urwid devs)

**0.39.0 (2023-11-23)**

Expand Down
6 changes: 6 additions & 0 deletions scripts/tag_version
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,19 @@ if dist_version != version:
sys.exit(1)

release_date = changelog_item["date"]
description = changelog_item.get("description")
changes = changelog_item["changes"]

if not isinstance(release_date, date):
print(f"Release date not set for version `{version}` in the changelog.", file=sys.stderr)
sys.exit(1)

commit_message = f"toot {version}\n\n"

if description:
lines = textwrap.wrap(description.strip(), 72)
commit_message += "\n".join(lines) + "\n\n"

for c in changes:
lines = textwrap.wrap(c, 70)
initial = True
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

setup(
name='toot',
version='0.40.0',
version='0.40.2',
description='Mastodon CLI client',
long_description=long_description.strip(),
author='Ivan Habunek',
Expand Down
2 changes: 1 addition & 1 deletion toot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from os.path import join, expanduser
from typing import NamedTuple

__version__ = '0.40.0'
__version__ = '0.40.2'


class App(NamedTuple):
Expand Down
4 changes: 4 additions & 0 deletions toot/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,10 @@ def get_instance(base_url: str) -> Response:
return http.anon_get(url)


def get_preferences(app, user) -> Response:
return http.get(app, user, '/api/v1/preferences')


def get_lists(app, user):
return http.get(app, user, "/api/v1/lists").json()

Expand Down
63 changes: 54 additions & 9 deletions toot/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
import sys
import typing as t

from click.shell_completion import CompletionItem
from click.testing import Result
from click.types import StringParamType
from functools import wraps

from toot import App, User, config, __version__
from toot.output import print_warning
from toot.settings import get_settings

if t.TYPE_CHECKING:
Expand Down Expand Up @@ -47,6 +51,16 @@ def get_default_map():
settings = get_settings()
common = settings.get("common", {})
commands = settings.get("commands", {})

# TODO: remove in version 1.0
tui_old = settings.get("tui", {})

if tui_old:
# TODO: don't show the warning for [toot.palette]
print_warning("Settings section [tui] has been deprecated in favour of [commands.tui].")
tui_new = commands.get("tui", {})
commands["tui"] = {**tui_old, **tui_new}

return {**common, **commands}


Expand All @@ -69,18 +83,44 @@ class Context(t.NamedTuple):
user: t.Optional[User] = None
color: bool = False
debug: bool = False
quiet: bool = False


class TootObj(t.NamedTuple):
"""Data to add to Click context"""
color: bool = True
debug: bool = False
quiet: bool = False
as_user: t.Optional[str] = None
# Pass a context for testing purposes
test_ctx: t.Optional[Context] = None


class AccountParamType(StringParamType):
"""Custom type to add shell completion for account names"""
name = "account"

def shell_complete(self, ctx, param, incomplete: str):
users = config.load_config()["users"].keys()
return [
CompletionItem(u)
for u in users
if u.lower().startswith(incomplete.lower())
]


class InstanceParamType(StringParamType):
"""Custom type to add shell completion for instance domains"""
name = "instance"

def shell_complete(self, ctx, param, incomplete: str):
apps = config.load_config()["apps"]

return [
CompletionItem(i)
for i in apps.keys()
if i.lower().startswith(incomplete.lower())
]


def pass_context(f: "t.Callable[te.Concatenate[Context, P], R]") -> "t.Callable[P, R]":
"""Pass the toot Context as first argument."""
@wraps(f)
Expand All @@ -98,11 +138,16 @@ def get_context() -> Context:
if obj.test_ctx:
return obj.test_ctx

user, app = config.get_active_user_app()
if not user or not app:
raise click.ClickException("This command requires you to be logged in.")
if obj.as_user:
user, app = config.get_user_app(obj.as_user)
if not user or not app:
raise click.ClickException(f"Account '{obj.as_user}' not found. Run `toot auth` to see available accounts.")
else:
user, app = config.get_active_user_app()
if not user or not app:
raise click.ClickException("This command requires you to be logged in.")

return Context(app, user, obj.color, obj.debug, obj.quiet)
return Context(app, user, obj.color, obj.debug)


json_option = click.option(
Expand All @@ -117,12 +162,12 @@ def get_context() -> Context:
@click.option("-w", "--max-width", type=int, default=80, help="Maximum width for content rendered by toot")
@click.option("--debug/--no-debug", default=False, help="Log debug info to stderr")
@click.option("--color/--no-color", default=sys.stdout.isatty(), help="Use ANSI color in output")
@click.option("--quiet/--no-quiet", default=False, help="Don't print anything to stdout")
@click.option("--as", "as_user", type=AccountParamType(), help="The account to use, overrides the active account.")
@click.version_option(__version__, message="%(prog)s v%(version)s")
@click.pass_context
def cli(ctx: click.Context, max_width: int, color: bool, debug: bool, quiet: bool):
def cli(ctx: click.Context, max_width: int, color: bool, debug: bool, as_user: str):
"""Toot is a Mastodon CLI"""
ctx.obj = TootObj(color, debug, quiet)
ctx.obj = TootObj(color, debug, as_user)
ctx.color = color
ctx.max_content_width = max_width

Expand Down
17 changes: 1 addition & 16 deletions toot/cli/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@
import platform
import sys
import webbrowser
from click.shell_completion import CompletionItem

from click.types import StringParamType

from toot import api, config, __version__
from toot.auth import get_or_create_app, login_auth_code, login_username_password
from toot.cli import cli
from toot.cli import AccountParamType, cli
from toot.cli.validators import validate_instance


Expand All @@ -22,18 +19,6 @@
)


class AccountParamType(StringParamType):
"""Custom type to add shell completion for account names"""

def shell_complete(self, ctx, param, incomplete: str):
accounts = config.load_config()["users"].keys()
return [
CompletionItem(a)
for a in accounts
if a.lower().startswith(incomplete.lower())
]


@cli.command()
def auth():
"""Show logged in accounts and instances"""
Expand Down
3 changes: 2 additions & 1 deletion toot/cli/lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
def lists(ctx: click.Context):
"""Display and manage lists"""
if ctx.invoked_subcommand is None:
print_warning("`toot lists` is deprecated in favour of `toot lists list`")
print_warning("`toot lists` is deprecated in favour of `toot lists list`.\n" +
"Run `toot lists -h` to see other list-related commands.")

user, app = config.get_active_user_app()
if not user or not app:
Expand Down
Loading

0 comments on commit 93ddb4f

Please sign in to comment.