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
|
# Copyright (c) 2023 Teddy Wing
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
use strict;
use warnings;
use IO::Socket::INET;
weechat::register(
'activity_bar',
'Teddy Wing',
'1.2',
'GPL-3.0-or-later',
'Monitor activity in all buffers and update AnyBar',
'shutdown',
''
);
# Flush socket immediately.
$| = 1;
my $socket = IO::Socket::INET->new(
PeerAddr => 'localhost',
PeerPort => '1738',
Proto => 'udp',
) or die "error: can't create socket: $!";
sub shutdown {
$socket->close();
}
# Map colours to priority levels. Higher number is higher priority.
my %color_priorities = (
'hollow' => 0,
'blue' => 1,
'purple' => 2,
'orange' => 3,
);
# Store the most recent colour set to AnyBar. Default to 'hollow'.
my $anybar_last_color = 'hollow';
my $print_hook = weechat::hook_print('', '', '', 0, 'print_cb', '');
weechat::hook_command(
'activity_bar',
'Activity Bar commands',
'[clear] | [enable|disable]',
'To disable activity notifications when WeeChat is focused, turn on terminal focus events:
/set weechat.startup.command_after_plugins "/print -stdout \033[?1004h\n"
/trigger add reset_focus signal "quit" "" "" "/print -stdout \033[?1004l\n"
Then disable activity_bar on focus, and enable it on unfocus:
/key bind meta2-I /activity_bar disable
/key bind meta2-O /activity_bar enable
clear: change AnyBar icon to hollow
enable: enable activity notification
disable: clear the AnyBar icon and disable activity notification',
'clear
|| enable
|| disable',
'activity_bar_command_cb',
''
);
sub print_cb {
my ($data, $buffer, $date, $tags, $displayed, $highlight, $prefix, $message) = @_;
my $buffer_type = weechat::buffer_get_string($buffer, 'localvar_type');
my $buffer_notify = weechat::buffer_get_integer($buffer, 'notify');
my $buffer_full_name = weechat::buffer_get_string($buffer, 'full_name');
# Don't notify about messages in the WeeChat core buffer.
if ($buffer_full_name eq 'core.weechat') {
return weechat::WEECHAT_RC_OK;
}
if ($buffer_type eq 'private') {
anybar_send('orange');
}
elsif ($highlight == 1) {
anybar_send('purple');
}
# Notify about regular messages if the buffer's `notify` property allows
# it.
elsif ($buffer_notify > 1) {
anybar_send('blue');
}
return weechat::WEECHAT_RC_OK;
}
sub anybar_send {
my ($message) = @_;
if (
$message ne 'hollow'
# If the current priority is lower than the previous colour.
&& $color_priorities{$message} < $color_priorities{$anybar_last_color}
) {
return;
}
$anybar_last_color = $message;
$socket->send($message);
}
sub activity_bar_command_cb {
my ($data, $buffer, $args) = @_;
if ($args eq 'clear') {
anybar_send('hollow');
}
if ($args eq 'disable') {
anybar_send('hollow');
weechat::unhook($print_hook);
$print_hook = undef;
}
if ($args eq 'enable' && !$print_hook) {
$print_hook = weechat::hook_print('', '', '', 0, 'print_cb', '');
}
return weechat::WEECHAT_RC_OK;
}
|