Files
unshackle/tests/core/test_console_width.py
T

56 lines
2.0 KiB
Python

import os
import subprocess
import sys
from pathlib import Path
import pytest
SCRIPT = """
import rich.console
rich.console.detect_legacy_windows = lambda: False
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.
The child turns off Rich legacy Windows mode, which on a Windows pipe drops one column and draws ASCII bars.
"""
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