Script: color_popup.pl

Interpret mirc formatting codes in the command line and show a color popup when needed.
Author: Nei — Version: 0.4 — License: GPL3
For WeeChat ≥ 0.3.0.
Tags: irc, color
Added: 2013-01-09 — Updated: 2015-12-13

Download GitHub Repository

 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
use strict; use warnings;
$INC{'Encode/ConfigLocal.pm'}=1;
require Encode;
use utf8;

# to read the following docs, you can use "perldoc color_popup.pl"

=head1 NAME

color_popup - show mirc colors when needed

=head1 SYNOPSIS

the color numbers will be shown when a color control code is
present.

=cut

use constant SCRIPT_NAME => 'color_popup';
weechat::register(SCRIPT_NAME, 'Nei <anti.teamidiot.de>', '0.4', 'GPL3', 'show mirc color codes', '', '') || return;

my %ones = map { $_ => 1 } 0, 8, 14, 15, 42, 43, 45, 53 .. 58, 65 .. 86, 95 .. 98;
weechat::hook_modifier('input_text_display_with_cursor', 'color_popup', '');

## color_popup -- show mirc colors
## () - modifier handler
## $_[2] - buffer pointer
## $_[3] - input string
## returns modified input string
sub color_popup {
	Encode::_utf8_on($_[3]);
	my $cc = qr/(?:\03(?:\d{1,2}(?:,(?:\d{1,2})?)?)?|\02|\x1d|\x0f|\x12|\x15|\x16|\x1f)/;
	my ($p1, $x, $p2) = split /((?:$cc)?\x19b#)/, $_[3], 2;
	for ($p1, $p2) {
		s/($cc)/$1■/g if weechat::config_string_to_boolean(weechat::config_get_plugin('reveal'));
		Encode::_utf8_on($_ = weechat::hook_modifier_exec(irc_color_decode => 1, weechat::hook_modifier_exec(irc_color_encode => 1, $_)));
	}
	$x .= ' ' . weechat::hook_modifier_exec(
	    irc_color_decode => 1, sub {
		$x =~ /\cC(\d{1,2})(,(\d{1,2})?)?/;
		my ($fg, $bg) = ($1//-1, $3//-1);
		my $sc = $bg >= 0 ? $bg : $2?-1:$fg;
		(join '', map {
		     "\03" . ($ones{0+$_} // 0) . ",$_$_"
		}
		    $sc >= 0 ? grep /^0?$sc/, '00' .. '99' : ('00' .. '15'))
		. "\03"
	    }->()
	   ) if $x =~ /^\03/ and weechat::current_buffer() eq $_[2];
	"$p1$x$p2"
}