File: _weechat.py

package info (click to toggle)
weechat-matrix 0.3.0-3.3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 808 kB
  • sloc: python: 7,992; makefile: 27
file content (260 lines) | stat: -rw-r--r-- 6,134 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import datetime
import random
import string

WEECHAT_BASE_COLORS = {
    "black":        "0",
    "red":          "1",
    "green":        "2",
    "brown":        "3",
    "blue":         "4",
    "magenta":      "5",
    "cyan":         "6",
    "default":      "7",
    "gray":         "8",
    "lightred":     "9",
    "lightgreen":   "10",
    "yellow":       "11",
    "lightblue":    "12",
    "lightmagenta": "13",
    "lightcyan":    "14",
    "white":        "15"
}


class MockObject(object):
    pass

class MockConfig(object):
    config_template = {
        'debug_buffer': None,
        'debug_category': None,
        '_ptr': None,
        'read': None,
        'free': None,
        'page_up_hook': None,
        'color': {
            'error_message_bg': "",
            'error_message_fg': "",
            'quote_bg': "",
            'quote_fg': "",
            'unconfirmed_message_bg': "",
            'unconfirmed_message_fg': "",
            'untagged_code_bg': "",
            'untagged_code_fg': "",
        },
        'upload_buffer': {
            'display': None,
            'move_line_down': None,
            'move_line_up': None,
            'render': None,
        },
        'look': {
            'bar_item_typing_notice_prefix': None,
            'busy_sign': None,
            'code_block_margin': None,
            'code_blocks': None,
            'disconnect_sign': None,
            'encrypted_room_sign': None,
            'encryption_warning_sign': None,
            'max_typing_notice_item_length': None,
            'pygments_style': None,
            'redactions': None,
            'server_buffer': None,
            'new_channel_position': None,
            'markdown_input': True,
        },
        'network': {
            'debug_buffer': None,
            'debug_category': None,
            'debug_level': None,
            'fetch_backlog_on_pgup': None,
            'lag_min_show': None,
            'lag_reconnect': None,
            'lazy_load_room_users': None,
            'max_initial_sync_events': None,
            'max_nicklist_users': None,
            'print_unconfirmed_messages': None,
            'read_markers_conditions': None,
            'typing_notice_conditions': None,
            'autoreconnect_delay_growing': None,
            'autoreconnect_delay_max': None,
        },
    }

    def __init__(self):
        for category, options in MockConfig.config_template.items():
            if options:
                category_object = MockObject()
                for option, value in options.items():
                    setattr(category_object, option, value)
            else:
                category_object = options

            setattr(self, category, category_object)


def color(color_name):
    # type: (str) -> str
    # yapf: disable
    escape_codes = []
    reset_code = "0"

    def make_fg_color(color_code):
        return "38;5;{}".format(color_code)

    def make_bg_color(color_code):
        return "48;5;{}".format(color_code)

    attributes = {
        "bold":       "1",
        "-bold":      "21",
        "reverse":    "27",
        "-reverse":   "21",
        "italic":     "3",
        "-italic":    "23",
        "underline":  "4",
        "-underline": "24",
        "reset":      "0",
        "resetcolor": "39"
    }

    short_attributes = {
        "*": "1",
        "!": "27",
        "/": "3",
        "_": "4"
    }

    colors = color_name.split(",", 2)

    fg_color = colors.pop(0)

    bg_color = colors.pop(0) if colors else ""

    if fg_color in attributes:
        escape_codes.append(attributes[fg_color])
    else:
        chars = list(fg_color)

        for char in chars:
            if char in short_attributes:
                escape_codes.append(short_attributes[char])
            elif char == "|":
                reset_code = ""
            else:
                break

        stripped_color = fg_color.lstrip("*_|/!")

        if stripped_color in WEECHAT_BASE_COLORS:
            escape_codes.append(
                make_fg_color(WEECHAT_BASE_COLORS[stripped_color]))

        elif stripped_color.isdigit():
            num_color = int(stripped_color)
            if 0 <= num_color < 256:
                escape_codes.append(make_fg_color(stripped_color))

    if bg_color in WEECHAT_BASE_COLORS:
        escape_codes.append(make_bg_color(WEECHAT_BASE_COLORS[bg_color]))
    else:
        if bg_color.isdigit():
            num_color = int(bg_color)
            if 0 <= num_color < 256:
                escape_codes.append(make_bg_color(bg_color))

    escape_string = "\033[{}{}m".format(reset_code, ";".join(escape_codes))

    return escape_string


def prefix(prefix_string):
    prefix_to_symbol = {
        "error":   "=!=",
        "network": "--",
        "action":  "*",
        "join":    "-->",
        "quit":    "<--"
    }

    if prefix_string in prefix_to_symbol:
        return prefix_to_symbol[prefix]

    return ""


def prnt(_, message):
    print(message)


def prnt_date_tags(_, date, tags_string, data):
    message = "{} {} [{}]".format(
        datetime.datetime.fromtimestamp(date),
        data,
        tags_string
    )
    print(message)


def config_search_section(*_, **__):
    pass


def config_new_option(*_, **__):
    pass


def mkdir_home(*_, **__):
    return True


def info_get(info, *_):
    if info == "nick_color_name":
        return random.choice(list(WEECHAT_BASE_COLORS.keys()))

    return ""


def buffer_new(*_, **__):
    return "".join(
        random.choice(string.ascii_uppercase + string.digits) for _ in range(8)
    )


def buffer_set(*_, **__):
    return


def buffer_get_string(_ptr, property):
    if property == "localvar_type":
        return "channel"
    return ""


def buffer_get_integer(_ptr, property):
    return 0


def current_buffer():
    return 1


def nicklist_add_group(*_, **__):
    return


def nicklist_add_nick(*_, **__):
    return


def nicklist_remove_nick(*_, **__):
    return


def nicklist_search_nick(*args, **kwargs):
    return buffer_new(args, kwargs)


def string_remove_color(message, _):
    return message