From 825ff60f08b46fa1c515e742f0f3a6c689818422 Mon Sep 17 00:00:00 2001 From: Roland Thomas Date: Wed, 23 Jul 2025 00:05:09 -0400 Subject: [PATCH] Tweak TLDR visual formatting, Fix applying RichText to prompts --- examples/argument_examples.py | 1 + examples/confirm_example.py | 2 +- examples/user_input_demo.py | 2 +- falyx/action/confirm_action.py | 43 ++++++++++++++++++------- falyx/action/user_input_action.py | 4 +-- falyx/parser/command_argument_parser.py | 15 ++++++--- falyx/version.py | 2 +- pyproject.toml | 2 +- 8 files changed, 49 insertions(+), 22 deletions(-) diff --git a/examples/argument_examples.py b/examples/argument_examples.py index d2b4724..faf2ae7 100644 --- a/examples/argument_examples.py +++ b/examples/argument_examples.py @@ -88,6 +88,7 @@ flx.add_command( name="test_args", action=test_args, ), + style="bold blue", argument_config=default_config, ) diff --git a/examples/confirm_example.py b/examples/confirm_example.py index a491c3b..44a5696 100644 --- a/examples/confirm_example.py +++ b/examples/confirm_example.py @@ -70,7 +70,7 @@ async def build_chain(dogs: list[Dog]) -> ChainedAction: ), ConfirmAction( name="test_confirm", - message="Do you want to process the dogs?", + prompt_message="Do you want to process the dogs?", confirm_type="yes_no_cancel", return_last_result=True, inject_into="dogs", diff --git a/examples/user_input_demo.py b/examples/user_input_demo.py index 2137c98..39c249f 100644 --- a/examples/user_input_demo.py +++ b/examples/user_input_demo.py @@ -22,7 +22,7 @@ chain = ChainedAction( "Name", UserInputAction( name="User Input", - prompt_text="Enter your {last_result}: ", + prompt_message="Enter your {last_result}: ", validator=validate_alpha(), ), Action( diff --git a/falyx/action/confirm_action.py b/falyx/action/confirm_action.py index a33e5a6..228b5c2 100644 --- a/falyx/action/confirm_action.py +++ b/falyx/action/confirm_action.py @@ -115,7 +115,7 @@ class ConfirmAction(BaseAction): inject_into=inject_into, never_prompt=never_prompt, ) - self.prompt_message = rich_text_to_prompt_text(prompt_message) + self.prompt_message = prompt_message self.confirm_type = ConfirmType(confirm_type) self.prompt_session = prompt_session or PromptSession( interrupt_exception=CancelSignal @@ -128,15 +128,21 @@ class ConfirmAction(BaseAction): match self.confirm_type: case ConfirmType.YES_NO: return await confirm_async( - self.prompt_message, - prefix="❓ ", - suffix=" [Y/n] > ", + rich_text_to_prompt_text(self.prompt_message), + suffix=rich_text_to_prompt_text( + f" [[{OneColors.GREEN_b}]Y[/]]es, " + f"[[{OneColors.DARK_RED_b}]N[/]]o > " + ), session=self.prompt_session, ) case ConfirmType.YES_NO_CANCEL: error_message = "Enter 'Y', 'y' to confirm, 'N', 'n' to decline, or 'C', 'c' to abort." answer = await self.prompt_session.prompt_async( - f"❓ {self.prompt_message} [Y]es, [N]o, or [C]ancel to abort > ", + rich_text_to_prompt_text( + f"❓ {self.prompt_message} [[{OneColors.GREEN_b}]Y[/]]es, " + f"[[{OneColors.DARK_YELLOW_b}]N[/]]o, " + f"or [[{OneColors.DARK_RED_b}]C[/]]ancel to abort > " + ), validator=words_validator( ["Y", "N", "C"], error_message=error_message ), @@ -146,13 +152,19 @@ class ConfirmAction(BaseAction): return answer.upper() == "Y" case ConfirmType.TYPE_WORD: answer = await self.prompt_session.prompt_async( - f"❓ {self.prompt_message} [{self.word}] to confirm or [N/n] > ", + rich_text_to_prompt_text( + f"❓ {self.prompt_message} [[{OneColors.GREEN_b}]{self.word.upper()}[/]] " + f"to confirm or [[{OneColors.DARK_RED}]N[/{OneColors.DARK_RED}]] > " + ), validator=word_validator(self.word), ) return answer.upper().strip() != "N" case ConfirmType.TYPE_WORD_CANCEL: answer = await self.prompt_session.prompt_async( - f"❓ {self.prompt_message} [{self.word}] to confirm or [N/n] > ", + rich_text_to_prompt_text( + f"❓ {self.prompt_message} [[{OneColors.GREEN_b}]{self.word.upper()}[/]] " + f"to confirm or [[{OneColors.DARK_RED}]N[/{OneColors.DARK_RED}]] > " + ), validator=word_validator(self.word), ) if answer.upper().strip() == "N": @@ -160,9 +172,11 @@ class ConfirmAction(BaseAction): return answer.upper().strip() == self.word.upper().strip() case ConfirmType.YES_CANCEL: answer = await confirm_async( - self.prompt_message, - prefix="❓ ", - suffix=" [Y/n] > ", + rich_text_to_prompt_text(self.prompt_message), + suffix=rich_text_to_prompt_text( + f" [[{OneColors.GREEN_b}]Y[/]]es, " + f"[[{OneColors.DARK_RED_b}]N[/]]o > " + ), session=self.prompt_session, ) if not answer: @@ -171,7 +185,10 @@ class ConfirmAction(BaseAction): case ConfirmType.OK_CANCEL: error_message = "Enter 'O', 'o' to confirm or 'C', 'c' to abort." answer = await self.prompt_session.prompt_async( - f"❓ {self.prompt_message} [O]k to confirm, [C]ancel to abort > ", + rich_text_to_prompt_text( + f"❓ {self.prompt_message} [[{OneColors.GREEN_b}]O[/]]k to confirm, " + f"[[{OneColors.DARK_RED}]C[/]]ancel to abort > " + ), validator=words_validator(["O", "C"], error_message=error_message), ) if answer.upper() == "C": @@ -179,7 +196,9 @@ class ConfirmAction(BaseAction): return answer.upper() == "O" case ConfirmType.ACKNOWLEDGE: answer = await self.prompt_session.prompt_async( - f"❓ {self.prompt_message} [A]cknowledge > ", + rich_text_to_prompt_text( + f"❓ {self.prompt_message} [[{OneColors.CYAN_b}]A[/]]cknowledge > " + ), validator=word_validator("A"), ) return answer.upper().strip() == "A" diff --git a/falyx/action/user_input_action.py b/falyx/action/user_input_action.py index 7923f18..8f62b03 100644 --- a/falyx/action/user_input_action.py +++ b/falyx/action/user_input_action.py @@ -71,7 +71,7 @@ class UserInputAction(BaseAction): name=name, inject_last_result=inject_last_result, ) - self.prompt_message = rich_text_to_prompt_text(prompt_message) + self.prompt_message = prompt_message self.validator = validator self.prompt_session = prompt_session or PromptSession( interrupt_exception=CancelSignal @@ -97,7 +97,7 @@ class UserInputAction(BaseAction): prompt_message = prompt_message.format(last_result=self.last_result) answer = await self.prompt_session.prompt_async( - prompt_message, + rich_text_to_prompt_text(prompt_message), validator=self.validator, default=kwargs.get("default_text", self.default_text), ) diff --git a/falyx/parser/command_argument_parser.py b/falyx/parser/command_argument_parser.py index d78480f..18c548b 100644 --- a/falyx/parser/command_argument_parser.py +++ b/falyx/parser/command_argument_parser.py @@ -479,7 +479,10 @@ class CommandArgumentParser: if argument.positional: self._positional[argument.dest] = argument else: - self._keyword_list.append(argument) + if argument.action == ArgumentAction.TLDR: + self._keyword_list.insert(1, argument) + else: + self._keyword_list.append(argument) def add_argument( self, @@ -1302,8 +1305,11 @@ class CommandArgumentParser: program = self.program or "falyx" command = self.aliases[0] if self.aliases else self.command_key if is_cli_mode: - command = f"{program} run {command}" - command = f"[{self.command_style}]{command}[/{self.command_style}]" + command = ( + f"{program} run [{self.command_style}]{command}[/{self.command_style}]" + ) + else: + command = f"[{self.command_style}]{command}[/{self.command_style}]" usage = self.get_usage() self.console.print(f"[bold]usage:[/] {usage}\n") @@ -1315,8 +1321,9 @@ class CommandArgumentParser: for example in self._tldr_examples: usage = f"{command} {example.usage.strip()}" description = example.description.strip() - block = f"[bold]{usage}[/bold]\n{description}" + block = f"[bold]{usage}[/bold]" self.console.print(Padding(Panel(block, expand=False), (0, 2))) + self.console.print(f" {description}", style="dim") def __eq__(self, other: object) -> bool: if not isinstance(other, CommandArgumentParser): diff --git a/falyx/version.py b/falyx/version.py index a9221c5..da1fe8f 100644 --- a/falyx/version.py +++ b/falyx/version.py @@ -1 +1 @@ -__version__ = "0.1.69" +__version__ = "0.1.70" diff --git a/pyproject.toml b/pyproject.toml index 927c5d7..5aa7c2e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "falyx" -version = "0.1.69" +version = "0.1.70" description = "Reliable and introspectable async CLI action framework." authors = ["Roland Thomas Jr "] license = "MIT"