Skip to content

Commit 3281045

Browse files
committed
torrent-info: fix categories sorting
1 parent 400d8fc commit 3281045

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

xklb/playback/torrents_info.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def torrents_info():
9292
]
9393

9494
tbl = []
95-
for state in interesting_states:
95+
for state in {*interesting_states} - {"uploading", "downloading"}:
9696
torrents = torrents_by_state.get(state)
9797
if not torrents:
9898
continue
@@ -185,6 +185,14 @@ def torrents_info():
185185
"file_count": sum(len(t.files) for t in torrents) if args.verbose >= 1 else None, # a bit slow
186186
}
187187
)
188+
189+
categories = sorted(
190+
categories,
191+
key=lambda d: (
192+
d["state"].endswith(("downloading", "DL")),
193+
iterables.safe_index(interesting_states, d["state"]),
194+
),
195+
)
188196
printing.table(iterables.list_dict_filter_bool(categories))
189197
print()
190198

xklb/utils/iterables.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ def safe_len(list_) -> Any | None:
5555
return len(str(list_))
5656

5757

58+
def safe_index(list_, value) -> int:
59+
if not list_:
60+
return 0
61+
try:
62+
return list_.index(value)
63+
except ValueError:
64+
return -1
65+
66+
5867
def get_all_lists(nested_dict):
5968
list_ = []
6069

xklb/utils/printing.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import csv, itertools, math, sys, textwrap, time
1+
import csv, itertools, math, sys, textwrap, time, json
22
from collections.abc import Callable
33
from datetime import datetime, timezone
44

@@ -21,8 +21,13 @@ def print_overwrite(*text, **kwargs):
2121

2222

2323
def table(tbl, **kwargs) -> None:
24+
table_text = tabulate(tbl, tablefmt=consts.TABULATE_STYLE, headers="keys", showindex=False, **kwargs)
25+
longest_line = max(len(s) for s in table_text.splitlines())
2426
try:
25-
print(tabulate(tbl, tablefmt=consts.TABULATE_STYLE, headers="keys", showindex=False, **kwargs))
27+
if longest_line > consts.TERMINAL_SIZE.columns:
28+
print(json.dumps(tbl, indent=3))
29+
else:
30+
print(table_text)
2631
except BrokenPipeError:
2732
sys.stdout = None
2833
sys.exit(141)

0 commit comments

Comments
 (0)