1
0
mirror of https://github.com/ihabunek/toot synced 2024-12-23 23:52:40 +01:00

Add shell completion for account names

This commit is contained in:
Ivan Habunek 2023-12-07 19:41:30 +01:00
parent c7b5669c78
commit 0848a6f7df
No known key found for this signature in database
GPG Key ID: F5F0623FF5EBCB3D

View File

@ -2,6 +2,9 @@ import click
import platform import platform
import sys import sys
import webbrowser import webbrowser
from click.shell_completion import CompletionItem
from click.types import StringParamType
from toot import api, config, __version__ from toot import api, config, __version__
from toot.auth import get_or_create_app, login_auth_code, login_username_password from toot.auth import get_or_create_app, login_auth_code, login_username_password
@ -19,6 +22,18 @@ instance_option = click.option(
) )
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() @cli.command()
def auth(): def auth():
"""Show logged in accounts and instances""" """Show logged in accounts and instances"""
@ -101,7 +116,7 @@ def login(base_url: str):
@cli.command() @cli.command()
@click.argument("account", required=False) @click.argument("account", type=AccountParamType(), required=False)
def logout(account: str): def logout(account: str):
"""Log out of ACCOUNT, delete stored access keys""" """Log out of ACCOUNT, delete stored access keys"""
accounts = _get_accounts_list() accounts = _get_accounts_list()
@ -119,7 +134,7 @@ def logout(account: str):
@cli.command() @cli.command()
@click.argument("account", required=False) @click.argument("account", type=AccountParamType(), required=False)
def activate(account: str): def activate(account: str):
"""Switch to logged in ACCOUNT.""" """Switch to logged in ACCOUNT."""
accounts = _get_accounts_list() accounts = _get_accounts_list()