fix: assume 200char terminal if no terminal size is available

closes #11477
This commit is contained in:
Matthias
2025-03-10 21:13:32 +01:00
parent fe48f6769f
commit b0b9e398e1
5 changed files with 40 additions and 23 deletions

View File

@@ -17,11 +17,11 @@ def start_list_exchanges(args: dict[str, Any]) -> None:
:param args: Cli args from Arguments()
:return: None
"""
from rich.console import Console
from rich.table import Table
from rich.text import Text
from freqtrade.exchange import list_available_exchanges
from freqtrade.loggers.rich_console import get_rich_console
available_exchanges: list[ValidExchangesType] = list_available_exchanges(
args["list_exchanges_all"]
@@ -77,15 +77,16 @@ def start_list_exchanges(args: dict[str, Any]) -> None:
)
# table.add_row(*[exchange[header] for header in headers])
console = Console()
console = get_rich_console()
console.print(table)
def _print_objs_tabular(objs: list, print_colorized: bool) -> None:
from rich.console import Console
from rich.table import Table
from rich.text import Text
from freqtrade.loggers.rich_console import get_rich_console
names = [s["name"] for s in objs]
objs_to_print: list[dict[str, Text | str]] = [
{
@@ -118,10 +119,7 @@ def _print_objs_tabular(objs: list, print_colorized: bool) -> None:
for row in objs_to_print:
table.add_row(*[row[header] for header in objs_to_print[0].keys()])
console = Console(
color_system="auto" if print_colorized else None,
width=200 if "pytest" in sys.modules else None,
)
console = get_rich_console(color_system="auto" if print_colorized else None)
console.print(table)

View File

@@ -3,12 +3,11 @@ from logging import Formatter
from logging.handlers import RotatingFileHandler, SysLogHandler
from pathlib import Path
from rich.console import Console
from freqtrade.constants import Config
from freqtrade.exceptions import OperationalException
from freqtrade.loggers.buffering_handler import FTBufferingHandler
from freqtrade.loggers.ft_rich_handler import FtRichHandler
from freqtrade.loggers.rich_console import get_rich_console
from freqtrade.loggers.set_log_levels import set_loggers
@@ -22,7 +21,8 @@ LOGFORMAT = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
bufferHandler = FTBufferingHandler(1000)
bufferHandler.setFormatter(Formatter(LOGFORMAT))
error_console = Console(stderr=True, color_system=None)
error_console = get_rich_console(stderr=True, color_system=None)
def get_existing_handlers(handlertype):

View File

@@ -0,0 +1,26 @@
import sys
from shutil import get_terminal_size
from rich.console import Console
def console_width() -> int | None:
"""
Get the width of the console
"""
if any(module in ["pytest", "ipykernel"] for module in sys.modules):
return 200
width, _ = get_terminal_size((1, 24))
# Fall back to 200 if terminal size is not available.
# This is determined by assuming an insane width of 1char, which is unlikely.
w = None if width > 1 else 200
return w
def get_rich_console(**kwargs) -> Console:
"""
Get a rich console with default settings
"""
kwargs["width"] = kwargs.get("width", console_width())
return Console(**kwargs)

View File

@@ -7,7 +7,6 @@ from rich.progress import (
TimeRemainingColumn,
)
from freqtrade.loggers import error_console
from freqtrade.util.rich_progress import CustomProgress
@@ -21,6 +20,8 @@ def get_progress_tracker(**kwargs) -> CustomProgress:
"""
Get progress Bar with custom columns.
"""
from freqtrade.loggers import error_console
return CustomProgress(
TextColumn("[progress.description]{task.description}"),
BarColumn(bar_width=None),

View File

@@ -1,12 +1,12 @@
import sys
from collections.abc import Sequence
from typing import Any, TypeAlias
from pandas import DataFrame
from rich.console import Console
from rich.table import Column, Table
from rich.text import Text
from freqtrade.loggers.rich_console import get_rich_console
TextOrString: TypeAlias = str | Text
@@ -38,11 +38,7 @@ def print_rich_table(
row_to_add: list[str | Text] = [r if isinstance(r, Text) else str(r) for r in row]
table.add_row(*row_to_add)
width = None
if any(module in ["pytest", "ipykernel"] for module in sys.modules):
width = 200
console = Console(width=width)
console = get_rich_console()
console.print(table)
@@ -74,9 +70,5 @@ def print_df_rich_table(
row = [_format_value(x, floatfmt=".3f") for x in value_list]
table.add_row(*row)
width = None
if any(module in ["pytest", "ipykernel"] for module in sys.modules):
width = 200
console = Console(width=width)
console = get_rich_console()
console.print(table)