Skip to content

Commit

Permalink
torrent-info: fix categories sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
chapmanjacobd committed Dec 12, 2024
1 parent 400d8fc commit afaa195
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
4 changes: 3 additions & 1 deletion xklb/playback/media_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,9 @@ def media_printer(args, data, units=None, media_len=None) -> None:
for line in virtual_csv.readlines():
printing.pipe_print(line.strip())

elif "j" in print_args or consts.MOBILE_TERMINAL:
elif consts.MOBILE_TERMINAL:
printing.extended_view(media)
elif "j" in print_args:
print(json.dumps(media, indent=3))
elif "c" in print_args:
printing.write_csv_to_stdout(media)
Expand Down
10 changes: 9 additions & 1 deletion xklb/playback/torrents_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def torrents_info():
]

tbl = []
for state in interesting_states:
for state in {*interesting_states} - {"uploading", "downloading"}:
torrents = torrents_by_state.get(state)
if not torrents:
continue
Expand Down Expand Up @@ -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()

Expand Down
9 changes: 9 additions & 0 deletions xklb/utils/iterables.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_ = []

Expand Down
9 changes: 7 additions & 2 deletions xklb/utils/printing.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import csv, itertools, math, sys, textwrap, time
import csv, itertools, math, sys, textwrap, time, json
from collections.abc import Callable
from datetime import datetime, timezone

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


def table(tbl, **kwargs) -> None:
table_text = tabulate(tbl, tablefmt=consts.TABULATE_STYLE, headers="keys", showindex=False, **kwargs)
longest_line = max(len(s) for s in table_text.splitlines())
try:
print(tabulate(tbl, tablefmt=consts.TABULATE_STYLE, headers="keys", showindex=False, **kwargs))
if longest_line > consts.TERMINAL_SIZE.columns:
extended_view(tbl)
else:
print(table_text)
except BrokenPipeError:
sys.stdout = None
sys.exit(141)
Expand Down

0 comments on commit afaa195

Please sign in to comment.