File: bar_items.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 (202 lines) | stat: -rw-r--r-- 6,898 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
# -*- coding: utf-8 -*-

# Copyright © 2018, 2019 Damir Jelić <poljar@termina.org.uk>
#
# Permission to use, copy, modify, and/or distribute this software for
# any purpose with or without fee is hereby granted, provided that the
# above copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
# RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
# CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

from __future__ import unicode_literals

from . import globals as G
from .globals import SERVERS, W
from .utf import utf8_decode


@utf8_decode
def matrix_bar_item_plugin(data, item, window, buffer, extra_info):
    # pylint: disable=unused-argument
    for server in SERVERS.values():
        if buffer in server.buffers.values() or buffer == server.server_buffer:
            return "matrix{color}/{color_fg}{name}".format(
                color=W.color("bar_delim"),
                color_fg=W.color("bar_fg"),
                name=server.name,
            )

    ptr_plugin = W.buffer_get_pointer(buffer, "plugin")
    name = W.plugin_get_name(ptr_plugin)

    return name


@utf8_decode
def matrix_bar_item_name(data, item, window, buffer, extra_info):
    # pylint: disable=unused-argument
    for server in SERVERS.values():
        if buffer in server.buffers.values():
            color = (
                "status_name_ssl"
                if server.ssl_context.check_hostname
                else "status_name"
            )

            room_buffer = server.find_room_from_ptr(buffer)
            room = room_buffer.room

            return "{color}{name}".format(
                color=W.color(color), name=room.display_name
            )

        if buffer == server.server_buffer:
            color = (
                "status_name_ssl"
                if server.ssl_context.check_hostname
                else "status_name"
            )

            return "{color}server{del_color}[{color}{name}{del_color}]".format(
                color=W.color(color),
                del_color=W.color("bar_delim"),
                name=server.name,
            )

    name = W.buffer_get_string(buffer, "name")

    return "{}{}".format(W.color("status_name"), name)


@utf8_decode
def matrix_bar_item_lag(data, item, window, buffer, extra_info):
    # pylint: disable=unused-argument
    for server in SERVERS.values():
        if buffer in server.buffers.values() or buffer == server.server_buffer:
            if server.lag >= G.CONFIG.network.lag_min_show:
                color = W.color("irc.color.item_lag_counting")
                if server.lag_done:
                    color = W.color("irc.color.item_lag_finished")

                lag = "{0:.3f}" if round(server.lag) < 1000 else "{0:.0f}"
                lag_string = "Lag: {color}{lag}{ncolor}".format(
                    lag=lag.format((server.lag / 1000)),
                    color=color,
                    ncolor=W.color("reset"),
                )
                return lag_string
            return ""

    return ""


@utf8_decode
def matrix_bar_item_buffer_modes(data, item, window, buffer, extra_info):
    # pylint: disable=unused-argument
    for server in SERVERS.values():
        if buffer in server.buffers.values():
            room_buffer = server.find_room_from_ptr(buffer)
            room = room_buffer.room
            modes = []

            if room.encrypted:
                modes.append(G.CONFIG.look.encrypted_room_sign)

            if (server.client
                    and server.client.room_contains_unverified(room.room_id)):
                modes.append(G.CONFIG.look.encryption_warning_sign)

            if not server.connected or not server.client.logged_in:
                modes.append(G.CONFIG.look.disconnect_sign)

            if room_buffer.backlog_pending or server.busy:
                modes.append(G.CONFIG.look.busy_sign)

            return "".join(modes)

    return ""


@utf8_decode
def matrix_bar_nicklist_count(data, item, window, buffer, extra_info):
    # pylint: disable=unused-argument
    color = W.color("status_nicklist_count")

    for server in SERVERS.values():
        if buffer in server.buffers.values():
            room_buffer = server.find_room_from_ptr(buffer)
            room = room_buffer.room
            return "{}{}".format(color, room.member_count)

    nicklist_enabled = bool(W.buffer_get_integer(buffer, "nicklist"))

    if nicklist_enabled:
        nick_count = W.buffer_get_integer(buffer, "nicklist_visible_count")
        return "{}{}".format(color, nick_count)

    return ""


@utf8_decode
def matrix_bar_typing_notices_cb(data, item, window, buffer, extra_info):
    """Update a status bar item showing users currently typing.
       This function is called by weechat every time a buffer is switched or
       W.bar_item_update(<item>) is explicitly called. The bar item shows
       currently typing users for the current buffer."""
    # pylint: disable=unused-argument
    for server in SERVERS.values():
        if buffer in server.buffers.values():
            room_buffer = server.find_room_from_ptr(buffer)
            room = room_buffer.room

            if room.typing_users:
                nicks = []

                for user_id in room.typing_users:
                    if user_id == room.own_user_id:
                        continue

                    nick = room_buffer.displayed_nicks.get(user_id, user_id)
                    nicks.append(nick)

                if not nicks:
                    return ""

                msg = "{}{}".format(
                    G.CONFIG.look.bar_item_typing_notice_prefix,
                    ", ".join(sorted(nicks))
                )

                max_len = G.CONFIG.look.max_typing_notice_item_length
                if len(msg) > max_len:
                    msg[:max_len - 3] + "..."

                return msg

            return ""

    return ""


def init_bar_items():
    W.bar_item_new("(extra)buffer_plugin", "matrix_bar_item_plugin", "")
    W.bar_item_new("(extra)buffer_name", "matrix_bar_item_name", "")
    W.bar_item_new("(extra)lag", "matrix_bar_item_lag", "")
    W.bar_item_new(
        "(extra)buffer_nicklist_count",
        "matrix_bar_nicklist_count",
        ""
    )
    W.bar_item_new(
        "(extra)matrix_typing_notice",
        "matrix_bar_typing_notices_cb",
        ""
    )
    W.bar_item_new("(extra)buffer_modes", "matrix_bar_item_buffer_modes", "")
    W.bar_item_new("(extra)matrix_modes", "matrix_bar_item_buffer_modes", "")