mirror of
https://github.com/ihabunek/toot.git
synced 2024-11-03 04:17:21 -05:00
Add a simple table printer and apply to lists
This commit is contained in:
parent
3d7f7cfce3
commit
8624ddb175
@ -7,9 +7,9 @@ from time import sleep, time
|
|||||||
from toot import api, config, __version__
|
from toot import api, config, __version__
|
||||||
from toot.auth import login_interactive, login_browser_interactive, create_app_interactive
|
from toot.auth import login_interactive, login_browser_interactive, create_app_interactive
|
||||||
from toot.exceptions import ApiError, ConsoleError
|
from toot.exceptions import ApiError, ConsoleError
|
||||||
from toot.output import (print_out, print_instance, print_account, print_acct_list,
|
from toot.output import (print_lists, print_out, print_instance, print_account, print_acct_list,
|
||||||
print_search_results, print_timeline, print_notifications,
|
print_search_results, print_table, print_timeline, print_notifications,
|
||||||
print_tag_list, print_list_list, print_list_accounts)
|
print_tag_list, print_list_accounts)
|
||||||
from toot.tui.utils import parse_datetime
|
from toot.tui.utils import parse_datetime
|
||||||
from toot.utils import args_get_instance, delete_tmp_status_file, editor_input, multiline_input, EOF_KEY
|
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):
|
def lists(app, user, args):
|
||||||
response = api.get_lists(app, user)
|
lists = api.get_lists(app, user)
|
||||||
print_list_list(response)
|
|
||||||
|
if lists:
|
||||||
|
print_lists(lists)
|
||||||
|
else:
|
||||||
|
print_out("You have no lists defined.")
|
||||||
|
|
||||||
|
|
||||||
def list_accounts(app, user, args):
|
def list_accounts(app, user, args):
|
||||||
|
@ -3,9 +3,10 @@ import re
|
|||||||
import sys
|
import sys
|
||||||
import textwrap
|
import textwrap
|
||||||
|
|
||||||
from toot.tui.utils import parse_datetime
|
from typing import List
|
||||||
from wcwidth import wcswidth
|
from wcwidth import wcswidth
|
||||||
|
|
||||||
|
from toot.tui.utils import parse_datetime
|
||||||
from toot.utils import get_text, parse_html
|
from toot.utils import get_text, parse_html
|
||||||
from toot.wcstring import wc_wrap
|
from toot.wcstring import wc_wrap
|
||||||
|
|
||||||
@ -210,15 +211,33 @@ def print_tag_list(tags):
|
|||||||
print_out("You're not following any hashtags.")
|
print_out("You're not following any hashtags.")
|
||||||
|
|
||||||
|
|
||||||
def print_list_list(lists):
|
def print_lists(lists):
|
||||||
if lists:
|
headers = ["ID", "Title", "Replies"]
|
||||||
for list_item in lists:
|
data = [[lst["id"], lst["title"], lst["replies_policy"]] for lst in lists]
|
||||||
replies_policy = list_item['replies_policy'] if list_item['replies_policy'] else ''
|
print_table(headers, data)
|
||||||
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>")
|
def print_table(headers: List[str], data: List[List[str]]):
|
||||||
else:
|
widths = [[len(cell) for cell in row] for row in data + [headers]]
|
||||||
print_out("You have no lists defined.")
|
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):
|
def print_list_accounts(accounts):
|
||||||
|
Loading…
Reference in New Issue
Block a user