From: Christian Heller Date: Mon, 9 Sep 2024 20:48:10 +0000 (+0200) Subject: In browser.py, fix sorting, make configurable via command line. X-Git-Url: https://plomlompom.com/repos/%7B%7B%20web_path%20%7D%7D/static/%7B%7Bprefix%7D%7D/templates?a=commitdiff_plain;h=503aa7fbbf9e36f0a4138afbebfc33c44eb3f09f;p=stable_plom In browser.py, fix sorting, make configurable via command line. --- diff --git a/browser.py b/browser.py index 5f6c26b..873b35b 100755 --- a/browser.py +++ b/browser.py @@ -16,6 +16,7 @@ from stable.gen_params import (GenParams, # noqa: E402 GEN_PARAMS, GEN_PARAMS_STR) # noqa: E402 IMG_DIR_DEFAULT = '.' +SORT_DEFAULT = 'guidance,n_steps,model,scheduler,prompt,seed,height,width' UPPER_DIR = '..' CACHE_PATH = 'cache.json' @@ -157,7 +158,16 @@ class MainWindow(Gtk.Window): def init_sort_orderer(): self.sort_order = [p.lower() for p in GEN_PARAMS] - self.sort_order += ['last_mod_time', 'name'] + new_sort_order = [] + do_reverse = '-' in self.app.suggested_sort_order + for pattern in self.app.suggested_sort_order: + for name in [n for n in self.sort_order + if n.startswith(pattern)]: + self.sort_order.remove(name) + new_sort_order += [name] + self.sort_order = new_sort_order + self.sort_order + if do_reverse: + self.sort_order.reverse() self.sort_store = Gio.ListStore(item_type=SortLabelItem) self.sort_selection = Gtk.SingleSelection.new(self.sort_store) factory = Gtk.SignalListItemFactory() @@ -244,13 +254,16 @@ class MainWindow(Gtk.Window): if self.include_dirs: if isinstance(a, DirItem) and isinstance(b, DirItem): cmp_upper_dir = f' {UPPER_DIR}' - if cmp_upper_dir in (a.name, b.name): - return cmp_upper_dir == b.name + if a.name == cmp_upper_dir: + return +1 + if b.name == cmp_upper_dir: + return -1 elif isinstance(a, DirItem): - return False + return +1 elif isinstance(b, DirItem): - return True + return +1 # apply self.sort_order within DirItems and FileItems (separately) + result = 0 for key in self.sort_order: a_cmp = None b_cmp = None @@ -258,13 +271,15 @@ class MainWindow(Gtk.Window): a_cmp = getattr(a, key) if hasattr(b, key): b_cmp = getattr(b, key) - if a_cmp is None and b_cmp is None: - return True - if a_cmp is None: - return False - if b_cmp is None: - return True - return a_cmp > b_cmp + if a_cmp is None and b_cmp is not None: + result = -1 + elif a_cmp is not None and b_cmp is None: + result = +1 + elif a_cmp > b_cmp: + result = +1 + elif a_cmp < b_cmp: + result = -1 + return result def init_gallery_slot(file_item): slot = Gtk.Box() @@ -562,6 +577,7 @@ class MainWindow(Gtk.Window): class Application(Gtk.Application): """Image browser application class.""" img_dir_absolute: str + suggested_sort_order: list[str] def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) @@ -570,8 +586,10 @@ class Application(Gtk.Application): """Parse arguments, start window, and keep it open.""" parser = ArgumentParser() parser.add_argument('directory', default=IMG_DIR_DEFAULT, nargs='?') + parser.add_argument('-s', '--sort-order', default=SORT_DEFAULT) opts = parser.parse_args() self.img_dir_absolute = abspath(opts.directory) + self.suggested_sort_order = opts.sort_order.split(',') win = MainWindow(self) win.present() self.hold()