Skip to content

Instantly share code, notes, and snippets.

@tatsuru
Created June 2, 2012 21:37
Show Gist options
  • Save tatsuru/2860022 to your computer and use it in GitHub Desktop.
Save tatsuru/2860022 to your computer and use it in GitHub Desktop.
weechat global log plugin
# Weechat plugin
use strict;
use warnings;
my %options = (
buffer => "glog",
logger => "",
exclude => "",
);
sub glog_init {
weechat::register("glog", "tatsuru", "0.1", "BSD", "show all buffers' logs in a single buffer", "", "");
weechat::hook_config("plugins.var.perl.glog.*", "glog_config_cb", "");
weechat::hook_print("", "", "", 1, "glog_message_hook_cb", "");
$options{buffer} = weechat::buffer_new($options{buffer}, "glog_buffer_input_cb", "", "glog_buffer_close_cb", "");
}
sub glog_config_cb {
my $data = shift;
my $option = shift;
my $value = shift;
$option =~ s/^plugins\.var\.perl\.glog\.//;
if ($option eq "buffer") {
weechat::command($options{buffer}, "/close");
$options{buffer} = weechat::buffer_new($value, "glog_buffer_input_cb", "", "glog_buffer_close_cb", "");
} elsif ($option eq "logger") {
$options{logger} = $value;
} elsif ($option eq "exclude") {
$options{exclude} = $value;
}
return weechat::WEECHAT_RC_OK;
}
sub glog_get_color {
my $channel = shift;
# exclude some colors for visibility on black-background terminal
# black, darkgray, brown
my @color_table = qw/ gray white lightred red
lightgreen green yellow
lightblue blue lightmagenta magenta
lightcyan cyan
/;
my @codes = unpack "C*", $channel;
my $sum = 0;
foreach (@codes) { $sum += $_; }
return $color_table[$sum % ($#color_table+1)];
}
sub glog_message_hook_cb {
my $data = shift;
my $buffer = shift;
my $date = shift;
my $tags = shift;
my $displayed = shift;
my $highlight = shift;
my $prefix = shift;
my $message = shift;
my $buffer_name = weechat::buffer_get_string($buffer, "name");
my $buffer_short_name = weechat::buffer_get_string($buffer, "short_name");
if ($options{exclude}) {
foreach my $exclude (split(/,/, $options{exclude})) {
if ($buffer_name eq $exclude) {
return weechat::WEECHAT_RC_OK;
}
}
}
my $log = sprintf ("%s\t%s<%s> (%s) %s",
$options{logger},
weechat::color(glog_get_color $buffer_name),
$buffer_name,
$prefix,
$message);
weechat::print($options{buffer}, $log);
return weechat::WEECHAT_RC_OK;
}
sub glog_buffer_input_cb {
return weechat::WEECHAT_RC_OK;
}
sub glog_buffer_close_cb {
$options{buffer} = "";
return weechat::WEECHAT_RC_OK;
}
glog_init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment