Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@tui(help=, command=) parameters #27

Merged
merged 5 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,19 @@ pip install trogon

See also the `examples` folder for two example apps.

## Custom command name and custom help

By default the command added will be called `tui` and the help text for it will be `Open Textual TUI.`

You can customize one or both of these using the `help=` and `command=` parameters:

```python
@tui(command="ui", help="Open terminal UI")
@click.group(...)
def cli():
...
```

## Follow this project

If this app interests you, you may want to join the Textual [Discord server](https://discord.gg/Enf6Z3qhVr) where you can talk to Textual developers / community.
37 changes: 37 additions & 0 deletions tests/test_help.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import click
from click.testing import CliRunner
import re
from trogon import tui


@tui()
@click.group()
def default():
pass


@tui(command="custom")
@click.group()
def custom_command():
pass


@tui(help="Custom help")
@click.group()
def custom_help():
pass


def test_default_help():
result = CliRunner().invoke(default, ["--help"])
assert re.search(r"tui\s+Open Textual TUI", result.output) is not None


def test_custom_command():
result = CliRunner().invoke(custom_command, ["--help"])
assert re.search(r"custom\s+Open Textual TUI", result.output) is not None


def test_custom_help():
result = CliRunner().invoke(custom_help, ["--help"])
assert re.search(r"tui\s+Custom help", result.output) is not None
20 changes: 12 additions & 8 deletions trogon/trogon.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def __init__(
self,
cli: click.BaseCommand,
click_app_name: str,
command_name: str,
name: str | None = None,
id: str | None = None,
classes: str | None = None,
Expand All @@ -69,6 +70,7 @@ def __init__(
self.is_grouped_cli = isinstance(cli, click.Group)
self.command_schemas = introspect_click_app(cli)
self.click_app_name = click_app_name
self.command_name = command_name

try:
self.version = metadata.version(self.click_app_name)
Expand All @@ -78,7 +80,7 @@ def __init__(
self.highlighter = ReprHighlighter()

def compose(self) -> ComposeResult:
tree = CommandTree("Commands", self.command_schemas)
tree = CommandTree("Commands", self.command_schemas, self.command_name)

title_parts = [Text(self.click_app_name, style="b")]
if self.version:
Expand Down Expand Up @@ -216,8 +218,9 @@ class Trogon(App):
def __init__(
self,
cli: click.Group,
app_name: str = None,
click_context: click.Context = None,
app_name: str | None = None,
command_name: str = "tui",
click_context: click.Context | None = None,
) -> None:
super().__init__()
self.cli = cli
Expand All @@ -228,9 +231,10 @@ def __init__(
self.app_name = detect_run_string()
else:
self.app_name = app_name
self.command_name = command_name

def on_mount(self):
self.push_screen(CommandBuilder(self.cli, self.app_name))
self.push_screen(CommandBuilder(self.cli, self.app_name, self.command_name))

@on(Button.Pressed, "#home-exec-button")
def on_button_pressed(self):
Expand Down Expand Up @@ -285,18 +289,18 @@ def action_visit(self, url: str) -> None:
open_url(url)


def tui(name: str | None = None):
def tui(name: str | None = None, command: str = "tui", help: str = "Open Textual TUI."):
def decorator(app: click.Group | click.Command):
@click.pass_context
def wrapped_tui(ctx, *args, **kwargs):
Trogon(app, app_name=name, click_context=ctx).run()
Trogon(app, app_name=name, command_name=command, click_context=ctx).run()

if isinstance(app, click.Group):
app.command(name="tui", help="Open Textual TUI.")(wrapped_tui)
app.command(name=command, help=help)(wrapped_tui)
else:
new_group = click.Group()
new_group.add_command(app)
new_group.command(name="tui", help="Open Textual TUI.")(wrapped_tui)
new_group.command(name=command, help=help)(wrapped_tui)
return new_group

return app
Expand Down
5 changes: 3 additions & 2 deletions trogon/widgets/command_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
class CommandTree(Tree[CommandSchema]):
COMPONENT_CLASSES = {"group"}

def __init__(self, label: TextType, cli_metadata: dict[CommandName, CommandSchema]):
def __init__(self, label: TextType, cli_metadata: dict[CommandName, CommandSchema], command_name: str):
super().__init__(label)
self.show_root = False
self.guide_depth = 2
self.show_guides = False
self.cli_metadata = cli_metadata
self.command_name = command_name

def render_label(
self, node: TreeNode[TreeDataType], base_style: Style, style: Style
Expand All @@ -31,7 +32,7 @@ def build_tree(
) -> TreeNode:
data = {key: data[key] for key in sorted(data)}
for cmd_name, cmd_data in data.items():
if cmd_name == "tui":
if cmd_name == self.command_name:
continue
if cmd_data.subcommands:
label = Text(cmd_name)
Expand Down