diff --git a/CHANGELOG.md b/CHANGELOG.md index 79d240fc4d..91ab0e788e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Added support for environment variable `TEXTUAL_ANIMATIONS` to control what animations Textual displays https://github.com/Textualize/textual/pull/4062 - Add attribute `App.animation_level` to control whether animations on that app run or not https://github.com/Textualize/textual/pull/4062 +- Added support for a `TEXTUAL_SCREENSHOT_LOCATION` environment variable to specify the location of an automated screenshot https://github.com/Textualize/textual/pull/4181/ +- Added support for a `TEXTUAL_SCREENSHOT_FILENAME` environment variable to specify the filename of an automated screenshot https://github.com/Textualize/textual/pull/4181/ ## [0.51.0] - 2024-02-15 diff --git a/src/textual/app.py b/src/textual/app.py index b2b1a81649..08c4392fbe 100644 --- a/src/textual/app.py +++ b/src/textual/app.py @@ -1153,7 +1153,7 @@ def export_screenshot(self, *, title: str | None = None) -> str: def save_screenshot( self, filename: str | None = None, - path: str = "./", + path: str | None = None, time_format: str | None = None, ) -> str: """Save an SVG screenshot of the current screen. @@ -1168,7 +1168,8 @@ def save_screenshot( Returns: Filename of screenshot. """ - if filename is None: + path = path or "./" + if not filename: if time_format is None: dt = datetime.now().isoformat() else: @@ -2397,7 +2398,10 @@ async def _ready(self) -> None: async def take_screenshot() -> None: """Take a screenshot and exit.""" - self.save_screenshot() + self.save_screenshot( + path=constants.SCREENSHOT_LOCATION, + filename=constants.SCREENSHOT_FILENAME, + ) self.exit() if constants.SCREENSHOT_DELAY >= 0: diff --git a/src/textual/constants.py b/src/textual/constants.py index 74a91797e7..6d0ebfe323 100644 --- a/src/textual/constants.py +++ b/src/textual/constants.py @@ -95,6 +95,12 @@ def _get_textual_animations() -> AnimationLevel: SCREENSHOT_DELAY: Final[int] = _get_environ_int("TEXTUAL_SCREENSHOT", -1) """Seconds delay before taking screenshot.""" +SCREENSHOT_LOCATION: Final[str | None] = get_environ("TEXTUAL_SCREENSHOT_LOCATION") +"""The location where screenshots should be written.""" + +SCREENSHOT_FILENAME: Final[str | None] = get_environ("TEXTUAL_SCREENSHOT_FILENAME") +"""The filename to use for the screenshot.""" + PRESS: Final[str] = get_environ("TEXTUAL_PRESS", "") """Keys to automatically press."""