-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtab-name.pl
371 lines (276 loc) · 8.82 KB
/
tab-name.pl
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
use common::sense;
use List::Util 'max';
use Xchat;
#format for the tab name when there are no unread messages and when there are
#%m is your channel mode
#%c is the name of the tab/channel
#%u is the amount of unread messages
#%; is the separator that enables aligning
my $format = '%m%c';
my $format_unread = '%m%c%;%u';
#whether to align anything after %; to the right
my $align = 1;
#only align up to the longest channel (might be somewhat slower)
my $align_min = 0;
#whether to right align the unread messages number to the largest unread messages number
my $right_align_unread = 1;
#specify on what kind of tabs the script works
my $working_tabs = [
1, #server
2, #channel
3, #dialog
4, #notices
5, #server notices
];
#same as above but specify what kind of tabs /allread works on
my $allread_working_tabs = [
1,
2,
3,
4,
5,
];
#text events that increase the message counter
my @events = (
'Channel Message',
'Channel Msg Hilight',
'Private Message to Dialog',
'Channel Action',
'Channel Action Hilight',
'Private Action to Dialog',
#'Join',
#'Part',
#'Part with Reason',
#'Quit',
);
Xchat::register 'Tab name', '1.06', 'Get more information out of your tab names.';
Xchat::hook_print 'Close Context', \&clean_up;
#for channel unread messages changes
if ($format =~ /%u/ || $format_unread =~ /%u/) {
Xchat::hook_print 'Focus Tab', \&reset_unread;
Xchat::hook_print $_, \&check_unread for @events;
Xchat::hook_print 'You Join', \&update_join;
}
#for channel mode changes
if ($format =~ /%m/ || $format_unread =~ /%m/) {
Xchat::hook_print 'Channel Mode Generic', \&generic_mode_change;
Xchat::hook_print $_, \&mode_change, { 'data' => $_ }
for 'Channel Voice', 'Channel DeVoice',
'Channel Half-Operator', 'Channel DeHalfOp',
'Channel Operator', 'Channel DeOp';
#for getting the mode correctly in unregistered channels
Xchat::hook_server '353', \&channel_join;
}
Xchat::hook_command 'allread', \&allread;
my $data = { };
my $active_tab = Xchat::get_context;
my $max_unread_length = 1;
my $max_channel_length = 0;
my $max_length = Xchat::get_prefs 'gui_tab_trunc'; #never align past this value
sub reset_unread {
my $context = Xchat::get_context;
my $channel = Xchat::get_info 'channel';
my $type = Xchat::context_info->{'type'};
$active_tab = $context;
return Xchat::EAT_NONE if !grep { $_ == $type } @$working_tabs;
return Xchat::EAT_NONE if !length $channel;
$data->{ $context }{'unread'} = 0;
update_name($context, $channel);
update_all();
return Xchat::EAT_NONE;
}
sub clean_up {
my $context = Xchat::get_context;
my $type = Xchat::context_info->{'type'};
return Xchat::EAT_NONE if !grep { $_ == $type } @$working_tabs;
if (exists $data->{ $context }) {
delete $data->{ $context };
}
update_all();
return Xchat::EAT_NONE;
}
sub check_unread {
my $context = Xchat::get_context;
my $channel = Xchat::get_info 'channel';
my $type = Xchat::context_info->{'type'};
return Xchat::EAT_NONE if !grep { $_ == $type } @$working_tabs;
return Xchat::EAT_NONE if $context == $active_tab || !length $channel;
$data->{ $context }{'unread'}++;
update_name($context, $channel);
update_all();
return Xchat::EAT_NONE;
}
#update the unread counter in case
sub update_join {
my $context = Xchat::get_context;
my $channel = Xchat::get_info 'channel';
my $type = Xchat::context_info->{'type'};
return Xchat::EAT_NONE if !grep { $_ == $type } @$working_tabs;
return Xchat::EAT_NONE if !exists $data->{ $context };
update_name($context, $channel);
return Xchat::EAT_NONE;
}
sub generic_mode_change {
my ($word) = @_;
my $context = Xchat::get_context;
my $channel = Xchat::get_info 'channel';
return Xchat::EAT_NONE if Xchat::context_info->{'type'} != 2;
#for channel modes, $word->[3] is "#channel nick"
return Xchat::EAT_NONE if $word->[3] !~ m/ /;
my ($channel, $nick) = split / /, $word->[3];
my $current_nick = Xchat::get_info 'nick';
return Xchat::EAT_NONE if fc $current_nick ne fc $nick;
delay(\&set_mode, $context, $channel);
return Xchat::EAT_NONE;
}
sub mode_change {
my ($word) = @_;
my $nick = Xchat::get_info 'nick';
my $context = Xchat::get_context;
my $channel = Xchat::get_info 'channel';
return Xchat::EAT_NONE if Xchat::context_info->{'type'} != 2;
return Xchat::EAT_NONE if fc $nick ne fc $word->[1];
delay(\&set_mode, $context, $channel);
return Xchat::EAT_NONE;
}
sub allread {
$data = { };
for (Xchat::get_list 'channels') {
Xchat::set_context $_->{'context'};
my $type = Xchat::context_info->{'type'};
next if !grep { $_ == $type } @$allread_working_tabs;
delete $data->{ $_->{'context'} };
Xchat::command 'gui color 0';
}
update_all();
return Xchat::EAT_ALL;
}
sub channel_join {
my $channel = $_[0][4];
my $users = substr $_[1][5], 1;
my $nick = Xchat::get_info 'nick';
my $context = Xchat::find_context($channel);
my $prefixes = Xchat::context_info->{'nickprefixes'};
delay(\&set_mode, $context, $channel) if $users =~ /[\Q$prefixes\E]\Q$nick\E/;
return Xchat::EAT_NONE;
}
sub update_name {
my ($context, $channel) = @_;
Xchat::set_context $context;
my $mode = _user_prefix();
my $name = $data->{ $context }{'unread'} ? $format_unread : $format;
if ($align && $name =~ /%;/ && $data->{ $context }{'unread'}) {
#one space
my $tab_length = 1;
#amount of unread count digits
$tab_length += $max_unread_length;
#one channel mode character, if specified
$tab_length += 1 if $mode && $format =~ /%m/;
#any other characters specified
{
my $format = $name;
$format =~ s/%[muc;]//g;
$tab_length += length $format;
}
my $limit = $max_length;
if ($align_min && $limit > $max_channel_length + $tab_length) {
$limit = $max_channel_length + $tab_length;
}
#the name of the channel
$tab_length += length $channel;
my $spaces = 1;
#resize the name of the channel if necessary
if ($tab_length > $limit) {
$channel = substr $channel, 0, $limit - $tab_length - length $channel - 2;
$channel .= '..';
}
#otherwise, add spaces so that we reach the limit
elsif ($tab_length < $limit) {
$spaces = 1 + $limit - $tab_length;
}
#replace mode character and channel name
$name =~ s/%m/$mode/;
$name =~ s/%c/$channel/;
#right align with spaces if enabled
if ($right_align_unread) {
$name =~ s/%u/sprintf '%*d', $max_unread_length, $data->{ $context }{'unread'}/e;
}
else {
$name =~ s/%u/$data->{ $context }{'unread'}/;
}
#turn into spaces and replace
$spaces = ' ' x $spaces;
$name =~ s/%;/$spaces/;
}
else {
$name =~ s/%m/$mode/;
$name =~ s/%c/$channel/;
#replace %u here with $max_unread_length
if ($data->{ $context }{'unread'}) {
if ($right_align_unread) {
$name =~ s/%u/sprintf '%*d', $max_unread_length, $data->{ $context }{'unread'}/e;
}
else {
$name =~ s/%u/$data->{ $context }{'unread'}/;
}
}
#replace with spaces to allow some custom formatting
else {
$name =~ s/%u/' ' x $max_unread_length/e; # if $data->{ $context }{'unread'};
}
}
Xchat::set_context $context;
Xchat::command "settab $name";
return 1;
}
sub update_all {
state $last_unread_length;
state $last_channel_length;
#update with new values
$max_unread_length = max map { length $data->{ $_ }{'unread'} } keys $data;
if ($align) {
$max_channel_length = max map {
#length of the channel
length($_->{'channel'}) +
#mode
($format_unread =~ /%m/ && _user_prefix($_->{'context'}) ? 1 : 0) +
#one space
1
} Xchat::get_list 'channels';
}
#check for changes
if ($last_unread_length != $max_unread_length || $last_channel_length != $max_channel_length) {
for (Xchat::get_list 'channels') {
next if $_->{'type'} != 2;
next if !length $_->{'channel'};
update_name($_->{'context'}, $_->{'channel'});
}
$last_unread_length = $max_unread_length;
$last_channel_length = $max_channel_length;
}
}
sub set_mode {
my ($context, $channel) = @_;
#make sure it's a channel tab
return 0 if Xchat::context_info($context)->{'type'} != 2;
update_name($context, $channel);
return 1;
}
sub delay {
my ($code, @params) = @_;
Xchat::hook_timer 0, sub {
$code->(@params);
return Xchat::REMOVE;
};
return 1;
}
sub _user_prefix {
my $old_context = Xchat::get_context;
my ($context) = @_;
my $prefix;
Xchat::set_context $context if $context;
my $info = Xchat::user_info();
$prefix = $info->{'prefix'} if $info;
Xchat::set_context $old_context if $context;
return $prefix;
}