Skip to content

Commit 36469fe

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

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

xklb/playback/media_printer.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,9 @@ def media_printer(args, data, units=None, media_len=None) -> None:
244244
for line in virtual_csv.readlines():
245245
printing.pipe_print(line.strip())
246246

247-
elif "j" in print_args or consts.MOBILE_TERMINAL:
247+
elif consts.MOBILE_TERMINAL:
248+
printing.extended_view(media)
249+
elif "j" in print_args:
248250
print(json.dumps(media, indent=3))
249251
elif "c" in print_args:
250252
printing.write_csv_to_stdout(media)

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: 10 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,16 @@ 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+
if not table_text:
26+
return
27+
28+
longest_line = max(len(s) for s in table_text.splitlines())
2429
try:
25-
print(tabulate(tbl, tablefmt=consts.TABULATE_STYLE, headers="keys", showindex=False, **kwargs))
30+
if longest_line > consts.TERMINAL_SIZE.columns:
31+
extended_view(tbl)
32+
else:
33+
print(table_text)
2634
except BrokenPipeError:
2735
sys.stdout = None
2836
sys.exit(141)

0 commit comments

Comments
 (0)