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
1 changed files with 17 additions and 2 deletions

View File

@ -2,6 +2,9 @@ import click
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
@ -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()
def auth():
"""Show logged in accounts and instances"""
@ -101,7 +116,7 @@ def login(base_url: str):
@cli.command()
@click.argument("account", required=False)
@click.argument("account", type=AccountParamType(), required=False)
def logout(account: str):
"""Log out of ACCOUNT, delete stored access keys"""
accounts = _get_accounts_list()
@ -119,7 +134,7 @@ def logout(account: str):
@cli.command()
@click.argument("account", required=False)
@click.argument("account", type=AccountParamType(), required=False)
def activate(account: str):
"""Switch to logged in ACCOUNT."""
accounts = _get_accounts_list()