Skip to content

Commit c5b2e69

Browse files
committed
pass size as a parameter
1 parent 2092d42 commit c5b2e69

File tree

7 files changed

+54
-20
lines changed

7 files changed

+54
-20
lines changed

src/textual/_doc.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def format_svg(source, language, css_class, options, md, attrs, **kwargs) -> str
3030
rows = int(attrs.get("lines", 24))
3131
columns = int(attrs.get("columns", 80))
3232
svg = take_svg_screenshot(
33-
None, path, press, title, terminal_size=(rows, columns)
33+
None, path, press, title, terminal_size=(columns, rows)
3434
)
3535
finally:
3636
os.chdir(cwd)
@@ -49,7 +49,7 @@ def take_svg_screenshot(
4949
app_path: str | None = None,
5050
press: Iterable[str] = ("_",),
5151
title: str | None = None,
52-
terminal_size: tuple[int, int] = (24, 80),
52+
terminal_size: tuple[int, int] = (80, 24),
5353
) -> str:
5454
"""
5555
@@ -65,10 +65,6 @@ def take_svg_screenshot(
6565
the screenshot was taken.
6666
6767
"""
68-
rows, columns = terminal_size
69-
70-
os.environ["COLUMNS"] = str(columns)
71-
os.environ["LINES"] = str(rows)
7268

7369
if app is None:
7470
assert app_path is not None
@@ -85,7 +81,11 @@ async def auto_pilot(pilot: Pilot) -> None:
8581
svg = app.export_screenshot(title=title)
8682
app.exit(svg)
8783

88-
svg = app.run(headless=True, auto_pilot=auto_pilot)
84+
svg = app.run(
85+
headless=True,
86+
auto_pilot=auto_pilot,
87+
size=terminal_size,
88+
)
8989
assert svg is not None
9090

9191
return svg

src/textual/app.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,11 @@ def size(self) -> Size:
444444
Returns:
445445
Size: Size of the terminal
446446
"""
447-
return Size(*self.console.size)
447+
if self._driver is not None and self._driver._size is not None:
448+
width, height = self._driver._size
449+
else:
450+
width, height = self.console.size
451+
return Size(width, height)
448452

449453
@property
450454
def log(self) -> Logger:
@@ -526,10 +530,11 @@ def export_screenshot(self, *, title: str | None = None) -> str:
526530
to use app title. Defaults to None.
527531
528532
"""
529-
533+
assert self._driver is not None, "App must be running"
534+
width, height = self.size
530535
console = Console(
531-
width=self.console.width,
532-
height=self.console.height,
536+
width=width,
537+
height=height,
533538
file=io.StringIO(),
534539
force_terminal=True,
535540
color_system="truecolor",
@@ -669,12 +674,15 @@ async def run_async(
669674
self,
670675
*,
671676
headless: bool = False,
677+
size: tuple[int, int] | None = None,
672678
auto_pilot: AutopilotCallbackType | None = None,
673679
) -> ReturnType | None:
674680
"""Run the app asynchronously.
675681
676682
Args:
677683
headless (bool, optional): Run in headless mode (no output). Defaults to False.
684+
size (tuple[int, int] | None, optional): Force terminal size to `(WIDTH, HEIGHT)`,
685+
or None to auto-detect. Defaults to None.
678686
auto_pilot (AutopilotCallbackType): An auto pilot coroutine.
679687
680688
Returns:
@@ -707,6 +715,7 @@ async def run_auto_pilot(
707715
await app._process_messages(
708716
ready_callback=None if auto_pilot is None else app_ready,
709717
headless=headless,
718+
terminal_size=size,
710719
)
711720
finally:
712721
if auto_pilot_task is not None:
@@ -719,12 +728,15 @@ def run(
719728
self,
720729
*,
721730
headless: bool = False,
731+
size: tuple[int, int] | None = None,
722732
auto_pilot: AutopilotCallbackType | None = None,
723733
) -> ReturnType | None:
724734
"""Run the app.
725735
726736
Args:
727737
headless (bool, optional): Run in headless mode (no output). Defaults to False.
738+
size (tuple[int, int] | None, optional): Force terminal size to `(WIDTH, HEIGHT)`,
739+
or None to auto-detect. Defaults to None.
728740
auto_pilot (AutopilotCallbackType): An auto pilot coroutine.
729741
730742
Returns:
@@ -735,6 +747,7 @@ async def run_app() -> None:
735747
"""Run the app."""
736748
await self.run_async(
737749
headless=headless,
750+
size=size,
738751
auto_pilot=auto_pilot,
739752
)
740753

@@ -1072,7 +1085,10 @@ def _print_error_renderables(self) -> None:
10721085
self._exit_renderables.clear()
10731086

10741087
async def _process_messages(
1075-
self, ready_callback: CallbackType | None = None, headless: bool = False
1088+
self,
1089+
ready_callback: CallbackType | None = None,
1090+
headless: bool = False,
1091+
terminal_size: tuple[int, int] | None = None,
10761092
) -> None:
10771093
self._set_active()
10781094

@@ -1161,7 +1177,7 @@ async def run_process_messages():
11611177
"type[Driver]",
11621178
HeadlessDriver if headless else self.driver_class,
11631179
)
1164-
driver = self._driver = driver_class(self.console, self)
1180+
driver = self._driver = driver_class(self.console, self, size=terminal_size)
11651181

