diff --git a/witchie/output.py b/witchie/output.py index 6fa0583..da8d66a 100644 --- a/witchie/output.py +++ b/witchie/output.py @@ -35,12 +35,15 @@ STYLE_TAG_PATTERN = re.compile(r""" > # literal """, re.X) -TERM_WIDTH = os.getenv('TERM_WIDTH') -try: - TERM_WIDTH = int(TERM_WIDTH) -except: - terminal_size = shutil.get_terminal_size() - TERM_WIDTH = terminal_size.columns + +def get_term_width(): + term_width = os.getenv('TERM_WIDTH') + try: + term_width = int(term_width) + except: + terminal_size = shutil.get_terminal_size() + term_width = terminal_size.columns + return term_width def colorize(message): @@ -157,14 +160,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=TERM_WIDTH)) + print_out(textwrap.fill(paragraph, width=get_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, TERM_WIDTH - len(ordinal)) + lines = textwrap.wrap(rule.text, get_term_width() - len(ordinal)) first = True for line in lines: if first: @@ -283,7 +286,7 @@ def print_search_results(results): print_out("Nothing found") -def print_status(status: Status, width: int = TERM_WIDTH, padding: int = 0): +def print_status(status: Status, width: int = get_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 @@ -347,7 +350,7 @@ def print_status(status: Status, width: int = TERM_WIDTH, padding: int = 0): ) -def print_html(text, width=TERM_WIDTH, padding=0): +def print_html(text, width=get_term_width(), padding=0): first = True for paragraph in html_to_paragraphs(text): if not first: @@ -384,7 +387,8 @@ def print_poll(poll: Poll): print_out(poll_footer) -def print_timeline(items: Iterable[Status], width=TERM_WIDTH, padding=0): +def print_timeline(items: Iterable[Status], padding=0): + width = get_term_width() width -= padding if padding: first_paddings = "│" * (padding - 1) + "┌" @@ -406,8 +410,8 @@ def print_tree(tree, depth=0): paddings = "│" * (depth - 1) + "└" else: paddings = "" - print_status(tree['status'], TERM_WIDTH - depth, padding=depth) - print_out(paddings + "─" * (TERM_WIDTH - depth)) + print_status(tree['status'], get_term_width() - depth, padding=depth) + print_out(paddings + "─" * (get_term_width() - depth)) for reply in tree['replies']: print_tree(reply, depth + 1) @@ -420,7 +424,8 @@ notification_msgs = { } -def print_notification(notification: Notification, width=TERM_WIDTH): +def print_notification(notification: Notification): + width = get_term_width() account = f"{notification.account.display_name} @{notification.account.acct}" msg = notification_msgs.get(notification.type) if msg is None: @@ -432,7 +437,8 @@ def print_notification(notification: Notification, width=TERM_WIDTH): print_status(notification.status, width) -def print_notifications(notifications: List[Notification], width=TERM_WIDTH): +def print_notifications(notifications: List[Notification]): + width = get_term_width() for notification in notifications: print_notification(notification) print_out("─" * width) diff --git a/witchie/utils/__init__.py b/witchie/utils/__init__.py index 9ed471f..0f17304 100644 --- a/witchie/utils/__init__.py +++ b/witchie/utils/__init__.py @@ -58,6 +58,8 @@ def get_text(element, links, images): return f'{element.text}' if element.name == 'u': return f'{element.text}' + if element.name == 'br': + return '\n' for a in element.find_all('a'): soup = BeautifulSoup("", "html.parser") text = a.text @@ -97,6 +99,11 @@ def get_text(element, links, images): new_tag = soup.new_tag('span') new_tag.string = '<strikethrough>' + a.text + '</strikethrough>' strike.replace_with(new_tag) + for underline in element.find_all('br'): + soup = BeautifulSoup("", "html.parser") + new_tag = soup.new_tag('span') + new_tag.string = '\n' + underline.replace_with(new_tag) text = element.get_text() text = text.replace('<', '\\<') text = text.replace('<', '<')