Get terminal size instead of fixing it to 80

This commit is contained in:
Huy Ngo 2024-08-05 11:44:03 +07:00
parent 34c65f838a
commit c18181ed81

View file

@ -34,6 +34,9 @@ STYLE_TAG_PATTERN = re.compile(r"""
> # literal
""", re.X)
terminal_size = os.get_terminal_size()
TERM_WIDTH = terminal_size.columns
def colorize(message):
"""
@ -148,14 +151,14 @@ def print_instance(instance: Instance):
if instance.description:
for paragraph in re.split(r"[\r\n]+", instance.description.strip()):
paragraph = get_text(paragraph)
print_out(textwrap.fill(paragraph, width=80))
print_out(textwrap.fill(paragraph, width=TERM_WIDTH))
print_out()
if instance.rules:
print_out("Rules:")
for ordinal, rule in enumerate(instance.rules):
ordinal = f"{ordinal + 1}."
lines = textwrap.wrap(rule.text, 80 - len(ordinal))
lines = textwrap.wrap(rule.text, TERM_WIDTH - len(ordinal))
first = True
for line in lines:
if first:
@ -280,7 +283,7 @@ def print_search_results(results):
print_out("<yellow>Nothing found</yellow>")
def print_status(status: Status, width: int = 80, padding: int = 0):
def print_status(status: Status, width: int = TERM_WIDTH, padding: int = 0):
status_id = status.original.id
in_reply_to_id = status.in_reply_to_id
reblogged_by = status.account if status.reblog else None
@ -344,7 +347,7 @@ def print_status(status: Status, width: int = 80, padding: int = 0):
)
def print_html(text, width=80, padding=0):
def print_html(text, width=TERM_WIDTH, padding=0):
first = True
for paragraph in html_to_paragraphs(text):
if not first:
@ -381,7 +384,7 @@ def print_poll(poll: Poll):
print_out(poll_footer)
def print_timeline(items: Iterable[Status], width=80, padding=0):
def print_timeline(items: Iterable[Status], width=TERM_WIDTH, padding=0):
width -= padding
if padding:
first_paddings = "" * (padding - 1) + ""
@ -403,8 +406,8 @@ def print_tree(tree, depth=0):
paddings = "" * (depth - 1) + ""
else:
paddings = ""
print_status(tree['status'], 80-depth, padding=depth)
print_out(paddings + "" * (80 - depth))
print_status(tree['status'], TERM_WIDTH-depth, padding=depth)
print_out(paddings + "" * (TERM_WIDTH - depth))
for reply in tree['replies']:
print_tree(reply, depth+1)
@ -417,7 +420,7 @@ notification_msgs = {
}
def print_notification(notification: Notification, width=80):
def print_notification(notification: Notification, width=TERM_WIDTH):
account = f"{notification.account.display_name} @{notification.account.acct}"
msg = notification_msgs.get(notification.type)
if msg is None:
@ -429,7 +432,7 @@ def print_notification(notification: Notification, width=80):
print_status(notification.status, width)
def print_notifications(notifications: List[Notification], width=80):
def print_notifications(notifications: List[Notification], width=TERM_WIDTH):
for notification in notifications:
print_notification(notification)
print_out("" * width)