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

Conversation

simonw
Copy link
Contributor

@simonw simonw commented May 25, 2023

@simonw
Copy link
Contributor Author

simonw commented May 25, 2023

@darrenburns
Copy link
Member

Thanks @simonw! I won't be able to review this until next week now but as a note and reminder for myself - I think there's a hardcoded check somewhere that looks like command_name == "tui" -- I suspect that might need updating too to refer to the variable instead of the hardcoded "tui" string.

@simonw
Copy link
Contributor Author

simonw commented May 27, 2023

Good catch! Found that here:

if cmd_name == "tui":

@simonw
Copy link
Contributor Author

simonw commented May 30, 2023

I tried to fix that and it ended up getting a bit tangled. Here's a diff that does NOT solve the problem, but does illustrate how many different layers were involved:

diff --git a/trogon/trogon.py b/trogon/trogon.py
index bd33718..b872cd7 100644
--- a/trogon/trogon.py
+++ b/trogon/trogon.py
@@ -62,6 +62,7 @@ class CommandBuilder(Screen):
         name: str | None = None,
         id: str | None = None,
         classes: str | None = None,
+        command_name: str = "tui",
     ):
         super().__init__(name, id, classes)
         self.command_data = None
@@ -69,6 +70,7 @@ class CommandBuilder(Screen):
         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)
@@ -78,7 +80,9 @@ class CommandBuilder(Screen):
         self.highlighter = ReprHighlighter()
 
     def compose(self) -> ComposeResult:
-        tree = CommandTree("Commands", self.command_schemas)
+        tree = CommandTree(
+            "Commands", self.command_schemas, command_name=self.command_name
+        )
 
         title_parts = [Text(self.click_app_name, style="b")]
         if self.version:
@@ -218,6 +222,7 @@ class Trogon(App):
         cli: click.Group,
         app_name: str = None,
         click_context: click.Context = None,
+        command_name: str = "tui",
     ) -> None:
         super().__init__()
         self.cli = cli
@@ -228,9 +233,12 @@ class Trogon(App):
             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, command_name=self.command_name)
+        )
 
     @on(Button.Pressed, "#home-exec-button")
     def on_button_pressed(self):
diff --git a/trogon/widgets/command_tree.py b/trogon/widgets/command_tree.py
index ec05278..699b594 100644
--- a/trogon/widgets/command_tree.py
+++ b/trogon/widgets/command_tree.py
@@ -11,12 +11,18 @@ from trogon.introspect import CommandSchema, CommandName
 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 = "tui",
+    ):
         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
@@ -31,7 +37,7 @@ class CommandTree(Tree[CommandSchema]):
         ) -> 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)

I'm not sure what I missed here - the command with custom name still shows up in the tree view when I run the interactive TUI, when it should be being skipped.

@darrenburns
Copy link
Member

@simonw It looks like you just forgot to pass the value into Trogon.__init__, so self.command_name was always using the default value "tui". I've pushed a change to fix it now and will look to merge this this week.

Copy link
Member

@darrenburns darrenburns left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thank you!

@darrenburns darrenburns merged commit d2a1e94 into Textualize:main Jun 6, 2023
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants