summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorПавел Жуков <33721692+LeaveMyYard@users.noreply.github.com>2023-05-16 12:17:22 +0300
committerПавел Жуков <33721692+LeaveMyYard@users.noreply.github.com>2023-05-16 12:17:22 +0300
commit00a184952733167541d72fb7d64dabeb5cd44dfc (patch)
tree0512f100fe38a3ffd8928c0b90a74094cf2ca326
parenta3887611c86c2845c12af0c1e73c8240c7f512a5 (diff)
[MAIN-169] Fix rich cutting long lines
-rw-r--r--examples/custom_formatter.py6
-rw-r--r--robusta_krr/core/abstract/formatters.py1
-rw-r--r--robusta_krr/core/abstract/strategies.py2
-rw-r--r--robusta_krr/core/models/config.py4
-rw-r--r--robusta_krr/core/runner.py5
-rw-r--r--robusta_krr/formatters/table.py4
-rw-r--r--robusta_krr/strategies/simple.py1
-rw-r--r--robusta_krr/utils/configurable.py9
8 files changed, 24 insertions, 8 deletions
diff --git a/examples/custom_formatter.py b/examples/custom_formatter.py
index a091e22..d3cb98d 100644
--- a/examples/custom_formatter.py
+++ b/examples/custom_formatter.py
@@ -8,8 +8,14 @@ from robusta_krr.api.models import Result
class CustomFormatter(BaseFormatter):
+ # This is the name that will be used to reference the formatter in the CLI
__display_name__ = "my_formatter"
+ # This will pass the result to Rich Console for formatting.
+ # By default, the result is passed to `print` function.
+ # See https://rich.readthedocs.io/en/latest/ for more info
+ __rich_console__ = True
+
def format(self, result: Result) -> str:
return "Custom formatter"
diff --git a/robusta_krr/core/abstract/formatters.py b/robusta_krr/core/abstract/formatters.py
index a4d1e7b..a38e9f3 100644
--- a/robusta_krr/core/abstract/formatters.py
+++ b/robusta_krr/core/abstract/formatters.py
@@ -21,6 +21,7 @@ class BaseFormatter(abc.ABC):
"""Base class for result formatters."""
__display_name__: str
+ __rich_console__: bool = False
def __str__(self) -> str:
return self.__display_name__.title()
diff --git a/robusta_krr/core/abstract/strategies.py b/robusta_krr/core/abstract/strategies.py
index dab5940..6b54d63 100644
--- a/robusta_krr/core/abstract/strategies.py
+++ b/robusta_krr/core/abstract/strategies.py
@@ -73,7 +73,7 @@ class BaseStrategy(abc.ABC, Generic[_StrategySettings]):
if self.__doc__ is None:
return None
- return f"\n[b]{self} Strategy[/b]\n\n" + dedent(self.__doc__.format_map(self.settings.dict())).strip() + "\n"
+ return f"[b]{self} Strategy[/b]\n\n" + dedent(self.__doc__.format_map(self.settings.dict())).strip()
@abc.abstractmethod
def run(self, history_data: HistoryData, object_data: K8sObjectData) -> RunResult:
diff --git a/robusta_krr/core/models/config.py b/robusta_krr/core/models/config.py
index 75a5403..4804ac6 100644
--- a/robusta_krr/core/models/config.py
+++ b/robusta_krr/core/models/config.py
@@ -45,6 +45,10 @@ class Config(pd.BaseSettings):
other_args: dict[str, Any]
+ @property
+ def Formatter(self) -> type[BaseFormatter]:
+ return BaseFormatter.find(self.format)
+
@pd.validator("namespaces")
def validate_namespaces(cls, v: Union[list[str], Literal["*"]]) -> Union[list[str], Literal["*"]]:
if v == []:
diff --git a/robusta_krr/core/runner.py b/robusta_krr/core/runner.py
index 9e1096b..85e0e99 100644
--- a/robusta_krr/core/runner.py
+++ b/robusta_krr/core/runner.py
@@ -49,9 +49,10 @@ class Runner(Configurable):
self.echo(no_prefix=True)
def _process_result(self, result: Result) -> None:
- formatted = result.format(self.config.format)
+ Formatter = self.config.Formatter
+ formatted = result.format(Formatter)
self.echo("\n", no_prefix=True)
- self.print_result(formatted)
+ self.print_result(formatted, rich=Formatter.__rich_console__)
def __get_resource_minimal(self, resource: ResourceType) -> float:
if resource == ResourceType.CPU:
diff --git a/robusta_krr/formatters/table.py b/robusta_krr/formatters/table.py
index aa5c3bf..8ef1696 100644
--- a/robusta_krr/formatters/table.py
+++ b/robusta_krr/formatters/table.py
@@ -1,7 +1,6 @@
from __future__ import annotations
import itertools
-from typing import Optional
from rich.table import Table
@@ -18,6 +17,7 @@ class TableFormatter(BaseFormatter):
"""Formatter for text output."""
__display_name__ = "table"
+ __rich_console__ = True
def _format(self, value: RecommendationValue) -> str:
if value is None:
@@ -52,7 +52,7 @@ class TableFormatter(BaseFormatter):
table = Table(
show_header=True,
header_style="bold magenta",
- title=result.description,
+ title=f"\n{result.description}\n" if result.description else None,
title_justify="left",
title_style="",
# TODO: Fix points calculation at [MAIN-270]
diff --git a/robusta_krr/strategies/simple.py b/robusta_krr/strategies/simple.py
index 1340f63..c360a69 100644
--- a/robusta_krr/strategies/simple.py
+++ b/robusta_krr/strategies/simple.py
@@ -48,6 +48,7 @@ class SimpleStrategy(BaseStrategy[SimpleStrategySettings]):
"""
__display_name__ = "simple"
+ __rich_console__ = True
def run(self, history_data: HistoryData, object_data: K8sObjectData) -> RunResult:
cpu_usage = self.settings.calculate_cpu_proposal(history_data[ResourceType.CPU])
diff --git a/robusta_krr/utils/configurable.py b/robusta_krr/utils/configurable.py
index ded2900..4777a9c 100644
--- a/robusta_krr/utils/configurable.py
+++ b/robusta_krr/utils/configurable.py
@@ -29,12 +29,15 @@ class Configurable(abc.ABC):
def __add_prefix(text: str, prefix: str, /, no_prefix: bool) -> str:
return f"{prefix} {text}" if not no_prefix else text
- def print_result(self, content: str) -> None:
+ def print_result(self, content: str, rich: bool = True) -> None:
"""
Prints the result in a console. The result is always put in stdout.
"""
- result_console = Console()
- result_console.print(content)
+ if rich:
+ result_console = Console()
+ result_console.print(content, overflow="ignore")
+ else:
+ print(content)
def echo(
self, message: str = "", *, no_prefix: bool = False, type: Literal["INFO", "WARNING", "ERROR"] = "INFO"