From 8497c29ec96e0916062e18b1e9fafb54d2377d1e Mon Sep 17 00:00:00 2001 From: Jacob Chapman <7908073+chapmanjacobd@users.noreply.github.com> Date: Wed, 11 Dec 2024 16:06:59 +0000 Subject: [PATCH] torrent-info: fix categories sorting --- xklb/playback/torrents_info.py | 8 ++++++++ xklb/utils/iterables.py | 9 +++++++++ 2 files changed, 17 insertions(+) diff --git a/xklb/playback/torrents_info.py b/xklb/playback/torrents_info.py index 78d34098..e97bf7cc 100644 --- a/xklb/playback/torrents_info.py +++ b/xklb/playback/torrents_info.py @@ -185,6 +185,14 @@ def torrents_info(): "file_count": sum(len(t.files) for t in torrents) if args.verbose >= 1 else None, # a bit slow } ) + + categories = sorted( + categories, + key=lambda d: ( + d["state"].endswith(("downloading", "DL")), + iterables.safe_index(interesting_states, d["state"]), + ), + ) printing.table(iterables.list_dict_filter_bool(categories)) print() diff --git a/xklb/utils/iterables.py b/xklb/utils/iterables.py index c157296b..07024467 100644 --- a/xklb/utils/iterables.py +++ b/xklb/utils/iterables.py @@ -55,6 +55,15 @@ def safe_len(list_) -> Any | None: return len(str(list_)) +def safe_index(list_, value) -> int: + if not list_: + return 0 + try: + return list_.index(value) + except ValueError: + return -1 + + def get_all_lists(nested_dict): list_ = []