From ace77e04a9b0ff766b54b927b57ec58c27ab8fbe Mon Sep 17 00:00:00 2001 From: Isaiah Odhner Date: Wed, 6 Sep 2023 02:42:03 -0400 Subject: [PATCH] Recursively find the .ans files --- src/textual_paint/gallery.py | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/textual_paint/gallery.py b/src/textual_paint/gallery.py index b75a56e..8602adc 100644 --- a/src/textual_paint/gallery.py +++ b/src/textual_paint/gallery.py @@ -2,6 +2,7 @@ import argparse import os +from pathlib import Path from textual.app import App, ComposeResult from textual.binding import Binding @@ -89,21 +90,29 @@ class GalleryApp(App[None]): def _load(self) -> None: """Load the folder specified on the command line.""" - folder = args.folder - if folder is None: - folder = os.path.join(os.path.dirname(__file__), "../../samples") - if not os.path.isdir(folder): - raise Exception(f"Folder not found: {folder}") + if args.folder is None: + gallery_folder = Path(os.path.dirname(__file__), "../../samples") + else: + gallery_folder = Path(args.folder) - for filename in os.listdir(folder): - if not filename.endswith(".ans"): - continue - path = os.path.join(folder, filename) + if not gallery_folder.exists(): + self.exit(None, f"Folder not found: {gallery_folder}") + + if not gallery_folder.is_dir(): + self.exit(None, f"Not a folder: {gallery_folder}") + + glob = "**/*.ans" + # glob = "**/*.{ans,txt}" # doesn't work with pathlib + + for path in gallery_folder.rglob(glob): # with open(path, "r", encoding="cp437") as f: with open(path, "r", encoding="utf8") as f: image = AnsiArtDocument.from_ansi(f.read()) - self.scroll.mount(GalleryItem(image, caption=filename)) + self.scroll.mount(GalleryItem(image, caption=path.name)) + + if len(self.scroll.children) == 0: + self.exit(None, f"No ANSI art ({glob}) found in folder: {gallery_folder}") def _scroll_to_adjacent_item(self, delta_index: int = 0) -> None: """Scroll to the next/previous item."""