diff --git a/toot/commands.py b/toot/commands.py index a2879e9..ce34e58 100644 --- a/toot/commands.py +++ b/toot/commands.py @@ -7,9 +7,9 @@ from time import sleep, time from toot import api, config, __version__ from toot.auth import login_interactive, login_browser_interactive, create_app_interactive from toot.exceptions import ApiError, ConsoleError -from toot.output import (print_out, print_instance, print_account, print_acct_list, - print_search_results, print_timeline, print_notifications, - print_tag_list, print_list_list, print_list_accounts) +from toot.output import (print_lists, print_out, print_instance, print_account, print_acct_list, + print_search_results, print_table, print_timeline, print_notifications, + print_tag_list, print_list_accounts) from toot.tui.utils import parse_datetime from toot.utils import args_get_instance, delete_tmp_status_file, editor_input, multiline_input, EOF_KEY @@ -425,8 +425,12 @@ def tags_followed(app, user, args): def lists(app, user, args): - response = api.get_lists(app, user) - print_list_list(response) + lists = api.get_lists(app, user) + + if lists: + print_lists(lists) + else: + print_out("You have no lists defined.") def list_accounts(app, user, args): diff --git a/toot/output.py b/toot/output.py index 25f7252..8015b7b 100644 --- a/toot/output.py +++ b/toot/output.py @@ -3,9 +3,10 @@ import re import sys import textwrap -from toot.tui.utils import parse_datetime +from typing import List from wcwidth import wcswidth +from toot.tui.utils import parse_datetime from toot.utils import get_text, parse_html from toot.wcstring import wc_wrap @@ -210,15 +211,33 @@ def print_tag_list(tags): print_out("You're not following any hashtags.") -def print_list_list(lists): - if lists: - for list_item in lists: - replies_policy = list_item['replies_policy'] if list_item['replies_policy'] else '' - print_out(f"Title: \"{list_item['title']}\"\t" - + f"ID: {list_item['id']}\t" - + f"Replies policy: {replies_policy}") - else: - print_out("You have no lists defined.") +def print_lists(lists): + headers = ["ID", "Title", "Replies"] + data = [[lst["id"], lst["title"], lst["replies_policy"]] for lst in lists] + print_table(headers, data) + + +def print_table(headers: List[str], data: List[List[str]]): + widths = [[len(cell) for cell in row] for row in data + [headers]] + widths = [max(width) for width in zip(*widths)] + + def style(string, tag): + return f"<{tag}>{string}" if tag else string + + def print_row(row, tag=None): + for idx, cell in enumerate(row): + width = widths[idx] + print_out(style(cell.ljust(width), tag), end="") + print_out(" ", end="") + print_out() + + underlines = ["-" * width for width in widths] + + print_row(headers, "bold") + print_row(underlines, "dim") + + for row in data: + print_row(row) def print_list_accounts(accounts):