diff options
author | Diogo Sousa <montezdesousa@gmail.com> | 2024-04-29 19:21:59 +0100 |
---|---|---|
committer | Diogo Sousa <montezdesousa@gmail.com> | 2024-04-29 19:21:59 +0100 |
commit | c4c91fc017e067626077ebc6fcddb9b795c8e63c (patch) | |
tree | 407f1a98cdb81b20dca28abda38983b4df66cfda | |
parent | fed6c4dea12ed9f70a1ebf2376dfcc52d81eea37 (diff) |
fix: cmd text
4 files changed, 103 insertions, 90 deletions
diff --git a/openbb_terminal/openbb_terminal/config/console.py b/openbb_terminal/openbb_terminal/config/console.py index 749e40c0a3b..67e030d5f96 100644 --- a/openbb_terminal/openbb_terminal/config/console.py +++ b/openbb_terminal/openbb_terminal/config/console.py @@ -31,12 +31,8 @@ class Console: self.menu_text = "" self.menu_path = "" - def capture(self): - """Capture the console output.""" - return self._console.capture() - @staticmethod - def filter_rich_tags(text): + def _filter_rich_tags(text): """Filter out rich tags from text.""" for val in RICH_TAGS: text = text.replace(val, "") @@ -44,7 +40,7 @@ class Console: return text @staticmethod - def blend_text( + def _blend_text( message: str, color1: Tuple[int, int, int], color2: Tuple[int, int, int] ) -> Text: """Blend text from one color to another.""" @@ -85,7 +81,7 @@ class Console: else: self._console.print(kwargs["text"]) else: - print(self.filter_rich_tags(kwargs["text"])) # noqa: T201 + print(self._filter_rich_tags(kwargs["text"])) # noqa: T201 elif not self._settings.TEST_MODE: self._console.print(*args, **kwargs) else: diff --git a/openbb_terminal/openbb_terminal/config/menu_text.py b/openbb_terminal/openbb_terminal/config/menu_text.py index bd6234097d6..ba4931919bd 100644 --- a/openbb_terminal/openbb_terminal/config/menu_text.py +++ b/openbb_terminal/openbb_terminal/config/menu_text.py @@ -2,7 +2,7 @@ __docformat__ = "numpy" -from typing import Dict, List, Optional, Union +from typing import Dict, List import i18n from openbb import obb @@ -29,18 +29,18 @@ RICH_TAGS = [ USE_COLOR = True -def get_ordered_list_sources(command_path: str) -> List: - """Return the preferred source for the given command. +def get_ordered_providers(command_path: str) -> List: + """Return the preferred provider for the given command. Parameters ---------- command_path: str - The command to find the source for. E.g. "/equity/price/historical + The command to find the provider for. E.g. "/equity/price/historical Returns ------- List - The list of sources for the given command. + The list of providers for the given command. """ command_reference = obb.reference.get("paths", {}).get(command_path, {}) # type: ignore if command_reference: @@ -52,7 +52,12 @@ def get_ordered_list_sources(command_path: str) -> List: class MenuText: """Create menu text with rich colors to be displayed by terminal.""" - def __init__(self, path: str = "", column_sources: int = 100): + CMD_NAME_LENGTH = 18 + CMD_DESCRIPTION_LENGTH = 50 + CMD_PROVIDERS_LENGTH = 23 + SECTION_SPACING = 4 + + def __init__(self, path: str = ""): """Initialize menu help. Parameters @@ -64,7 +69,6 @@ class MenuText: """ self.menu_text = "" self.menu_path = path - self.col_src = column_sources self.warnings: List[Dict[str, str]] = [] def add_raw(self, raw_text: str): @@ -117,54 +121,54 @@ class MenuText: ) self.menu_text += f"[param]{parameter_translated}{space}:[/param] {value}\n" - def _adjust_command_length(self, key_command: str) -> str: + def _format_cmd_name(self, cmd_name: str) -> str: """Adjust the length of the command if it is too long. Parameters ---------- - key_command : str - command to be adjusted + cmd_name : str + command to be formatted Returns ------- str - adjusted command + formatted command """ - if len(key_command) > 18: - new_key_command = key_command[:18] # Default to trimming to 18 characters - - if "_" in key_command: - key_command_split = key_command.split("_") - - new_key_command = ( - "_".join(key_command_split[:2]) - if len(key_command_split) > 2 - else key_command_split[0] + if len(cmd_name) > self.CMD_NAME_LENGTH: + new_cmd_name = cmd_name[ + : self.CMD_NAME_LENGTH + ] # Default to trimming to 18 characters + + if "_" in cmd_name: + cmd_name_split = cmd_name.split("_") + + new_cmd_name = ( + "_".join(cmd_name_split[:2]) + if len(cmd_name_split) > 2 + else cmd_name_split[0] ) - if len(new_key_command) > 18: - new_key_command = new_key_command[:18] + if len(new_cmd_name) > self.CMD_NAME_LENGTH: + new_cmd_name = new_cmd_name[: self.CMD_NAME_LENGTH] - if new_key_command != key_command: + if new_cmd_name != cmd_name: self.warnings.append( { "warning": "Command name too long", - "command": key_command, - "trimmed_command": new_key_command, + "command": cmd_name, + "trimmed_command": new_cmd_name, } ) - key_command = new_key_command + cmd_name = new_cmd_name - return key_command + return cmd_name - def _handle_command_description( - self, key_command: str, command_description: str - ) -> str: + def _format_cmd_description(self, cmd_name: str, command_description: str) -> str: """Handle the command description. Parameters ---------- - key_command : str + cmd_name : str command to be adjusted command_description : str description of the command @@ -175,75 +179,88 @@ class MenuText: adjusted command description """ if not command_description: - command_description = i18n.t(self.menu_path + key_command) - if command_description == self.menu_path + key_command: + command_description = i18n.t(self.menu_path + cmd_name) + if command_description == self.menu_path + cmd_name: command_description = "" return ( - command_description[:88] + "..." - if len(command_description) > 91 + command_description[: self.CMD_DESCRIPTION_LENGTH - 3] + "..." + if len(command_description) > self.CMD_DESCRIPTION_LENGTH else command_description ) - def add_cmd( - self, key_command: str, condition: bool = True, command_description: str = "" - ): + def add_cmd(self, name: str, description: str = "", disable: bool = False): """Append command text (after translation from key) to a menu. Parameters ---------- - key_command : str + name : str key command to be executed by user. It is also used as a key to get description of command. - condition : bool - condition in which command is available to user. I.e. displays command and description. - If condition is false, the command line is greyed out. + description : str + description of the command + disable : bool + If disable is true, the command line is greyed out. """ - key_command = self._adjust_command_length(key_command) - command_description = self._handle_command_description( - key_command, command_description + formatted_name = self._format_cmd_name(name) + formatted_description = self._format_cmd_description( + formatted_name, description ) - spacing = (23 - (len(key_command) + 4)) * " " - - cmd = f"{key_command}{spacing}{command_description}" - cmd = f"[cmds] {cmd}[/cmds]" if condition else f"[unvl] {cmd}[/unvl]" - - sources = get_ordered_list_sources(f"{self.menu_path}{key_command}") - - if sources: - space = (self.col_src - len(cmd)) * " " if self.col_src > len(cmd) else " " - cmd += f"{space}[src][{', '.join(sources)}][/src]" + spacing = ( + max( + (self.CMD_NAME_LENGTH - len(formatted_name) - self.SECTION_SPACING), + self.SECTION_SPACING, + ) + * " " + ) + cmd = f"{formatted_name}{spacing}{formatted_description}" + cmd = f"[unvl] {cmd}[/unvl]" if disable else f"[cmds] {cmd}[/cmds]" + + providers = get_ordered_providers(f"{self.menu_path}{formatted_name}") + + if providers: + spacing = ( + max( + ( + self.CMD_DESCRIPTION_LENGTH + - len(formatted_description) + + self.SECTION_SPACING + ), + self.SECTION_SPACING, + ) + * " " + ) + cmd += rf"{spacing}[src]\[{', '.join(providers)}][/src]" self.menu_text += cmd + "\n" def add_menu( self, - key_menu: str, - condition: Optional[Union[bool, str]] = True, - menu_description: str = "", + name: str, + description: str = "", + disable: bool = False, ): """Append menu text (after translation from key) to a menu. Parameters ---------- - key_menu : str + name : str key menu to be executed by user. It is also used as a key to get description of menu. - condition : bool - condition in which menu is available to user. I.e. displays menu and description. - If condition is false, the menu line is greyed out. + disable : bool + If disable is true, the menu line is greyed out. """ - spacing = (23 - (len(key_menu) + 4)) * " " + spacing = (23 - (len(name) + 4)) * " " - if menu_description: - menu = f"{key_menu}{spacing}{menu_description}" + if description: + menu = f"{name}{spacing}{description}" else: - menu_description = i18n.t(self.menu_path + key_menu) - if menu_description == self.menu_path + key_menu: - menu_description = "" - menu = f"{key_menu}{spacing}{menu_description}" + description = i18n.t(self.menu_path + name) + if description == self.menu_path + name: + description = "" + menu = f"{name}{spacing}{description}" - if condition: - self.menu_text += f"[menu]> {menu}[/menu]\n" - else: + if disable: self.menu_text += f"[unvl]> {menu}[/unvl]\n" + else: + self.menu_text += f"[menu]> {menu}[/menu]\n" def add_setting(self, key_setting: str, status: bool = True): """Append menu text (after translation from key) to a menu. diff --git a/openbb_terminal/openbb_terminal/controllers/base_platform_controller.py b/openbb_terminal/openbb_terminal/controllers/base_platform_controller.py index ea4da7b66e4..1802010c1df 100644 --- a/openbb_terminal/openbb_terminal/controllers/base_platform_controller.py +++ b/openbb_terminal/openbb_terminal/controllers/base_platform_controller.py @@ -268,16 +268,16 @@ class PlatformController(BaseController): if self.CHOICES_MENUS: for menu in self.CHOICES_MENUS: - menu_description = self._get_menu_description(menu) - mt.add_menu(key_menu=menu, menu_description=menu_description) + description = self._get_menu_description(menu) + mt.add_menu(name=menu, description=description) if self.CHOICES_COMMANDS: mt.add_raw("\n") for command in self.CHOICES_COMMANDS: command_description = self._get_command_description(command) mt.add_cmd( - key_command=command.replace(f"{self._name}_", ""), - command_description=command_description, + name=command.replace(f"{self._name}_", ""), + description=command_description, ) session.console.print(text=mt.menu_text, menu=self._name) diff --git a/openbb_terminal/openbb_terminal/controllers/terminal_controller.py b/openbb_terminal/openbb_terminal/controllers/terminal_controller.py index 6726e4907a7..b1962183ba5 100644 --- a/openbb_terminal/openbb_terminal/controllers/terminal_controller.py +++ b/openbb_terminal/openbb_terminal/controllers/terminal_controller.py @@ -242,8 +242,8 @@ class TerminalController(BaseController): .get("description") ) or "" mt.add_menu( - key_menu=router, - menu_description=menu_description.split(".")[0].lower(), + name=router, + description=menu_description.split(".")[0].lower(), ) else: mt.add_cmd(router) @@ -260,8 +260,8 @@ class TerminalController(BaseController): .get("description") ) or "" mt.add_menu( - key_menu=router, - menu_description=menu_description.split(".")[0].lower(), + name=router, + description=menu_description.split(".")[0].lower(), ) else: mt.add_cmd(router) @@ -277,8 +277,8 @@ class TerminalController(BaseController): .get("description") ) or "" mt.add_menu( - key_menu=router, - menu_description=menu_description.split(".")[0].lower(), + name=router, + description=menu_description.split(".")[0].lower(), ) else: mt.add_cmd(router) |