Created
February 16, 2012 03:07
-
-
Save eschulte/1841354 to your computer and use it in GitHub Desktop.
irssi script to write window activity list to an external file for consumption by xmobar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Prints a window activity line to a file with color markup for consumption by xmobar | |
# | |
# Based on activity_file and nicklist | |
# | |
# | |
# The script uses 3 variables: | |
# act_file_width: How many characters wide the act list can be | |
# act_file_path: Path to the file | |
# act_file_hilight: How to hilight the "ACT" in the beginning: | |
# 0: No "ACT" | |
# 1: "ACT" with no color | |
# 2: "ACT" with color | |
# 3: " ACT " with color and added spaces | |
# | |
# Changes: | |
# 1.1: Now also updates fifo when config or window numbers are changed. | |
# | |
# 1.2: Adapted by Eric Schulte to write a file instead of a fifo, | |
# and to write color codes in a format readable by xmobar. | |
# May be used by adding the following to your xmobarrc | |
# | |
# , Run Com "tail" ["-1", "/home/USERNAME/.irssi/act_file"] "irssi" 10 | |
use Irssi qw( | |
settings_get_int settings_get_str | |
settings_add_int settings_add_str | |
signal_add_last | |
); | |
use Fcntl; | |
use IO::Handle; | |
use vars qw($VERSION %IRSSI); | |
$VERSION = "1.2"; | |
%IRSSI = | |
( | |
authors => 'Daniel Kalør (Xnybre)', | |
contact => 'irssi@kalor.dk', | |
name => 'act_file', | |
description => 'Print window activity to a file', | |
license => 'GPLv2', | |
changed => '2008-08-27' | |
); | |
my $last_values = {}; | |
my @colors = ("","\033[0;36;40m","\033[1;37;40m","\033[1;35;40m"); | |
sub file_stop | |
{ | |
truncate(FILE, 0); | |
print FILE "\n"; | |
print FILE "--\n"; | |
close FILE; | |
Irssi::print("File closed."); | |
} | |
sub cleanup | |
{ | |
file_stop(); | |
} | |
sub item_status_changed | |
{ | |
my ($item, $oldstatus) = @_; | |
return if ! ref $item->{server}; | |
my $tag = $item->{server}{tag}; | |
my $name = $item->{name}; | |
return if ! $tag || ! $name; | |
store_status() if ! $last_values->{$tag}{$name} || | |
$last_values->{$tag}{$name}{level} != $item->{data_level}; | |
} | |
sub store_status | |
{ | |
my $new_values = {}; | |
my @items = (); | |
for my $window ( sort { $a->{refnum} <=> $b->{refnum} } Irssi::windows() ) | |
{ | |
for my $item ( $window->items() ) | |
{ | |
next if ! ref $item->{server}; | |
my $tag = $item->{server}{tag}; | |
my $name = $item->{name}; | |
next if ! $tag || ! $name || $item->{data_level} == 0; | |
$new_values->{$tag}{$name} = | |
{ | |
level => $item->{data_level}, | |
window => $window->{refnum}, | |
}; | |
push @items, $new_values->{$tag}{$name}; | |
} | |
} | |
truncate(FILE, 0); | |
print FILE "\n"; | |
my $maxw = settings_get_int('act_file_width'); | |
my $w = 0; | |
if(scalar(@items) > 0) | |
{ | |
my $hi = settings_get_int('act_file_hilight'); | |
if($hi == 1) | |
{ | |
print FILE "ACT:" or file_stop(); | |
$w += 4; | |
} | |
elsif($hi == 2) | |
{ | |
print FILE "\033[1;37;41mACT:\033[0m" or file_stop(); | |
$w += 4; | |
} | |
elsif($hi > 2) | |
{ | |
print FILE "\033[1;37;41m ACT: \033[0m" or file_stop(); | |
$w += 6; | |
} | |
} | |
my $num_items = scalar (@items); | |
my $counter = 0; | |
for ( @items ) | |
{ | |
$counter += 1; | |
my $win = $_->{window}; | |
my $level = $_->{level}; | |
my $winw = int(log($win)/log(10))+1; | |
if($winw + 2 + $w > $maxw) | |
{ | |
print FILE ""; | |
last; | |
} | |
if($level > 2) | |
{ print FILE "<fc=#ff7272>$win</fc>" or file_stop(); } | |
else | |
{ print FILE "$win" or file_stop(); } | |
if($counter < $num_items) | |
{ print FILE "," or file_stop(); } | |
$w += $winw + 1; | |
} | |
print FILE "\n"; | |
$last_values = $new_values; | |
} | |
settings_add_int('act_file','act_file_width',25); | |
settings_add_int('act_file','act_file_hilight',0); | |
settings_add_str('act_file', 'act_file_path', Irssi::get_irssi_dir . '/act_file'); | |
## open/create FILE | |
my $path = settings_get_str('act_file_path'); | |
sysopen(FILE, $path, O_CREAT|O_WRONLY) or die "Couldn\'t write to the file ($!)."; | |
FILE->autoflush(1); | |
print FILE ""; # erase screen & jump to 0,0 | |
# store initial status | |
store_status(); | |
signal_add_last('setup changed', \&store_status); | |
signal_add_last('window item activity', \&item_status_changed); | |
signal_add_last('window refnum changed', \&store_status); | |
signal_add_last('gui exit', \&cleanup); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment