From 2ee4e25742e8b2eb3ec3c2dd74ac55ff32705cd2 Mon Sep 17 00:00:00 2001 From: imSp4rky Date: Sat, 5 Dec 2026 20:24:22 -0700 Subject: [PATCH] fix(console): print all console output at the terminal width when wide_output is on --- docs/reference/configuration/misc.md | 2 +- tests/conftest.py | 6 ++++ tests/core/test_console_width.py | 52 ++++++++++++++++++++++++++++ unshackle/core/console.py | 31 +++++++++-------- unshackle/core/drm/key_resolution.py | 2 +- unshackle/core/tracks/tracks.py | 6 ++-- 6 files changed, 81 insertions(+), 18 deletions(-) create mode 100644 tests/core/test_console_width.py diff --git a/docs/reference/configuration/misc.md b/docs/reference/configuration/misc.md index db89d6899..12693e0de 100644 --- a/docs/reference/configuration/misc.md +++ b/docs/reference/configuration/misc.md @@ -152,7 +152,7 @@ calls. | Key | Type | Default | Description | |-----|------|---------|-------------| | `theme` | str | `catppuccin-mocha` | Named colour theme for all CLI output: banner, logs, progress bars, and help screens. One of `catppuccin-mocha` (aliases `default`, `catppuccin`, `mocha`), `dracula`, `nord`, `gruvbox`, `one-dark` (alias `onedark`), `tokyo-night` (alias `tokyonight`), `monokai`, `flexoki`, `solarized-dark` (alias `solarized`). Case-insensitive; underscores work in place of hyphens. Unknown names fall back to the default with a warning. Preview them all with `unshackle env theme`. | -| `wide_output` | bool | `false` | Print the `dl` title and track listings at the terminal width. When `false`, they fold at 80 columns, the width that logs and progress bars always use. | +| `wide_output` | bool | `false` | Print all console output (rules, logs, listings and progress bars) at the terminal width. The download and mux progress bars fill the line. When `false`, the output folds at 80 columns and these bars are 40 cells wide. The `env` listings use the terminal width and uncaught tracebacks use 80 columns, whatever the value. | --- diff --git a/tests/conftest.py b/tests/conftest.py index 09f0f6d35..5804b827a 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -77,6 +77,12 @@ def no_worker_handlers(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr(console, "_notice_handler", None) +@pytest.fixture(autouse=True) +def fixed_console_width(monkeypatch: pytest.MonkeyPatch) -> None: + """Hold the shared console at 80 columns, whatever the terminal and the user's ``wide_output``.""" + monkeypatch.setattr(console.console, "_width", 80) + + @pytest.fixture(autouse=True) def fresh_proxy_catalogues() -> None: """Fetch each proxy provider catalogue again, so one test's stub never answers another test.""" diff --git a/tests/core/test_console_width.py b/tests/core/test_console_width.py new file mode 100644 index 000000000..2dcb5156e --- /dev/null +++ b/tests/core/test_console_width.py @@ -0,0 +1,52 @@ +import os +import subprocess +import sys +from pathlib import Path + +import pytest + +SCRIPT = """ +from unshackle.core.config import config +config.wide_output = {wide} +from rich.padding import Padding +from rich.table import Table +from unshackle.core.console import console +from unshackle.core.tracks import Audio, Tracks, Video +print(console.width) +tracks = Tracks([Video(url="https://example.test/v.mpd", language="en"), Audio(url="https://example.test/a.mpd", language="en")]) +tree, (video, audio) = tracks.tree(add_progress=True) +video(downloaded="Downloaded") +audio(total=None, downloaded="1 MB/s") +grid = Table.grid() +grid.add_row(tree) +for line in console.render_lines(Padding(grid, (1, 5)), pad=False): + text = "".join(s.text for s in line) + if "━" in text: + print(text.count("━"), text.rfind("━")) +""" + + +@pytest.mark.parametrize(("wide", "width"), [(True, 120), (False, 80)]) +def test_console_follows_wide_output(wide: bool, width: int) -> None: + """With wide_output the console takes the terminal width and track bars fill the line to one right edge. + + Without it the console holds 80 columns and each bar 40 cells. Both are read at import, hence the subprocess. + """ + out = subprocess.run( + [sys.executable, "-c", SCRIPT.format(wide=wide)], + env={**os.environ, "COLUMNS": "120"}, + cwd=Path(__file__).parents[2], + capture_output=True, + text=True, + encoding="utf-8", + errors="replace", + check=True, + timeout=60, + ) + console_width, *bars = out.stdout.strip().splitlines()[-3:] + (full, full_end), (pulse, pulse_end) = (map(int, bar.split()) for bar in bars) + assert int(console_width) == width + if wide: + assert full > 40 and pulse > 40 and full_end == pulse_end + else: + assert full == pulse == 40 diff --git a/unshackle/core/console.py b/unshackle/core/console.py index a5438b071..e0c5691c9 100644 --- a/unshackle/core/console.py +++ b/unshackle/core/console.py @@ -26,6 +26,7 @@ from rich.padding import Padding, PaddingDimensions from rich.panel import Panel from rich.progress import BarColumn, Task from rich.rule import Rule +from rich.segment import Segment from rich.status import Status from rich.style import Style, StyleType from rich.table import Table @@ -378,8 +379,15 @@ class SyncLive(Live): class GradientPulseBarColumn(BarColumn): LUT_SIZE = 32 - def __init__(self, *args: Any, pulse_period: float = 40.0, pulse_speed: float = 15.0, **kwargs: Any) -> None: - super().__init__(*args, **kwargs) + def __init__( + self, + bar_width: int | None = None if config.wide_output else 40, + *args: Any, + pulse_period: float = 40.0, + pulse_speed: float = 15.0, + **kwargs: Any, + ) -> None: + super().__init__(bar_width, *args, **kwargs) self.pulse_period = pulse_period self.pulse_speed = pulse_speed lo = Color.parse(primary_scheme["dark_gray"]).triplet @@ -404,11 +412,9 @@ class _GradientPulse: def __rich_console__(self, console: Console, options: ConsoleOptions) -> RenderResult: width = min(self.width or options.max_width, options.max_width) - bar = Text() for i in range(width): fade = (math.cos((i - self.offset) * math.tau / self.period) + 1) / 2 - bar.append("━", self.lut[int(fade * (len(self.lut) - 1))]) - yield bar + yield Segment("━", self.lut[int(fade * (len(self.lut) - 1))]) def __rich_measure__(self, console: Console, options: ConsoleOptions) -> Measurement: return Measurement(4, self.width or options.max_width) @@ -452,7 +458,7 @@ rich.cells.cached_cell_len.cache_clear() console = ComfyConsole( log_time=False, log_path=False, - width=80, + width=None if config.wide_output else 80, theme=Theme( { "bar.back": primary_scheme["dark_gray"], @@ -502,9 +508,9 @@ def listing_table(title: str | None = None, **kwargs: Any) -> Table: def print_wide(renderable: RenderableType, pad: PaddingDimensions = (1, 3)) -> None: """Print a listing at the terminal width, indented like the rich-click help panels. - The shared console holds 80 columns to keep logs and progress compact. Long values such as paths - and URLs fold badly at that width, so this widens the console for one print and restores it after. It - restores ``_width``, because on a legacy Windows console the ``width`` property reads one column less. + Without ``wide_output`` the shared console holds 80 columns to keep logs and progress compact. Long values + such as paths and URLs fold badly at that width, so this widens the console for one print and restores it after. + It restores ``_width``, because on a legacy Windows console the ``width`` property reads one column less. """ fixed_width = console._width console.width = max(console.width, shutil.get_terminal_size().columns - console.legacy_windows) @@ -515,11 +521,8 @@ def print_wide(renderable: RenderableType, pad: PaddingDimensions = (1, 3)) -> N def print_listing(renderable: RenderableType) -> None: - """Print a listing at the terminal width when ``wide_output`` is on, else at the shared console width.""" - if config.wide_output: - print_wide(renderable, (0, 5)) - else: - console.print(Padding(renderable, (0, 5))) + """Print a listing at the shared console width, indented like the log lines.""" + console.print(Padding(renderable, (0, 5))) _prompt_handler: Callable[[str, str | None], str] | None = None diff --git a/unshackle/core/drm/key_resolution.py b/unshackle/core/drm/key_resolution.py index b9619dfce..84d09cc34 100644 --- a/unshackle/core/drm/key_resolution.py +++ b/unshackle/core/drm/key_resolution.py @@ -67,7 +67,7 @@ def truncate_pssh_for_display(pssh_string: str, drm_type: str) -> str: if logging.root.level == logging.DEBUG or not pssh_string: return pssh_string - max_width = console.width - len(drm_type) - 12 + max_width = max(console.width - len(drm_type) - 12, 3) if len(pssh_string) <= max_width: return pssh_string diff --git a/unshackle/core/tracks/tracks.py b/unshackle/core/tracks/tracks.py index dbbba4b41..834376a20 100644 --- a/unshackle/core/tracks/tracks.py +++ b/unshackle/core/tracks/tracks.py @@ -17,7 +17,7 @@ from typing import Any from langcodes import Language from rich.markup import escape from rich.progress import Progress, SpinnerColumn, TaskID, TextColumn, TimeRemainingColumn -from rich.table import Table +from rich.table import Column, Table from rich.tree import Tree from unshackle.core import binaries @@ -272,7 +272,9 @@ class Tracks: "•", TimeRemainingColumn(compact=True, elapsed_when_finished=True), "•", - TextColumn("[progress.data.speed]{task.fields[downloaded]}"), + TextColumn( + "[progress.data.speed]{task.fields[downloaded]}", table_column=Column(min_width=11) + ), console=console, speed_estimate_period=10, )