pass size as a parameter

This commit is contained in:
Will McGugan
2022-10-29 21:09:16 +01:00
parent 2092d42bbf
commit c5b2e6982e
7 changed files with 54 additions and 20 deletions

View File

@@ -30,7 +30,7 @@ def format_svg(source, language, css_class, options, md, attrs, **kwargs) -> str
rows = int(attrs.get("lines", 24))
columns = int(attrs.get("columns", 80))
svg = take_svg_screenshot(
None, path, press, title, terminal_size=(rows, columns)
None, path, press, title, terminal_size=(columns, rows)
)
finally:
os.chdir(cwd)
@@ -49,7 +49,7 @@ def take_svg_screenshot(
app_path: str | None = None,
press: Iterable[str] = ("_",),
title: str | None = None,
terminal_size: tuple[int, int] = (24, 80),
terminal_size: tuple[int, int] = (80, 24),
) -> str:
"""
@@ -65,10 +65,6 @@ def take_svg_screenshot(
the screenshot was taken.
"""
rows, columns = terminal_size
os.environ["COLUMNS"] = str(columns)
os.environ["LINES"] = str(rows)
if app is None:
assert app_path is not None
@@ -85,7 +81,11 @@ def take_svg_screenshot(
svg = app.export_screenshot(title=title)
app.exit(svg)
svg = app.run(headless=True, auto_pilot=auto_pilot)
svg = app.run(
headless=True,
auto_pilot=auto_pilot,
size=terminal_size,
)
assert svg is not None
return svg

View File

@@ -444,7 +444,11 @@ class App(Generic[ReturnType], DOMNode):
Returns:
Size: Size of the terminal
"""
return Size(*self.console.size)
if self._driver is not None and self._driver._size is not None:
width, height = self._driver._size
else:
width, height = self.console.size
return Size(width, height)
@property
def log(self) -> Logger:
@@ -526,10 +530,11 @@ class App(Generic[ReturnType], DOMNode):
to use app title. Defaults to None.
"""
assert self._driver is not None, "App must be running"
width, height = self.size
console = Console(
width=self.console.width,
height=self.console.height,
width=width,
height=height,
file=io.StringIO(),
force_terminal=True,
color_system="truecolor",
@@ -669,12 +674,15 @@ class App(Generic[ReturnType], DOMNode):
self,
*,
headless: bool = False,
size: tuple[int, int] | None = None,
auto_pilot: AutopilotCallbackType | None = None,
) -> ReturnType | None:
"""Run the app asynchronously.
Args:
headless (bool, optional): Run in headless mode (no output). Defaults to False.
size (tuple[int, int] | None, optional): Force terminal size to `(WIDTH, HEIGHT)`,
or None to auto-detect. Defaults to None.
auto_pilot (AutopilotCallbackType): An auto pilot coroutine.
Returns:
@@ -707,6 +715,7 @@ class App(Generic[ReturnType], DOMNode):
await app._process_messages(
ready_callback=None if auto_pilot is None else app_ready,
headless=headless,
terminal_size=size,
)
finally:
if auto_pilot_task is not None:
@@ -719,12 +728,15 @@ class App(Generic[ReturnType], DOMNode):
self,
*,
headless: bool = False,
size: tuple[int, int] | None = None,
auto_pilot: AutopilotCallbackType | None = None,
) -> ReturnType | None:
"""Run the app.
Args:
headless (bool, optional): Run in headless mode (no output). Defaults to False.
size (tuple[int, int] | None, optional): Force terminal size to `(WIDTH, HEIGHT)`,
or None to auto-detect. Defaults to None.
auto_pilot (AutopilotCallbackType): An auto pilot coroutine.
Returns:
@@ -735,6 +747,7 @@ class App(Generic[ReturnType], DOMNode):
"""Run the app."""
await self.run_async(
headless=headless,
size=size,
auto_pilot=auto_pilot,
)
@@ -1072,7 +1085,10 @@ class App(Generic[ReturnType], DOMNode):
self._exit_renderables.clear()
async def _process_messages(
self, ready_callback: CallbackType | None = None, headless: bool = False
self,
ready_callback: CallbackType | None = None,
headless: bool = False,
terminal_size: tuple[int, int] | None = None,
) -> None:
self._set_active()
@@ -1161,7 +1177,7 @@ class App(Generic[ReturnType], DOMNode):
"type[Driver]",
HeadlessDriver if headless else self.driver_class,
)
driver = self._driver = driver_class(self.console, self)
driver = self._driver = driver_class(self.console, self, size=terminal_size)
driver.start_application_mode()
try:

View File

@@ -13,11 +13,17 @@ if TYPE_CHECKING:
class Driver(ABC):
def __init__(
self, console: "Console", target: "MessageTarget", debug: bool = False
self,
console: "Console",
target: "MessageTarget",
*,
debug: bool = False,
size: tuple[int, int] | None = None,
) -> None:
self.console = console
self._target = target
self._debug = debug
self._size = size
self._loop = asyncio.get_running_loop()
self._mouse_down_time = _clock.get_time_no_wait()

View File

@@ -14,6 +14,8 @@ class HeadlessDriver(Driver):
return True
def _get_terminal_size(self) -> tuple[int, int]:
if self._size is not None:
return self._size
width: int | None = 80
height: int | None = 25
import shutil

View File

@@ -30,9 +30,14 @@ class LinuxDriver(Driver):
"""Powers display and input for Linux / MacOS"""
def __init__(
self, console: "Console", target: "MessageTarget", debug: bool = False
self,
console: "Console",
target: "MessageTarget",
*,
debug: bool = False,
size: tuple[int, int] | None = None,
) -> None:
super().__init__(console, target, debug)
super().__init__(console, target, debug=debug, size=size)
self.fileno = sys.stdin.fileno()
self.attrs_before: list[Any] | None = None
self.exit_event = Event()

View File

@@ -18,9 +18,14 @@ class WindowsDriver(Driver):
"""Powers display and input for Windows."""
def __init__(
self, console: "Console", target: "MessageTarget", debug: bool = False
self,
console: "Console",
target: "MessageTarget",
*,
debug: bool = False,
size: tuple[int, int] | None = None,
) -> None:
super().__init__(console, target, debug)
super().__init__(console, target, debug=debug, size=size)
self.in_fileno = sys.stdin.fileno()
self.out_fileno = sys.stdout.fileno()

View File

@@ -41,7 +41,7 @@ def snap_compare(
def compare(
app_path: str,
press: Iterable[str] = ("_",),
terminal_size: tuple[int, int] = (24, 80),
terminal_size: tuple[int, int] = (80, 24),
) -> bool:
"""
Compare a current screenshot of the app running at app_path, with
@@ -52,7 +52,7 @@ def snap_compare(
Args:
app_path (str): The path of the app.
press (Iterable[str]): Key presses to run before taking screenshot. "_" is a short pause.
terminal_size (tuple[int, int]): A pair of integers (rows, columns), representing terminal size.
terminal_size (tuple[int, int]): A pair of integers (WIDTH, SIZE), representing terminal size.
Returns:
bool: True if the screenshot matches the snapshot.