Skip to content

Commit d2a1e94

Browse files
authored
Merge pull request #27 from simonw/main
@tui(help=, command=) parameters
2 parents 2059c51 + 163d7e1 commit d2a1e94

File tree

4 files changed

+65
-10
lines changed

4 files changed

+65
-10
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,19 @@ pip install trogon
102102

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

105+
## Custom command name and custom help
106+
107+
By default the command added will be called `tui` and the help text for it will be `Open Textual TUI.`
108+
109+
You can customize one or both of these using the `help=` and `command=` parameters:
110+
111+
```python
112+
@tui(command="ui", help="Open terminal UI")
113+
@click.group(...)
114+
def cli():
115+
...
116+
```
117+
105118
## Follow this project
106119

107120
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.

tests/test_help.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import click
2+
from click.testing import CliRunner
3+
import re
4+
from trogon import tui
5+
6+
7+
@tui()
8+
@click.group()
9+
def default():
10+
pass
11+
12+
13+
@tui(command="custom")
14+
@click.group()
15+
def custom_command():
16+
pass
17+
18+
19+
@tui(help="Custom help")
20+
@click.group()
21+
def custom_help():
22+
pass
23+
24+
25+
def test_default_help():
26+
result = CliRunner().invoke(default, ["--help"])
27+
assert re.search(r"tui\s+Open Textual TUI", result.output) is not None
28+
29+
30+
def test_custom_command():
31+
result = CliRunner().invoke(custom_command, ["--help"])
32+
assert re.search(r"custom\s+Open Textual TUI", result.output) is not None
33+
34+
35+
def test_custom_help():
36+
result = CliRunner().invoke(custom_help, ["--help"])
37+
assert re.search(r"tui\s+Custom help", result.output) is not None

trogon/trogon.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def __init__(
5959
self,
6060
cli: click.BaseCommand,
6161
click_app_name: str,
62+
command_name: str,
6263
name: str | None = None,
6364
id: str | None = None,
6465
classes: str | None = None,
@@ -69,6 +70,7 @@ def __init__(
6970
self.is_grouped_cli = isinstance(cli, click.Group)
7071
self.command_schemas = introspect_click_app(cli)
7172
self.click_app_name = click_app_name
73+
self.command_name = command_name
7274

7375
try:
7476
self.version = metadata.version(self.click_app_name)
@@ -78,7 +80,7 @@ def __init__(
7880
self.highlighter = ReprHighlighter()
7981

8082
def compose(self) -> ComposeResult:
81-
tree = CommandTree("Commands", self.command_schemas)
83+
tree = CommandTree("Commands", self.command_schemas, self.command_name)
8284

8385
title_parts = [Text(self.click_app_name, style="b")]
8486
if self.version:
@@ -216,8 +218,9 @@ class Trogon(App):
216218
def __init__(
217219
self,
218220
cli: click.Group,
219-
app_name: str = None,
220-
click_context: click.Context = None,
221+
app_name: str | None = None,
222+
command_name: str = "tui",
223+
click_context: click.Context | None = None,
221224
) -> None:
222225
super().__init__()
223226
self.cli = cli
@@ -228,9 +231,10 @@ def __init__(
228231
self.app_name = detect_run_string()
229232
else:
230233
self.app_name = app_name
234+
self.command_name = command_name
231235

232236
def on_mount(self):
233-
self.push_screen(CommandBuilder(self.cli, self.app_name))
237+
self.push_screen(CommandBuilder(self.cli, self.app_name, self.command_name))
234238

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

287291

288-
def tui(name: str | None = None):
292+
def tui(name: str | None = None, command: str = "tui", help: str = "Open Textual TUI."):
289293
def decorator(app: click.Group | click.Command):
290294
@click.pass_context
291295
def wrapped_tui(ctx, *args, **kwargs):
292-
Trogon(app, app_name=name, click_context=ctx).run()
296+
Trogon(app, app_name=name, command_name=command, click_context=ctx).run()
293297

294298
if isinstance(app, click.Group):
295-
app.command(name="tui", help="Open Textual TUI.")(wrapped_tui)
299+
app.command(name=command, help=help)(wrapped_tui)
296300
else:
297301
new_group = click.Group()
298302
new_group.add_command(app)
299-
new_group.command(name="tui", help="Open Textual TUI.")(wrapped_tui)
303+
new_group.command(name=command, help=help)(wrapped_tui)
300304
return new_group
301305

302306
return app

trogon/widgets/command_tree.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111
class CommandTree(Tree[CommandSchema]):
1212
COMPONENT_CLASSES = {"group"}
1313

14-
def __init__(self, label: TextType, cli_metadata: dict[CommandName, CommandSchema]):
14+
def __init__(self, label: TextType, cli_metadata: dict[CommandName, CommandSchema], command_name: str):
1515
super().__init__(label)
1616
self.show_root = False
1717
self.guide_depth = 2
1818
self.show_guides = False
1919
self.cli_metadata = cli_metadata
20+
self.command_name = command_name
2021

2122
def render_label(
2223
self, node: TreeNode[TreeDataType], base_style: Style, style: Style
@@ -31,7 +32,7 @@ def build_tree(
3132
) -> TreeNode:
3233
data = {key: data[key] for key in sorted(data)}
3334
for cmd_name, cmd_data in data.items():
34-
if cmd_name == "tui":
35+
if cmd_name == self.command_name:
3536
continue
3637
if cmd_data.subcommands:
3738
label = Text(cmd_name)

0 commit comments

Comments
 (0)