11661182
driver.start_application_mode()
11671183
try:

src/textual/driver.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,17 @@
1313

1414
class Driver(ABC):
1515
def __init__(
16-
self, console: "Console", target: "MessageTarget", debug: bool = False
16+
self,
17+
console: "Console",
18+
target: "MessageTarget",
19+
*,
20+
debug: bool = False,
21+
size: tuple[int, int] | None = None,
1722
) -> None:
1823
self.console = console
1924
self._target = target
2025
self._debug = debug
26+
self._size = size
2127
self._loop = asyncio.get_running_loop()
2228
self._mouse_down_time = _clock.get_time_no_wait()
2329

src/textual/drivers/headless_driver.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ def is_headless(self) -> bool:
1414
return True
1515

1616
def _get_terminal_size(self) -> tuple[int, int]:
17+
if self._size is not None:
18+
return self._size
1719
width: int | None = 80
1820
height: int | None = 25
1921
import shutil

src/textual/drivers/linux_driver.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,14 @@ class LinuxDriver(Driver):
3030
"""Powers display and input for Linux / MacOS"""
3131

3232
def __init__(
33-
self, console: "Console", target: "MessageTarget", debug: bool = False
33+
self,
34+
console: "Console",
35+
target: "MessageTarget",
36+
*,
37+
debug: bool = False,
38+
size: tuple[int, int] | None = None,
3439
) -> None:
35-
super().__init__(console, target, debug)
40+
super().__init__(console, target, debug=debug, size=size)
3641
self.fileno = sys.stdin.fileno()
3742
self.attrs_before: list[Any] | None = None
3843
self.exit_event = Event()

src/textual/drivers/windows_driver.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,14 @@ class WindowsDriver(Driver):
1818
"""Powers display and input for Windows."""
1919

2020
def __init__(
21-
self, console: "Console", target: "MessageTarget", debug: bool = False
21+
self,
22+
console: "Console",
23+
target: "MessageTarget",
24+
*,
25+
debug: bool = False,
26+
size: tuple[int, int] | None = None,
2227
) -> None:
23-
super().__init__(console, target, debug)
28+
super().__init__(console, target, debug=debug, size=size)
2429
self.in_fileno = sys.stdin.fileno()
2530
self.out_fileno = sys.stdout.fileno()
2631

tests/snapshot_tests/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def snap_compare(
4141
def compare(
4242
app_path: str,
4343
press: Iterable[str] = ("_",),
44-
terminal_size: tuple[int, int] = (24, 80),
44+
terminal_size: tuple[int, int] = (80, 24),
4545
) -> bool:
4646
"""
4747
Compare a current screenshot of the app running at app_path, with
@@ -52,7 +52,7 @@ def compare(
5252
Args:
5353
app_path (str): The path of the app.
5454
press (Iterable[str]): Key presses to run before taking screenshot. "_" is a short pause.
55-
terminal_size (tuple[int, int]): A pair of integers (rows, columns), representing terminal size.
55+
terminal_size (tuple[int, int]): A pair of integers (WIDTH, SIZE), representing terminal size.
5656
5757
Returns:
5858
bool: True if the screenshot matches the snapshot.

0 commit comments

Comments
 (0)