Skip to content

Commit 1604c2b

Browse files
authored
Merge pull request #4181 from davep/screenshot-location
Add support for specifying the screenshot location and filename
2 parents e849168 + c52ad4e commit 1604c2b

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1616

1717
- Added support for environment variable `TEXTUAL_ANIMATIONS` to control what animations Textual displays https://github.com/Textualize/textual/pull/4062
1818
- Add attribute `App.animation_level` to control whether animations on that app run or not https://github.com/Textualize/textual/pull/4062
19+
- Added support for a `TEXTUAL_SCREENSHOT_LOCATION` environment variable to specify the location of an automated screenshot https://github.com/Textualize/textual/pull/4181/
20+
- Added support for a `TEXTUAL_SCREENSHOT_FILENAME` environment variable to specify the filename of an automated screenshot https://github.com/Textualize/textual/pull/4181/
1921

2022

2123
## [0.51.0] - 2024-02-15

src/textual/app.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,7 +1153,7 @@ def export_screenshot(self, *, title: str | None = None) -> str:
11531153
def save_screenshot(
11541154
self,
11551155
filename: str | None = None,
1156-
path: str = "./",
1156+
path: str | None = None,
11571157
time_format: str | None = None,
11581158
) -> str:
11591159
"""Save an SVG screenshot of the current screen.
@@ -1168,7 +1168,8 @@ def save_screenshot(
11681168
Returns:
11691169
Filename of screenshot.
11701170
"""
1171-
if filename is None:
1171+
path = path or "./"
1172+
if not filename:
11721173
if time_format is None:
11731174
dt = datetime.now().isoformat()
11741175
else:
@@ -2397,7 +2398,10 @@ async def _ready(self) -> None:
23972398

23982399
async def take_screenshot() -> None:
23992400
"""Take a screenshot and exit."""
2400-
self.save_screenshot()
2401+
self.save_screenshot(
2402+
path=constants.SCREENSHOT_LOCATION,
2403+
filename=constants.SCREENSHOT_FILENAME,
2404+
)
24012405
self.exit()
24022406

24032407
if constants.SCREENSHOT_DELAY >= 0:

src/textual/constants.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ def _get_textual_animations() -> AnimationLevel:
9595
SCREENSHOT_DELAY: Final[int] = _get_environ_int("TEXTUAL_SCREENSHOT", -1)
9696
"""Seconds delay before taking screenshot."""
9797

98+
SCREENSHOT_LOCATION: Final[str | None] = get_environ("TEXTUAL_SCREENSHOT_LOCATION")
99+
"""The location where screenshots should be written."""
100+
101+
SCREENSHOT_FILENAME: Final[str | None] = get_environ("TEXTUAL_SCREENSHOT_FILENAME")
102+
"""The filename to use for the screenshot."""
103+
98104
PRESS: Final[str] = get_environ("TEXTUAL_PRESS", "")
99105
"""Keys to automatically press."""
100106

0 commit comments

Comments
 (0)