1use Irssi;
2use Irssi::TextUI;
3use strict;
4
5use vars qw($VERSION %IRSSI);
6
7$VERSION="0.2.1";
8%IRSSI = (
9	authors=> 'BC-bd',
10	contact=> 'bd@bc-bd.org',
11	name=> 'rotator',
12	description=> 'Displaye a small, changeing statusbar item to show irssi is still running',
13	sbitems=> 'rotator',
14	license=> 'GPL v2',
15	url=> 'https://bc-bd.org/svn/repos/irssi/trunk/',
16);
17
18# rotator Displaye a small, changeing statusbar item to show irssi is still running
19# for irssi 0.8.4 by bd@bc-bd.org
20#
21#########
22# USAGE
23###
24#
25# To use this script type f.e.:
26#
27#		/statusbar window add -after more -alignment right rotator
28#
29# For more info on statusbars read the docs for /statusbar
30#
31#########
32# OPTIONS
33#########
34#
35# /set rotator_seperator <char>
36# 	The character that is used to split the string in elements.
37#
38# /set rotator_chars <string>
39# 	The string to display. Examples are:
40#
41# 		/set rotator_chars . o 0 O
42# 		/set rotator_chars _ � �
43# 		/set rotator_chars %r� %Y- %g-
44#
45# /set rotator_speed <int>
46# 	The number of milliseconds to display every char.
47# 	1 second = 1000 milliseconds.
48#
49# /set rotator_bounce <ON|OFF>
50#		* ON  : reverse direction at the end of rotator_chars
51#		* OFF : start from the beginning
52#
53###
54################
55###
56# Changelog
57#
58# Version 0.2.1
59#  - checking rotator_speed to be > 10
60#
61# Version 0.2
62#  - added rotator_bounce
63#  - added rotator_seperator
64#  - added support for elements longer than one char
65#  - fixed displaying of special chars (thx to peder for pointing this one out)
66#
67# Version 0.1
68#  - initial release
69#
70###
71################
72
73my ($pos,$char,$timeout,$boundary,$direction);
74
75$char = '';
76
77sub rotatorTimeout {
78	my @rot = split(Irssi::settings_get_str('rotator_seperator'), Irssi::settings_get_str('rotator_chars'));
79	my $len = scalar @rot;
80
81	$char = quotemeta($rot[$pos]);
82
83	if ($pos == $boundary) {
84		if (Irssi::settings_get_bool('rotator_bounce')) {
85			if ($direction < 0) {
86				$boundary = $len -1;
87			} else {
88				$boundary = 0;
89			}
90			$direction *= -1;
91		} else {
92			$pos = -1;
93		}
94	}
95
96	$pos += $direction;
97
98	Irssi::statusbar_items_redraw('rotator');
99}
100
101sub rotatorStatusbar() {
102	my ($item, $get_size_only) = @_;
103
104	$item->default_handler($get_size_only, "{sb ".$char."}", undef, 1);
105}
106
107sub rotatorSetup() {
108	my $time = Irssi::settings_get_int('rotator_speed');
109
110	Irssi::timeout_remove($timeout);
111
112	$boundary = scalar split(Irssi::settings_get_str('rotator_seperator'), Irssi::settings_get_str('rotator_chars')) -1;
113	$direction = +1;
114	$pos = 0;
115
116	if ($time < 10) {
117		Irssi::print("rotator: rotator_speed must be > 10");
118	} else {
119		$timeout = Irssi::timeout_add($time, 'rotatorTimeout' , undef);
120	}
121}
122
123Irssi::signal_add('setup changed', 'rotatorSetup');
124
125Irssi::statusbar_item_register('rotator', '$0', 'rotatorStatusbar');
126
127Irssi::settings_add_str('misc', 'rotator_chars', '. o 0 O 0 o');
128Irssi::settings_add_str('misc', 'rotator_seperator', ' ');
129Irssi::settings_add_int('misc', 'rotator_speed', 2000);
130Irssi::settings_add_bool('misc', 'rotator_bounce', 1);
131
132if (Irssi::settings_get_int('rotator_speed') < 10) {
133	Irssi::print("rotator: rotator_speed must be > 10");
134} else {
135	$timeout = Irssi::timeout_add(Irssi::settings_get_int('rotator_speed'), 'rotatorTimeout' , undef);
136}
137
138rotatorSetup();
139