Skip to content

Commit 163d7e1

Browse files
committed
Ensure custom command names dont appear in sidebar
1 parent c93fa7f commit 163d7e1

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

trogon/trogon.py

Lines changed: 9 additions & 5 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):
@@ -289,7 +293,7 @@ def tui(name: str | None = None, command: str = "tui", help: str = "Open Textual
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):
295299
app.command(name=command, help=help)(wrapped_tui)

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)