diff --git a/README.md b/README.md index abb7dfe..b164bb8 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,19 @@ ## Описание **Argenta** — Python library for creating custom shells +![prewiev](imgs/mock_app_preview.png) +Пример внешнего вида TUI, написанного с помощью Argenta +Рекомендуемая библиотека для форматирования текста - [rich](https://github.com/Textualize/rich) +```python +from argenta.app import App +from rich.console import Console + +app = App(print_func=Console().print) # default: builtins.print + +# code... +``` + + --- # Установка diff --git a/argenta/app/entity.py b/argenta/app/entity.py index 2e91c8f..9c5285e 100644 --- a/argenta/app/entity.py +++ b/argenta/app/entity.py @@ -6,8 +6,9 @@ from argenta.command.models import Command, InputCommand from argenta.router import Router from argenta.router.defaults import system_router from argenta.command.exceptions import (UnprocessedInputFlagException, - RepeatedInputFlagsException, - EmptyInputCommandException) + RepeatedInputFlagsException, + EmptyInputCommandException, + BaseInputCommandException) from argenta.app.exceptions import (InvalidRouterInstanceException, InvalidDescriptionMessagePatternException, NoRegisteredRoutersException, @@ -79,9 +80,7 @@ class App: try: input_command: InputCommand = InputCommand.parse(raw_command=raw_command) - except (UnprocessedInputFlagException, - RepeatedInputFlagsException, - EmptyInputCommandException) as error: + except BaseInputCommandException as error: self.print_func(self.line_separate) self._error_handler(error, raw_command) self.print_func(self.line_separate) @@ -240,17 +239,11 @@ class App: for command_handler in registered_router.get_command_handlers(): self.print_func(self._description_message_pattern.format( command=command_handler.get_handled_command().get_trigger(), - description=command_handler.get_handled_command().get_description() - ) - ) + description=command_handler.get_handled_command().get_description())) self.print_func(self.command_group_description_separate) - def _error_handler(self, - error: UnprocessedInputFlagException | - RepeatedInputFlagsException | - EmptyInputCommandException, - raw_command: str) -> None: + def _error_handler(self, error: BaseInputCommandException, raw_command: str) -> None: match error: case UnprocessedInputFlagException(): self._invalid_input_flags_handler(raw_command) diff --git a/argenta/command/exceptions.py b/argenta/command/exceptions.py index 3437c8d..faf7cf2 100644 --- a/argenta/command/exceptions.py +++ b/argenta/command/exceptions.py @@ -1,12 +1,16 @@ from argenta.command.flag.models import InputFlag, Flag -class UnprocessedInputFlagException(Exception): +class BaseInputCommandException(Exception): + pass + + +class UnprocessedInputFlagException(BaseInputCommandException): def __str__(self): return "Unprocessed Input Flags" -class RepeatedInputFlagsException(Exception): +class RepeatedInputFlagsException(BaseInputCommandException): def __init__(self, flag: Flag | InputFlag): self.flag = flag def __str__(self): @@ -14,6 +18,6 @@ class RepeatedInputFlagsException(Exception): f"Duplicate flag was detected in the input: '{self.flag.get_string_entity()}'") -class EmptyInputCommandException(Exception): +class EmptyInputCommandException(BaseInputCommandException): def __str__(self): - return "Input Command is empty" \ No newline at end of file + return "Input Command is empty" diff --git a/imgs/mock_app_preview.png b/imgs/mock_app_preview.png new file mode 100644 index 0000000..1be0ae7 Binary files /dev/null and b/imgs/mock_app_preview.png differ diff --git a/mock/mock_app/handlers/routers.py b/mock/mock_app/handlers/routers.py index eeb40c0..da57c98 100644 --- a/mock/mock_app/handlers/routers.py +++ b/mock/mock_app/handlers/routers.py @@ -2,13 +2,13 @@ from pprint import pprint from rich.console import Console from argenta.command import Command -from argenta.command.flag import Flags, InputFlags, Flag +from argenta.command.flag import Flags, InputFlags from argenta.command.flag.defaults import PredeterminedFlags from argenta.router import Router from .handlers_implementation.help_command import help_command -work_router: Router = Router(title='Work nts:') +work_router: Router = Router(title='Work points:') work_router.set_invalid_input_flag_handler(lambda flag: print(f'Invalid input flag: {flag.get_string_entity()} {flag.get_value() if flag.get_value() else ''}')) settings_router: Router = Router(title='Settings points:') @@ -22,14 +22,14 @@ def command_help(): help_command() -@work_router.command(Command(trigger='P', description='Start Solving', flags=Flags(PredeterminedFlags.HOST, PredeterminedFlags.PORT))) +@work_router.command(Command(trigger='S', description='Start Solving', flags=Flags(PredeterminedFlags.HOST, PredeterminedFlags.PORT))) def command_start_solving(args: InputFlags): print('Solving...') pprint(args.get_flag('host')) #start_solving_command() -@settings_router.command(Command(trigger='G', description='Update WordMath')) +@settings_router.command(Command(trigger='U', description='Update WordMath')) def command_update(): print('Command update') diff --git a/mock/mock_app/main.py b/mock/mock_app/main.py index c899a99..22d502b 100644 --- a/mock/mock_app/main.py +++ b/mock/mock_app/main.py @@ -9,7 +9,7 @@ app: App = App(prompt='[italic white bold]What do you want to do(enter number of line_separate=f'\n{"[bold green]-[/bold green][bold red]-[/bold red]"*25}\n', print_func=Console().print, command_group_description_separate='', - repeat_command_groups=True) + repeat_command_groups=False) def main(): @@ -24,13 +24,14 @@ def main(): app.set_initial_message(initial_greeting) app.set_farewell_message(goodbye_message) - app.add_message_on_startup('--help for get helping\n\n') + app.add_message_on_startup('[b dim]Usage[/b dim]: [i] <[green]flags[/green]>[/i]\n') + app.add_message_on_startup('[b dim]Help[/b dim]: [i][/i] [b red]--help[/b red]\n\n') app.set_invalid_input_flags_handler(lambda raw_command: print(f"Invalid input flags: {raw_command}")) app.set_unknown_command_handler(lambda command: print(f"Unknown command: {command.get_trigger()}")) app.set_repeated_input_flags_handler(lambda raw_command: print(f"Repeated input flags: {raw_command}")) - app.set_description_message_pattern('[bold red][{command}][/bold red] [blue]*=*=*[/blue] [bold yellow italic]{description}') + app.set_description_message_pattern('[bold red][{command}][/bold red] [blue dim]*=*=*[/blue dim] [bold yellow italic]{description}') app.start_polling()