use strict;
use warnings;
use experimental 'signatures';

our $VERSION = '0.1'; # 88880ac9fc365da
our %IRSSI = (
    authors     => 'Wouter Coekaerts, Nei',
    contact	=> 'Nei @ anti@conference.jabber.teamidiot.de',
    url		=> "http://anti.teamidiot.de/",
    name        => 'viewlevels',
    description => 'prints the message level after every line. useful for debugging your level settings',
    license     => 'GPLv2',
    changed     => '2022-06-20',
);

use Irssi;

die 'This script requires Irssi 1.3 or later'
    if (Irssi::parse_special('$abiversion')||0) < 37;

my $view_levels;

sub render_line_text ($dest, $str, $meta) {
    $$str .= Irssi::format_string_expand('%N%K |') . Irssi::bits2level($dest->{level}) . '| '
	if $view_levels;
}

sub read_settings {
    my $old = $view_levels;
    $view_levels = Irssi::settings_get_bool('view_levels');
    Irssi::command('scrollback redraw')
	    if ($old && !$view_levels) || (!$old && $view_levels);
}

Irssi::settings_add_bool('viewlevels', 'view_levels', 1);
Irssi::signal_add_last('gui render line text', 'render_line_text');
Irssi::signal_add('setup changed', 'read_settings');
Irssi::signal_add('setup reread', 'read_settings');

read_settings();
