Add a simple table printer and apply to lists

This commit is contained in:
Ivan Habunek 2023-03-30 11:28:23 +02:00
parent 3d7f7cfce3
commit 8624ddb175
No known key found for this signature in database
GPG key ID: CDBD63C43A30BB95
2 changed files with 38 additions and 15 deletions

View file

@ -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):

View file

@ -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: <green>\"{list_item['title']}\"</green>\t"
+ f"ID: <green>{list_item['id']}\t</green>"
+ f"Replies policy: <green>{replies_policy}</green>")
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}</{tag}>" 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):