summaryrefslogtreecommitdiff
path: root/weechat/python/micmot.py
blob: d6965b13353e8fd3136debfdd63ee91a5869705f (plain)
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
#!/usr/bin/python3
#
# Weechat script to display buffer actitity in tmux pane name.
#
# A workaround for: https://github.com/weechat/weechat/issues/741
#
# Ideally weechat should support multiple hotlists, to make it possible to
# logically separate different chats into groups. Since such a feature is
# missing, and implementing it would be non-trivial, this script helps if
# running multiple weechat instances within a tmux. It abuses the ability to
# rename pane to make them double as channel activity indicators.
#
# Requires something like: `F=~/.weechat/micmot-fifo; mkfifo $F; tail -f $F &`
#
# Also requires ":set-option allow-rename on" in the tmux buffer.

import os
import re
import weechat # type: ignore

TMUX_FIFO = os.environ.get('WEECHAT_TMUX_FIFO',
    os.environ['HOME'] + '/.weechat/micmot-fifo')

PANE_NAME = os.environ.get('WEECHAT_TMUX_PANE', 'weechat')
PANE_MAXLEN = 30

weechat.register(
    "micmot",
    "|cos|",
    "0.1.0",
    "MIT",
    "Abusing tmux pane names for displaying channel activity.",
    "",
    ""
)

def set_pane_name(name):
    with open(TMUX_FIFO,'a') as f:
        f.write("\x1bk%s\x1b\\" % name)

def highlight_pane():
    with open(TMUX_FIFO,'a') as f:
        f.write("\x07")

def set_pane_title(name):
    with open(TMUX_FIFO, 'a') as f:
        f.write("\x1b]2;%s\x1b\\" % name)

def hotlist_cb(a, b, c):
    # https://weechat.org/files/doc/stable/weechat_user.en.html#notify_levels
    tmux = ""
    infolist = weechat.infolist_get("hotlist", "", "")
    while weechat.infolist_next(infolist) != 0:
        buffer_name = weechat.infolist_string(infolist, "buffer_name")
        plugin_name = weechat.infolist_string(infolist, "plugin_name")
        needle_pointer = weechat.buffer_search(plugin_name, buffer_name)
        if needle_pointer:
            short = weechat.buffer_get_string(needle_pointer, "short_name")
            count_01 = weechat.infolist_integer(infolist, "count_01")
            count_02 = weechat.infolist_integer(infolist, "count_02")
            count_03 = weechat.infolist_integer(infolist, "count_03")
            if count_01 > 0 or count_02 > 0 or count_03 > 0:
                if tmux:
                    tmux = "{},{}".format(tmux,short)
                else:
                    tmux = "{}".format(short)
        else:
            weechat.prnt("", "No buffer matching {} for plugin {}."
                .format(buffer_name, plugin_name))

    if tmux:
        set_pane_name("[{}]".format(
            re.sub(r'[^.,#0-9A-Za-z-]+', '', tmux[:PANE_MAXLEN])))
        highlight_pane()
    else:
        set_pane_name(PANE_NAME)

    weechat.infolist_free(infolist)
    return weechat.WEECHAT_RC_OK

weechat.prnt("", "micmot " + PANE_NAME + TMUX_FIFO)
set_pane_name(PANE_NAME)
set_pane_title(PANE_NAME)
weechat.hook_signal("hotlist_changed", "hotlist_cb", "")