Skip to content

Commit b0b9e39

Browse files
committed
fix: assume 200char terminal if no terminal size is available
closes freqtrade#11477
1 parent fe48f67 commit b0b9e39

File tree

5 files changed

+40
-23
lines changed

5 files changed

+40
-23
lines changed

freqtrade/commands/list_commands.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ def start_list_exchanges(args: dict[str, Any]) -> None:
1717
:param args: Cli args from Arguments()
1818
:return: None
1919
"""
20-
from rich.console import Console
2120
from rich.table import Table
2221
from rich.text import Text
2322

2423
from freqtrade.exchange import list_available_exchanges
24+
from freqtrade.loggers.rich_console import get_rich_console
2525

2626
available_exchanges: list[ValidExchangesType] = list_available_exchanges(
2727
args["list_exchanges_all"]
@@ -77,15 +77,16 @@ def start_list_exchanges(args: dict[str, Any]) -> None:
7777
)
7878
# table.add_row(*[exchange[header] for header in headers])
7979

80-
console = Console()
80+
console = get_rich_console()
8181
console.print(table)
8282

8383

8484
def _print_objs_tabular(objs: list, print_colorized: bool) -> None:
85-
from rich.console import Console
8685
from rich.table import Table
8786
from rich.text import Text
8887

88+
from freqtrade.loggers.rich_console import get_rich_console
89+
8990
names = [s["name"] for s in objs]
9091
objs_to_print: list[dict[str, Text | str]] = [
9192
{
@@ -118,10 +119,7 @@ def _print_objs_tabular(objs: list, print_colorized: bool) -> None:
118119
for row in objs_to_print:
119120
table.add_row(*[row[header] for header in objs_to_print[0].keys()])
120121

121-
console = Console(
122-
color_system="auto" if print_colorized else None,
123-
width=200 if "pytest" in sys.modules else None,
124-
)
122+
console = get_rich_console(color_system="auto" if print_colorized else None)
125123
console.print(table)
126124

127125

freqtrade/loggers/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
from logging.handlers import RotatingFileHandler, SysLogHandler
44
from pathlib import Path
55

6-
from rich.console import Console
7-
86
from freqtrade.constants import Config
97
from freqtrade.exceptions import OperationalException
108
from freqtrade.loggers.buffering_handler import FTBufferingHandler
119
from freqtrade.loggers.ft_rich_handler import FtRichHandler
10+
from freqtrade.loggers.rich_console import get_rich_console
1211
from freqtrade.loggers.set_log_levels import set_loggers
1312

1413

@@ -22,7 +21,8 @@
2221
bufferHandler = FTBufferingHandler(1000)
2322
bufferHandler.setFormatter(Formatter(LOGFORMAT))
2423

25-
error_console = Console(stderr=True, color_system=None)
24+
25+
error_console = get_rich_console(stderr=True, color_system=None)
2626

2727

2828
def get_existing_handlers(handlertype):

freqtrade/loggers/rich_console.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import sys
2+
from shutil import get_terminal_size
3+
4+
from rich.console import Console
5+
6+
7+
def console_width() -> int | None:
8+
"""
9+
Get the width of the console
10+
"""
11+
if any(module in ["pytest", "ipykernel"] for module in sys.modules):
12+
return 200
13+
14+
width, _ = get_terminal_size((1, 24))
15+
# Fall back to 200 if terminal size is not available.
16+
# This is determined by assuming an insane width of 1char, which is unlikely.
17+
w = None if width > 1 else 200
18+
return w
19+
20+
21+
def get_rich_console(**kwargs) -> Console:
22+
"""
23+
Get a rich console with default settings
24+
"""
25+
kwargs["width"] = kwargs.get("width", console_width())
26+
return Console(**kwargs)

freqtrade/util/progress_tracker.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
TimeRemainingColumn,
88
)
99

10-
from freqtrade.loggers import error_console
1110
from freqtrade.util.rich_progress import CustomProgress
1211

1312

@@ -21,6 +20,8 @@ def get_progress_tracker(**kwargs) -> CustomProgress:
2120
"""
2221
Get progress Bar with custom columns.
2322
"""
23+
from freqtrade.loggers import error_console
24+
2425
return CustomProgress(
2526
TextColumn("[progress.description]{task.description}"),
2627
BarColumn(bar_width=None),

freqtrade/util/rich_tables.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import sys
21
from collections.abc import Sequence
32
from typing import Any, TypeAlias
43

54
from pandas import DataFrame
6-
from rich.console import Console
75
from rich.table import Column, Table
86
from rich.text import Text
97

8+
from freqtrade.loggers.rich_console import get_rich_console
9+
1010

1111
TextOrString: TypeAlias = str | Text
1212

@@ -38,11 +38,7 @@ def print_rich_table(
3838
row_to_add: list[str | Text] = [r if isinstance(r, Text) else str(r) for r in row]
3939
table.add_row(*row_to_add)
4040

41-
width = None
42-
if any(module in ["pytest", "ipykernel"] for module in sys.modules):
43-
width = 200
44-
45-
console = Console(width=width)
41+
console = get_rich_console()
4642
console.print(table)
4743

4844

@@ -74,9 +70,5 @@ def print_df_rich_table(
7470
row = [_format_value(x, floatfmt=".3f") for x in value_list]
7571
table.add_row(*row)
7672

77-
width = None
78-
if any(module in ["pytest", "ipykernel"] for module in sys.modules):
79-
width = 200
80-
81-
console = Console(width=width)
73+
console = get_rich_console()
8274
console.print(table)

0 commit comments

Comments
 (0)