#!/usr/bin/perl
# window_title module for irssi
# Copyright (C) 2007  Dwayne C. Litzenberger
#
# 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 2 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Specific permission is granted for the GPLed code in this
# distribition to be linked to OpenSSL without invoking GPL clause
# 2(b).

use Irssi;
use vars qw ($VERSION %IRSSI);
$VERSION = "0.1";
%IRSSI = (
    authors => "Dwayne C. Litzenberger",
    contact => "dlitz\@dlitz.net",
    name        => "window_title",
    description => "Set the xterm window title",
    license => "GPL",
    changed => "Wed, 04 Apr 2007 09:49:55 -0600"
);


sub update_window_title {
    # Get the active Windowitem
    my $window = Irssi::active_win();
    my $windowitem = $window->{active};

    # Check if any window is highlighted
    my $hilight = 0;
    foreach my $w (Irssi::windows()) {
        my $wi = $w->{active};
        my $level = $wi->{data_level};  # 0=no new data, 1=test, 2=msg, 3=highlighted text
        if ($level >= 3) {
            $hilight = 1;
        }
    }

    # Output the window title
    printf STDERR "\033]0;%s%sirssi\007", (
        $hilight ? "*" : "",
        $windowitem->{name} ? $windowitem->{name} . " - " : "");
}

Irssi::signal_add('gui print text', 'update_window_title');

# vim:set ts=4 sw=4 sts=4 expandtab:
