From 70afa25c4bdf9b0a788879037e6feb7d37b916e0 Mon Sep 17 00:00:00 2001 From: Isaiah Odhner Date: Wed, 6 Sep 2023 17:45:51 -0400 Subject: [PATCH] Hide junk from gallery by default --- README.md | 2 +- src/textual_paint/gallery.py | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 424189b..48f8725 100644 --- a/README.md +++ b/README.md @@ -164,7 +164,7 @@ You can display a saved ANSI file in the terminal with `cat`: cat samples/ship.ans ``` -To view all the sample files, run: +To browse the sample art, run: ```bash python -m src.textual_paint.gallery diff --git a/src/textual_paint/gallery.py b/src/textual_paint/gallery.py index 885405b..9732d75 100644 --- a/src/textual_paint/gallery.py +++ b/src/textual_paint/gallery.py @@ -92,8 +92,10 @@ class GalleryApp(App[None]): def _load(self) -> None: """Load the folder specified on the command line.""" + hide_old_versions = False if args.folder is None: gallery_folder = Path(os.path.dirname(__file__), "../../samples").resolve() + hide_old_versions = True else: gallery_folder = Path(args.folder) @@ -132,6 +134,27 @@ class GalleryApp(App[None]): ]) paths = [path for _, path in sorted_parts_and_paths] + if hide_old_versions: + # Hide any paths that are not the latest version of a file, + # for files matching some_identifier_vX.Y_optional_comment.ext + latest_versions: dict[str, tuple[float, Path]] = {} + for path in paths: + version_match = re.match(r"(.+)_v(\d+(?:\.\d+)?)", path.stem) + if version_match: + id = version_match.group(1) + version = float(version_match.group(2)) + if id in latest_versions: + if version > latest_versions[id][0]: + latest_versions[id] = version, path + else: + latest_versions[id] = version, path + else: + latest_versions[str(path)] = -1, path + paths = [path for _, path in latest_versions.values()] + + # Hide some uninteresting files + paths = [path for path in paths if not re.match("0x0|1x1|2x2|4x4_font_template|gradient_test|pipe_strip_mega|cp437_as_utf8|galaxies_v1", path.name)] + # Debugging # self.exit(None, "\n".join(str(path) for path in paths)) # return