Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make default filename for screenshots use ISO8601 datetime format #1518

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
davep marked this conversation as resolved.
Show resolved Hide resolved
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