work on dividing line

This commit is contained in:
2025-04-07 19:44:51 +03:00
parent ab1d335f8e
commit 0e308ce77f
3 changed files with 51 additions and 31 deletions
+19 -10
View File
@@ -1,15 +1,24 @@
from collections.abc import Sized
def check(string: str):
if len(string) != 1:
raise ValueError
class BaseDividingLine: class BaseDividingLine:
def __init__(self, unit_part: check): def __init__(self, unit_part: str = '-'):
self.unit_part = unit_part self.unit_part = unit_part
def get_unit_part(self):
if len(self.unit_part) == 0:
return ' '
else:
return self.unit_part[0]
class StaticDividingLine(BaseDividingLine):
def __init__(self, unit_part: str = '-', length: int = 25):
super().__init__(unit_part)
self.length = length
def get_full_line(self):
return f'\n[dim]{self.length * self.get_unit_part()}[/dim]\n'
class DynamicDividingLine(BaseDividingLine):
def get_full_line(self, length: int):
return f'\n[dim]{self.get_unit_part() * length}[/dim]\n'
BaseDividingLine('sygu')
+24 -14
View File
@@ -8,6 +8,7 @@ import re
from argenta.command.models import Command, InputCommand from argenta.command.models import Command, InputCommand
from argenta.router import Router from argenta.router import Router
from argenta.router.defaults import system_router from argenta.router.defaults import system_router
from argenta.app.dividing_line.models import StaticDividingLine, DynamicDividingLine
from argenta.command.exceptions import (UnprocessedInputFlagException, from argenta.command.exceptions import (UnprocessedInputFlagException,
RepeatedInputFlagsException, RepeatedInputFlagsException,
EmptyInputCommandException, EmptyInputCommandException,
@@ -29,7 +30,7 @@ class BaseApp:
exit_command_description: str = 'Exit command', exit_command_description: str = 'Exit command',
system_points_title: str = 'System points:', system_points_title: str = 'System points:',
ignore_command_register: bool = True, ignore_command_register: bool = True,
dividing_line: str = '-', dividing_line: StaticDividingLine | DynamicDividingLine = StaticDividingLine(),
repeat_command_groups: bool = True, repeat_command_groups: bool = True,
print_func: Callable[[str], None] = Console().print) -> None: print_func: Callable[[str], None] = Console().print) -> None:
self._prompt = prompt self._prompt = prompt
@@ -98,11 +99,15 @@ class BaseApp:
return False return False
elif handled_command_trigger == command.get_trigger(): elif handled_command_trigger == command.get_trigger():
return False return False
if isinstance(self._dividing_line, StaticDividingLine):
self._print_func(self._dividing_line.get_full_line())
self.unknown_command_handler(command)
self._print_func(self._dividing_line.get_full_line())
elif isinstance(self._dividing_line, DynamicDividingLine):
with redirect_stdout(io.StringIO()) as f: with redirect_stdout(io.StringIO()) as f:
self.unknown_command_handler(command) self.unknown_command_handler(command)
res: str = f.getvalue() res: str = f.getvalue()
self._print_framed_text(res) self._print_framed_text_with_dynamic_line(res)
return True return True
@@ -126,19 +131,13 @@ class BaseApp:
self.empty_input_command_handler() self.empty_input_command_handler()
@staticmethod def _print_framed_text_with_dynamic_line(self, text: str):
def _make_line_separator(size: int, one_part_of_sep: str):
return f'\n[dim]{one_part_of_sep * size}[/dim]\n'
def _print_framed_text(self, text: str):
clear_text = re.sub(r'\u001b\[[0-9;]*m', '', text) clear_text = re.sub(r'\u001b\[[0-9;]*m', '', text)
max_length_line = max([len(line) for line in clear_text.split('\n')]) max_length_line = max([len(line) for line in clear_text.split('\n')])
max_length_line = max_length_line if 10 <= max_length_line <= 80 else 80 if max_length_line > 80 else 10 max_length_line = max_length_line if 10 <= max_length_line <= 80 else 80 if max_length_line > 80 else 10
self._print_func(self._dividing_line.get_full_line(max_length_line))
self._print_func(self._make_line_separator(max_length_line, self._dividing_line))
print(text.strip('\n')) print(text.strip('\n'))
self._print_func(self._make_line_separator(max_length_line, self._dividing_line)) self._print_func(self._dividing_line.get_full_line(max_length_line))
@@ -165,10 +164,15 @@ class App(BaseApp):
try: try:
input_command: InputCommand = InputCommand.parse(raw_command=raw_command) input_command: InputCommand = InputCommand.parse(raw_command=raw_command)
except BaseInputCommandException as error: except BaseInputCommandException as error:
if isinstance(self._dividing_line, StaticDividingLine):
self._print_func(self._dividing_line.get_full_line())
self._error_handler(error, raw_command)
self._print_func(self._dividing_line.get_full_line())
elif isinstance(self._dividing_line, DynamicDividingLine):
with redirect_stdout(io.StringIO()) as f: with redirect_stdout(io.StringIO()) as f:
self._error_handler(error, raw_command) self._error_handler(error, raw_command)
res: str = f.getvalue() res: str = f.getvalue()
self._print_framed_text(res) self._print_framed_text_with_dynamic_line(res)
continue continue
if self._is_exit_command(input_command): if self._is_exit_command(input_command):
@@ -177,11 +181,17 @@ class App(BaseApp):
if self._is_unknown_command(input_command): if self._is_unknown_command(input_command):
continue continue
if isinstance(self._dividing_line, StaticDividingLine):
self._print_func(self._dividing_line.get_full_line())
for registered_router in self._registered_routers:
registered_router.input_command_handler(input_command)
self._print_func(self._dividing_line.get_full_line())
elif isinstance(self._dividing_line, DynamicDividingLine):
with redirect_stdout(io.StringIO()) as f: with redirect_stdout(io.StringIO()) as f:
for registered_router in self._registered_routers: for registered_router in self._registered_routers:
registered_router.input_command_handler(input_command) registered_router.input_command_handler(input_command)
res: str = f.getvalue() res: str = f.getvalue()
self._print_framed_text(res) self._print_framed_text_with_dynamic_line(res)
if not self._repeat_command_groups_description: if not self._repeat_command_groups_description:
self._print_func(self._prompt) self._print_func(self._prompt)
+2 -1
View File
@@ -2,9 +2,10 @@ from mock.mock_app.handlers.routers import work_router, settings_router
from argenta.app import App from argenta.app import App
from argenta.app.defaults import PredeterminedMessages from argenta.app.defaults import PredeterminedMessages
from argenta.app.dividing_line.models import StaticDividingLine, DynamicDividingLine
app: App = App() app: App = App(dividing_line=DynamicDividingLine())
def main(): def main():