Skip to content

Commit

Permalink
Merge pull request #1518 from edrogers/fix-formatting-of-default-scre…
Browse files Browse the repository at this point in the history
…enshot-filename

Make default filename for screenshots use ISO8601 datetime format
  • Loading branch information
willmcgugan authored Jan 19, 2023
2 parents c6909b7 + a871986 commit 6b51f00
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [0.11.0] - Unreleased

### Changed

- The default filename for screenshots uses a datetime format similar to ISO8601, but with reserved characters replaced by underscores https://github.com/Textualize/textual/pull/1518

## [0.10.0] - 2023-01-19

### Added
Expand Down
18 changes: 11 additions & 7 deletions src/textual/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -722,25 +722,29 @@ def save_screenshot(
self,
filename: str | None = None,
path: str = "./",
time_format: str = "%Y%m%d %H%M%S %f",
time_format: str | None = None,
) -> str:
"""Save an SVG screenshot of the current screen.
Args:
filename: Filename of SVG screenshot, or None to auto-generate
a filename with the date and time. Defaults to None.
path: Path to directory for output. Defaults to current working directory.
time_format: Time format to use if filename is None. Defaults to "%Y-%m-%d %X %f".
time_format: Date and time format to use if filename is None.
Defaults to a format like ISO 8601 with some reserved characters replaced with underscores.
Returns:
Filename of screenshot.
"""
if filename is None:
svg_filename = (
f"{self.title.lower()} {datetime.now().strftime(time_format)}.svg"
)
for reserved in '<>:"/\\|?*':
svg_filename = svg_filename.replace(reserved, "_")
if time_format is None:
dt = datetime.now().isoformat()
else:
dt = datetime.now().strftime(time_format)
svg_filename_stem = f"{self.title.lower()} {dt}"
for reserved in ' <>:"/\\|?*.':
svg_filename_stem = svg_filename_stem.replace(reserved, "_")
svg_filename = svg_filename_stem + ".svg"
else:
svg_filename = filename
svg_path = os.path.expanduser(os.path.join(path, svg_filename))
Expand Down

0 comments on commit 6b51f00

Please sign in to comment.