output.py - toot - Unnamed repository; edit this file 'description' to name the repository.
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) LICENSE
---
output.py (2949B)
---
1 # -*- coding: utf-8 -*-
2
3 import sys
4 import re
5
6 from textwrap import wrap
7 from toot.utils import format_content, get_text
8
9 START_CODES = {
10 'red': '\033[31m',
11 'green': '\033[32m',
12 'yellow': '\033[33m',
13 'blue': '\033[34m',
14 'magenta': '\033[35m',
15 'cyan': '\033[36m',
16 }
17
18 END_CODE = '\033[0m'
19
20 START_PATTERN = "<(" + "|".join(START_CODES.keys()) + ")>"
21
22 END_PATTERN = "</(" + "|".join(START_CODES.keys()) + ")>"
23
24
25 def start_code(match):
26 name = match.group(1)
27 return START_CODES[name]
28
29
30 def colorize(text):
31 text = re.sub(START_PATTERN, start_code, text)
32 text = re.sub(END_PATTERN, END_CODE, text)
33
34 return text
35
36
37 def strip_tags(text):
38 text = re.sub(START_PATTERN, '', text)
39 text = re.sub(END_PATTERN, '', text)
40
41 return text
42
43
44 USE_ANSI_COLOR = "--no-color" not in sys.argv
45
46
47 def print_out(*args, **kwargs):
48 args = [colorize(a) if USE_ANSI_COLOR else strip_tags(a) for a in args]
49 print(*args, **kwargs)
50
51
52 def print_err(*args, **kwargs):
53 args = ["<red>{}</red>".format(a) for a in args]
54 args = [colorize(a) if USE_ANSI_COLOR else strip_tags(a) for a in args]
55 print(*args, file=sys.stderr, **kwargs)
56
57
58 def print_instance(instance):
59 print_out("<green>{}</green>".format(instance['title']))
60 print_out("<blue>{}</blue>".format(instance['uri']))
61 print_out("running Mastodon {}".format(instance['version']))
62 print_out("")
63
64 description = instance['description'].strip()
65 if not description:
66 return
67
68 lines = [line.strip() for line in format_content(description) if line.strip()]
69 for line in lines:
70 for l in wrap(line.strip()):
71 print(l)
72 print()
73
74
75 def print_account(account):
76 print_out("<green>@{}</green> {}".format(account['acct'], account['display_name']))
77
78 note = get_text(account['note'])
79
80 if note:
81 print_out("")
82 print_out("\n".join(wrap(note)))
83
84 print_out("")
85 print_out("ID: <green>{}</green>".format(account['id']))
86 print_out("Since: <green>{}</green>".format(account['created_at'][:19].replace('T', ' @ ')))
87 print_out("")
88 print_out("Followers: <yellow>{}</yellow>".format(account['followers_count']))
89 print_out("Following: <yellow>{}</yellow>".format(account['following_count']))
90 print_out("Statuses: <yellow>{}</yellow>".format(account['statuses_count']))
91 print_out("")
92 print_out(account['url'])
93
94
95 def print_search_results(results):
96 accounts = results['accounts']
97 hashtags = results['hashtags']
98
99 if accounts:
100 print_out("\nAccounts:")
101 for account in accounts:
102 print_out("* <green>@{}</green> {}".format(
103 account['acct'],
104 account['display_name']
105 ))
106
107 if hashtags:
108 print_out("\nHashtags:")
109 print_out(", ".join(["<green>#{}</green>".format(t) for t in hashtags]))
110
111 if not accounts and not hashtags:
112 print_out("<yellow>Nothing found</yellow>")