1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
from typing import TYPE_CHECKING, Any, Dict, Optional, Tuple
from rich import panel
from rich.console import (
Console as RichConsole,
Theme,
)
from rich.text import Text
from openbb_cli.config.menu_text import RICH_TAGS
if TYPE_CHECKING:
from openbb_cli.models.settings import Settings
class Console:
"""Create a rich console to wrap the console print with a Panel."""
def __init__(
self,
settings: "Settings",
style: Optional[Dict[str, Any]] = None,
):
"""Initialize the ConsoleAndPanel class."""
self._console = RichConsole(
theme=Theme(style),
highlight=False,
soft_wrap=True,
)
self._settings = settings
self.menu_text = ""
self.menu_path = ""
def capture(self):
"""Capture the console output."""
return self._console.capture()
@staticmethod
def filter_rich_tags(text):
"""Filter out rich tags from text."""
for val in RICH_TAGS:
text = text.replace(val, "")
return text
@staticmethod
def blend_text(
message: str, color1: Tuple[int, int, int], color2: Tuple[int, int, int]
) -> Text:
"""Blend text from one color to another."""
text = Text(message)
r1, g1, b1 = color1
r2, g2, b2 = color2
dr = r2 - r1
dg = g2 - g1
db = b2 - b1
size = len(text) + 5
for index in range(size):
blend = index / size
color = f"#{int(r1 + dr * blend):02X}{int(g1 + dg * blend):02X}{int(b1 + db * blend):02X}"
text.stylize(color, index, index + 1)
return text
def print(self, *args, **kwargs):
"""Print the text to the console."""
if kwargs and "text" in list(kwargs) and "menu" in list(kwargs):
if not self._settings.TEST_MODE:
if self._settings.ENABLE_RICH_PANEL:
if self._settings.SHOW_VERSION:
version = self._settings.VERSION
version = f"[param]OpenBB Platform CLI v{version}[/param] (https://openbb.co)"
else:
version = (
"[param]OpenBB Platform CLI[/param] (https://openbb.co)"
)
self._console.print(
panel.Panel(
"\n" + kwargs["text"],
title=kwargs["menu"],
subtitle_align="right",
subtitle=version,
)
)
else:
self._console.print(kwargs["text"])
else:
print(self.filter_rich_tags(kwargs["text"])) # noqa: T201
elif not self._settings.TEST_MODE:
self._console.print(*args, **kwargs)
else:
print(*args, **kwargs) # noqa: T201
def input(self, *args, **kwargs):
"""Get input from the user."""
self.print(*args, **kwargs, end="")
return input()
|