2019-09-05 22:35:29 +02:00
|
|
|
import os
|
2017-05-08 09:09:20 +02:00
|
|
|
import re
|
2019-09-05 22:35:29 +02:00
|
|
|
import sys
|
2022-11-22 21:27:04 +01:00
|
|
|
import textwrap
|
2017-04-19 14:47:30 +02:00
|
|
|
|
2022-11-21 08:15:49 +01:00
|
|
|
from toot.tui.utils import parse_datetime
|
2022-11-22 21:27:04 +01:00
|
|
|
from wcwidth import wcswidth
|
2018-06-07 10:27:11 +02:00
|
|
|
|
2022-11-22 21:27:04 +01:00
|
|
|
from toot.utils import get_text, parse_html
|
2019-02-14 16:53:58 +01:00
|
|
|
from toot.wcstring import wc_wrap
|
2019-02-14 15:47:40 +01:00
|
|
|
|
2017-12-29 14:26:40 +01:00
|
|
|
|
2022-12-02 07:40:47 +01:00
|
|
|
STYLES = {
|
|
|
|
'reset': '\033[0m',
|
|
|
|
'bold': '\033[1m',
|
|
|
|
'dim': '\033[2m',
|
|
|
|
'italic': '\033[3m',
|
|
|
|
'underline': '\033[4m',
|
|
|
|
'red': '\033[91m',
|
|
|
|
'green': '\033[92m',
|
|
|
|
'yellow': '\033[93m',
|
|
|
|
'blue': '\033[94m',
|
|
|
|
'magenta': '\033[95m',
|
|
|
|
'cyan': '\033[96m',
|
2017-05-08 09:09:20 +02:00
|
|
|
}
|
2017-04-19 14:47:30 +02:00
|
|
|
|
2022-12-02 07:40:47 +01:00
|
|
|
STYLE_TAG_PATTERN = re.compile(r"""
|
|
|
|
(?<!\\) # not preceeded by a backslash - allows escaping
|
|
|
|
< # literal
|
|
|
|
(/)? # optional closing - first group
|
|
|
|
(.*?) # style names - ungreedy - second group
|
|
|
|
> # literal
|
|
|
|
""", re.X)
|
2017-04-19 14:47:30 +02:00
|
|
|
|
|
|
|
|
2022-12-02 07:40:47 +01:00
|
|
|
def colorize(message):
|
|
|
|
"""
|
|
|
|
Replaces style tags in `message` with ANSI escape codes.
|
2017-04-19 14:47:30 +02:00
|
|
|
|
2022-12-02 07:40:47 +01:00
|
|
|
Markup is inspired by HTML, but you can use multiple words pre tag, e.g.:
|
2017-04-19 14:47:30 +02:00
|
|
|
|
2022-12-02 07:40:47 +01:00
|
|
|
<red bold>alert!</red bold> a thing happened
|
2017-04-19 14:47:30 +02:00
|
|
|
|
2022-12-02 07:40:47 +01:00
|
|
|
Empty closing tag will reset all styes:
|
2017-04-19 14:47:30 +02:00
|
|
|
|
2022-12-02 07:40:47 +01:00
|
|
|
<red bold>alert!</> a thing happened
|
2017-04-19 14:47:30 +02:00
|
|
|
|
2022-12-02 07:40:47 +01:00
|
|
|
Styles can be nested:
|
2017-04-19 14:47:30 +02:00
|
|
|
|
2022-12-02 07:40:47 +01:00
|
|
|
<red>red <underline>red and underline</underline> red</red>
|
|
|
|
"""
|
2017-04-19 14:47:30 +02:00
|
|
|
|
2022-12-02 07:40:47 +01:00
|
|
|
def _codes(styles):
|
|
|
|
for style in styles:
|
|
|
|
yield STYLES.get(style, "")
|
2017-04-19 14:47:30 +02:00
|
|
|
|
2022-12-02 07:40:47 +01:00
|
|
|
def _generator(message):
|
|
|
|
# A list is used instead of a set because we want to keep style order
|
|
|
|
# This allows nesting colors, e.g. "<blue>foo<red>bar</red>baz</blue>"
|
|
|
|
position = 0
|
|
|
|
active_styles = []
|
|
|
|
|
|
|
|
for match in re.finditer(STYLE_TAG_PATTERN, message):
|
|
|
|
is_closing = bool(match.group(1))
|
|
|
|
styles = match.group(2).strip().split()
|
|
|
|
|
|
|
|
start, end = match.span()
|
|
|
|
# Replace backslash for escaped <
|
|
|
|
yield message[position:start].replace("\\<", "<")
|
|
|
|
|
|
|
|
if is_closing:
|
|
|
|
yield STYLES["reset"]
|
|
|
|
|
|
|
|
# Empty closing tag resets all styles
|
|
|
|
if styles == []:
|
|
|
|
active_styles = []
|
|
|
|
else:
|
|
|
|
active_styles = [s for s in active_styles if s not in styles]
|
|
|
|
yield from _codes(active_styles)
|
|
|
|
else:
|
|
|
|
active_styles = active_styles + styles
|
|
|
|
yield from _codes(styles)
|
|
|
|
|
|
|
|
position = end
|
|
|
|
|
|
|
|
if position == 0:
|
|
|
|
# Nothing matched, yield the original string
|
|
|
|
yield message
|
|
|
|
else:
|
|
|
|
# Yield the remaining fragment
|
|
|
|
yield message[position:]
|
|
|
|
# Reset styles at the end to prevent leaking
|
|
|
|
yield STYLES["reset"]
|
|
|
|
|
|
|
|
return "".join(_generator(message))
|
|
|
|
|
|
|
|
|
|
|
|
def strip_tags(message):
|
|
|
|
return re.sub(STYLE_TAG_PATTERN, "", message)
|
2017-04-19 14:47:30 +02:00
|
|
|
|
|
|
|
|
2019-09-05 22:35:29 +02:00
|
|
|
def use_ansi_color():
|
|
|
|
"""Returns True if ANSI color codes should be used."""
|
|
|
|
|
|
|
|
# Windows doesn't support color unless ansicon is installed
|
|
|
|
# See: http://adoxa.altervista.org/ansicon/
|
|
|
|
if sys.platform == 'win32' and 'ANSICON' not in os.environ:
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Don't show color if stdout is not a tty, e.g. if output is piped on
|
|
|
|
if not sys.stdout.isatty():
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Don't show color if explicitly specified in options
|
|
|
|
if "--no-color" in sys.argv:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
USE_ANSI_COLOR = use_ansi_color()
|
|
|
|
|
2018-06-15 09:02:19 +02:00
|
|
|
QUIET = "--quiet" in sys.argv
|
2017-04-19 14:47:30 +02:00
|
|
|
|
2017-05-08 09:09:20 +02:00
|
|
|
|
|
|
|
def print_out(*args, **kwargs):
|
2018-06-15 09:02:19 +02:00
|
|
|
if not QUIET:
|
|
|
|
args = [colorize(a) if USE_ANSI_COLOR else strip_tags(a) for a in args]
|
|
|
|
print(*args, **kwargs)
|
2017-05-08 09:09:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
def print_err(*args, **kwargs):
|
2022-12-12 12:46:24 +01:00
|
|
|
args = [f"<red>{a}</red>" for a in args]
|
2017-05-08 09:09:20 +02:00
|
|
|
args = [colorize(a) if USE_ANSI_COLOR else strip_tags(a) for a in args]
|
|
|
|
print(*args, file=sys.stderr, **kwargs)
|
2017-12-29 14:26:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
def print_instance(instance):
|
2022-12-12 12:46:24 +01:00
|
|
|
print_out(f"<green>{instance['title']}</green>")
|
|
|
|
print_out(f"<blue>{instance['uri']}</blue>")
|
|
|
|
print_out(f"running Mastodon {instance['version']}")
|
2022-11-22 21:27:04 +01:00
|
|
|
print_out()
|
|
|
|
|
|
|
|
description = instance.get("description")
|
|
|
|
if description:
|
|
|
|
for paragraph in re.split(r"[\r\n]+", description.strip()):
|
|
|
|
paragraph = get_text(paragraph)
|
|
|
|
print_out(textwrap.fill(paragraph, width=80))
|
|
|
|
print_out()
|
|
|
|
|
|
|
|
rules = instance.get("rules")
|
|
|
|
if rules:
|
|
|
|
print_out("Rules:")
|
|
|
|
for ordinal, rule in enumerate(rules):
|
|
|
|
ordinal = f"{ordinal + 1}."
|
|
|
|
lines = textwrap.wrap(rule["text"], 80 - len(ordinal))
|
|
|
|
first = True
|
|
|
|
for line in lines:
|
|
|
|
if first:
|
|
|
|
print_out(f"{ordinal} {line}")
|
|
|
|
first = False
|
|
|
|
else:
|
|
|
|
print_out(f"{' ' * len(ordinal)} {line}")
|
2017-12-29 14:42:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
def print_account(account):
|
2022-12-12 12:46:24 +01:00
|
|
|
print_out(f"<green>@{account['acct']}</green> {account['display_name']}")
|
2017-12-29 14:42:51 +01:00
|
|
|
|
2022-12-16 12:56:51 +01:00
|
|
|
if account["note"]:
|
2017-12-29 14:42:51 +01:00
|
|
|
print_out("")
|
2022-12-16 12:56:51 +01:00
|
|
|
print_html(account["note"])
|
2017-12-29 14:42:51 +01:00
|
|
|
|
|
|
|
print_out("")
|
2022-12-12 12:46:24 +01:00
|
|
|
print_out(f"ID: <green>{account['id']}</green>")
|
|
|
|
print_out(f"Since: <green>{account['created_at'][:10]}</green>")
|
2017-12-29 14:42:51 +01:00
|
|
|
print_out("")
|
2022-12-12 12:46:24 +01:00
|
|
|
print_out(f"Followers: <yellow>{account['followers_count']}</yellow>")
|
|
|
|
print_out(f"Following: <yellow>{account['following_count']}</yellow>")
|
|
|
|
print_out(f"Statuses: <yellow>{account['statuses_count']}</yellow>")
|
2022-12-16 12:56:51 +01:00
|
|
|
|
|
|
|
if account["fields"]:
|
|
|
|
for field in account["fields"]:
|
|
|
|
name = field["name"].title()
|
|
|
|
print_out(f'\n<yellow>{name}</yellow>:')
|
|
|
|
print_html(field["value"])
|
|
|
|
|
2017-12-29 14:42:51 +01:00
|
|
|
print_out("")
|
2022-12-12 12:46:24 +01:00
|
|
|
print_out(account["url"])
|
2017-12-29 14:42:51 +01:00
|
|
|
|
|
|
|
|
2019-02-14 16:53:58 +01:00
|
|
|
HASHTAG_PATTERN = re.compile(r'(?<!\w)(#\w+)\b')
|
|
|
|
|
|
|
|
|
|
|
|
def highlight_hashtags(line):
|
|
|
|
return re.sub(HASHTAG_PATTERN, '<cyan>\\1</cyan>', line)
|
|
|
|
|
2022-11-22 21:27:21 +01:00
|
|
|
|
2022-11-17 22:45:51 +01:00
|
|
|
def print_acct_list(accounts):
|
|
|
|
for account in accounts:
|
2022-12-12 12:46:24 +01:00
|
|
|
print_out(f"* <green>@{account['acct']}</green> {account['display_name']}")
|
2019-02-14 16:53:58 +01:00
|
|
|
|
2022-11-22 21:27:21 +01:00
|
|
|
|
2022-12-20 18:55:32 +01:00
|
|
|
def print_tag_list(tags):
|
2022-12-31 09:14:28 +01:00
|
|
|
if tags:
|
|
|
|
for tag in tags:
|
2023-01-02 10:11:19 +01:00
|
|
|
print_out(f"* <green>#{tag['name']}\t</green>{tag['url']}")
|
2022-12-31 09:14:28 +01:00
|
|
|
else:
|
|
|
|
print_out("You're not following any hashtags.")
|
2022-12-20 18:55:32 +01:00
|
|
|
|
|
|
|
|
2017-12-29 14:42:51 +01:00
|
|
|
def print_search_results(results):
|
|
|
|
accounts = results['accounts']
|
|
|
|
hashtags = results['hashtags']
|
|
|
|
|
|
|
|
if accounts:
|
|
|
|
print_out("\nAccounts:")
|
2022-11-17 22:45:51 +01:00
|
|
|
print_acct_list(accounts)
|
2017-12-29 14:42:51 +01:00
|
|
|
|
|
|
|
if hashtags:
|
|
|
|
print_out("\nHashtags:")
|
2022-12-12 12:46:24 +01:00
|
|
|
print_out(", ".join([f"<green>#{t['name']}</green>" for t in hashtags]))
|
2017-12-29 14:42:51 +01:00
|
|
|
|
|
|
|
if not accounts and not hashtags:
|
|
|
|
print_out("<yellow>Nothing found</yellow>")
|
2018-06-07 10:27:11 +02:00
|
|
|
|
|
|
|
|
2019-02-14 16:53:58 +01:00
|
|
|
def print_status(status, width):
|
|
|
|
reblog = status['reblog']
|
|
|
|
content = reblog['content'] if reblog else status['content']
|
|
|
|
media_attachments = reblog['media_attachments'] if reblog else status['media_attachments']
|
|
|
|
in_reply_to = status['in_reply_to_id']
|
2022-11-29 10:08:21 +01:00
|
|
|
poll = reblog.get('poll') if reblog else status.get('poll')
|
2018-06-07 10:27:11 +02:00
|
|
|
|
2022-11-21 08:15:49 +01:00
|
|
|
time = parse_datetime(status['created_at'])
|
2022-11-06 05:31:49 +01:00
|
|
|
time = time.strftime('%Y-%m-%d %H:%M %Z')
|
2019-01-19 18:38:17 +01:00
|
|
|
|
2019-02-15 13:26:22 +01:00
|
|
|
username = "@" + status['account']['acct']
|
2022-12-12 12:46:24 +01:00
|
|
|
spacing = width - wcswidth(username) - wcswidth(time) - 2
|
2019-01-19 18:38:17 +01:00
|
|
|
|
2019-02-14 16:53:58 +01:00
|
|
|
display_name = status['account']['display_name']
|
|
|
|
if display_name:
|
|
|
|
spacing -= wcswidth(display_name) + 1
|
2018-06-07 10:27:11 +02:00
|
|
|
|
2022-12-12 12:46:24 +01:00
|
|
|
print_out(
|
|
|
|
f"<green>{display_name}</green>" if display_name else "",
|
|
|
|
f"<blue>{username}</blue>",
|
2019-02-14 16:53:58 +01:00
|
|
|
" " * spacing,
|
2022-12-12 12:46:24 +01:00
|
|
|
f"<yellow>{time}</yellow>",
|
|
|
|
)
|
2018-06-07 10:27:11 +02:00
|
|
|
|
2022-12-16 12:56:51 +01:00
|
|
|
print_out("")
|
|
|
|
print_html(content, width)
|
2019-02-14 16:53:58 +01:00
|
|
|
|
|
|
|
if media_attachments:
|
|
|
|
print_out("\nMedia:")
|
|
|
|
for attachment in media_attachments:
|
2022-11-30 08:55:46 +01:00
|
|
|
url = attachment["url"]
|
2019-02-14 16:53:58 +01:00
|
|
|
for line in wc_wrap(url, width):
|
|
|
|
print_out(line)
|
|
|
|
|
2022-11-28 03:27:02 +01:00
|
|
|
if poll:
|
2022-11-29 09:20:00 +01:00
|
|
|
print_poll(poll)
|
2022-11-28 03:27:02 +01:00
|
|
|
|
2022-12-12 12:46:24 +01:00
|
|
|
print_out()
|
|
|
|
print_out(
|
|
|
|
f"ID <yellow>{status['id']}</yellow> ",
|
|
|
|
f"↲ In reply to <yellow>{in_reply_to}</yellow> " if in_reply_to else "",
|
|
|
|
f"↻ Reblogged <blue>@{reblog['account']['acct']}</blue> " if reblog else "",
|
|
|
|
)
|
2022-11-28 03:27:02 +01:00
|
|
|
|
|
|
|
|
2022-12-16 12:56:51 +01:00
|
|
|
def print_html(text, width=80):
|
|
|
|
first = True
|
|
|
|
for paragraph in parse_html(text):
|
|
|
|
if not first:
|
|
|
|
print_out("")
|
|
|
|
for line in paragraph:
|
|
|
|
for subline in wc_wrap(line, width):
|
|
|
|
print_out(highlight_hashtags(subline))
|
|
|
|
first = False
|
|
|
|
|
|
|
|
|
2022-11-29 09:20:00 +01:00
|
|
|
def print_poll(poll):
|
|
|
|
print_out()
|
|
|
|
for idx, option in enumerate(poll["options"]):
|
|
|
|
perc = (round(100 * option["votes_count"] / poll["votes_count"])
|
|
|
|
if poll["votes_count"] else 0)
|
2022-11-28 03:27:02 +01:00
|
|
|
|
2022-11-29 09:20:00 +01:00
|
|
|
if poll["voted"] and poll["own_votes"] and idx in poll["own_votes"]:
|
|
|
|
voted_for = " <yellow>✓</yellow>"
|
|
|
|
else:
|
|
|
|
voted_for = ""
|
2022-11-28 03:27:02 +01:00
|
|
|
|
2022-11-29 09:20:00 +01:00
|
|
|
print_out(f'{option["title"]} - {perc}% {voted_for}')
|
2022-11-28 03:27:02 +01:00
|
|
|
|
2022-11-29 09:20:00 +01:00
|
|
|
poll_footer = f'Poll · {poll["votes_count"]} votes'
|
2022-11-28 03:27:02 +01:00
|
|
|
|
2022-11-29 09:20:00 +01:00
|
|
|
if poll["expired"]:
|
|
|
|
poll_footer += " · Closed"
|
2022-11-28 03:27:02 +01:00
|
|
|
|
2022-11-29 09:20:00 +01:00
|
|
|
if poll["expires_at"]:
|
|
|
|
expires_at = parse_datetime(poll["expires_at"]).strftime("%Y-%m-%d %H:%M")
|
|
|
|
poll_footer += f" · Closes on {expires_at}"
|
|
|
|
|
2022-12-12 12:46:24 +01:00
|
|
|
print_out()
|
|
|
|
print_out(poll_footer)
|
2019-02-14 16:53:58 +01:00
|
|
|
|
|
|
|
|
|
|
|
def print_timeline(items, width=100):
|
|
|
|
print_out("─" * width)
|
2018-06-07 10:27:11 +02:00
|
|
|
for item in items:
|
2019-02-14 16:53:58 +01:00
|
|
|
print_status(item, width)
|
|
|
|
print_out("─" * width)
|
2019-04-16 14:09:54 +02:00
|
|
|
|
|
|
|
|
|
|
|
notification_msgs = {
|
|
|
|
"follow": "{account} now follows you",
|
|
|
|
"mention": "{account} mentioned you in",
|
|
|
|
"reblog": "{account} reblogged your status",
|
|
|
|
"favourite": "{account} favourited your status",
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def print_notification(notification, width=100):
|
|
|
|
account = "{display_name} @{acct}".format(**notification["account"])
|
|
|
|
msg = notification_msgs.get(notification["type"])
|
|
|
|
if msg is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
print_out("─" * width)
|
|
|
|
print_out(msg.format(account=account))
|
|
|
|
status = notification.get("status")
|
|
|
|
if status is not None:
|
|
|
|
print_status(status, width)
|
|
|
|
|
|
|
|
|
|
|
|
def print_notifications(notifications, width=100):
|
|
|
|
for notification in notifications:
|
|
|
|
print_notification(notification)
|
|
|
|
print_out("─" * width)
|