Add tmux conf
[shutils.git] / dotfiles / irssi / scripts / trackbar.pl
1 # trackbar.pl
2 #
3 # This little script will do just one thing: it will draw a line each time you
4 # switch away from a window. This way, you always know just upto where you've
5 # been reading that window :) It also removes the previous drawn line, so you
6 # don't see double lines.
7 #
8 # Usage:
9 #
10 # The script works right out of the box, but if you want you can change
11 # the working by /set'ing the following variables:
12 #
13 # trackbar_string The characters to repeat to draw the bar
14 # trackbar_style The style for the bar, %r is red for example
15 # See formats.txt that came with irssi
16 #
17 # /mark is a command that will redraw the line at the bottom. However! This
18 # requires irssi version after 20021228. otherwise you'll get the error
19 # redraw: unknown command, and your screen is all goofed up :)
20 #
21 # /upgrade & buf.pl notice: This version tries to remove the trackbars before
22 # the upgrade is done, so buf.pl does not restore them, as they are not removeable
23 # afterwards by trackbar. Unfortiounatly, to make this work, trackbar and buf.pl
24 # need to be loaded in a specific order. Please experiment to see which order works
25 # for you (strangely, it differs from configuration to configuration, something I will
26 # try to fix in a next version)
27 #
28 # Authors:
29 # - Main maintainer & author: Peter 'kinlo' Leurs
30 # - Many thanks to Timo 'cras' Sirainen for placing me on my way
31 # - on-upgrade-remove-line patch by Uwe Dudenhoeffer
32 #
33 # Version history:
34 # 1.4: - Changed our's by my's so the irssi script header is valid
35 # - Removed utf-8 support. In theory, the script should work w/o any
36 # problems for utf-8, just set trackbar_string to a valid utf-8 character
37 # and everything *should* work. However, this script is being plagued by
38 # irssi internal bugs. The function Irssi::settings_get_str does NOT handle
39 # unicode strings properly, hence you will notice problems when setting the bar
40 # to a unicode char. For changing your bar to utf-8 symbols, read the line sub.
41 # 1.3: - Upgrade now removes the trackbars.
42 # - Some code cleanups, other defaults
43 # - /mark sets the line to the bottom
44 # 1.2: - Support for utf-8
45 # - How the bar looks can now be configured with trackbar_string
46 # and trackbar_style
47 # 1.1: - Fixed bug when closing window
48 # 1.0: - Initial release
49 #
50 #
51 # Call for help!
52 #
53 # There is a trackbar version 2.0 that properly handles resizes and immediate config change
54 # activation. However, there is/are some bug(s) in irssi's main buffer/window code that causes
55 # irssi to 'forget' lines, which is ofcourse completly unaccepteable. I haven't found the time
56 # nor do I know the irssi's internals enough to find and fix this bug, if you want to help, please
57 # contact me, I'll give you a copy of the 2.0 version that will immediatly show you the problems.
58 #
59 # Known bugs:
60 # - if you /clear a window, it will be uncleared when returning to the window
61 # - UTF-8 characters in the trackbar_string doesnt work. This is an irssi bug.
62 # - if you resize your irssi (in xterm or so) the bar is not resized
63 # - changing the trackbar style is only visible after returning to a window
64 # however, changing style/resize takes in effect after you left the window.
65 #
66 # Whishlist/todo:
67 # - instead of drawing a line, just invert timestamp or something,
68 # to save a line (but I don't think this is possible with current irssi)
69 # - some pageup keybinding possibility, to scroll up upto the trackbar
70 # - <@coekie> kinlo: if i switch to another window, in another split window, i
71 # want the trackbar to go down in the previouswindow in that splitwindow :)
72 # - < bob_2> anyway to clear the line once the window is read?
73 # - < elho> kinlo: wishlist item: a string that gets prepended to the repeating pattern
74 # - < elho> an option to still have the timestamp in front of the bar
75 # - < elho> oh and an option to not draw it in the status window :P
76 #
77 # BTW: when you have feature requests, mailing a patch that works is the fastest way
78 # to get it added :p
79
80 use strict;
81 use 5.6.1;
82 use Irssi;
83 use Irssi::TextUI;
84 use POSIX qw(strftime);
85
86 my $VERSION = "1.4";
87
88 my %IRSSI = (
89 authors => "Peter 'kinlo' Leurs",
90 contact => "peter\@pfoe.be",
91 name => "trackbar",
92 description => "Shows a bar where you've last read a window",
93 license => "GPLv2",
94 url => "http://www.pfoe.be/~peter/trackbar/",
95 changed => "Thu Feb 20 16:18:08 2003",
96 );
97
98 my %config;
99
100 Irssi::theme_register([
101 'trackbar_loaded', '%R>>%n %_Scriptinfo:%_ Loaded $0 version $1 by $2.',
102 'trackbar_wrong_style', '%R>>%n %_Trackbar:%_ I detected a malformed %_trackbar_style%_, the default setting has been restored.',
103 'trackbar_wrong_string', '%R>>%n %_Trackbar:%_ I detected a malformed %_trackbar_string%_, the default setting has been restored.',
104 'trackbar_wrong_version', '%R>>%n %_Trackbar:%_ Please upgrade your client to 0.8.9 or above if you would like to use this version of trackbar.',
105 'trackbar_not_allowed', '%R>>%n %_Trackbar:%_ You are not allowed to use that character in the %_trackbar_string%_ setting, the default setting has been restored.',
106 'trackbar_all_removed', '%R>>%n %_Trackbar:%_ All the trackbars have been removed.',
107 'trackbar_line', '{timestamp $Z} ',
108 'trackbar_not_found', '%R>>%n %_Trackbar:%_ No trackbar found in this window.',
109 'trackbar_help', '$0',
110 'trackbar_init', '%R>>%n %_Trackbar:%_ Processed all the Irssi auto generated startup windows. Trackbar is ready.',
111 ]);
112
113 Irssi::settings_add_str('trackbar', 'trackbar_string' => '-');
114 $config{'trackbar_string'} = Irssi::settings_get_str('trackbar_string');
115
116 Irssi::settings_add_str('trackbar', 'trackbar_style' => '%K');
117 $config{'trackbar_style'} = Irssi::settings_get_str('trackbar_style');
118
119 Irssi::signal_add(
120 'setup changed' => sub {
121 $config{'trackbar_string'} = Irssi::settings_get_str('trackbar_string');
122 $config{'trackbar_style'} = Irssi::settings_get_str('trackbar_style');
123 if ($config{'trackbar_style'} =~ /(?<!%)[^%]|%%|%$/) {
124 Irssi::print(
125 "trackbar: %RWarning!%n 'trackbar_style' seems to contain "
126 . "printable characters. Only use format codes (read "
127 . "formats.txt).", MSGLEVEL_CLIENTERROR);
128 }
129 }
130 );
131
132 Irssi::signal_add(
133 'window changed' => sub {
134 my (undef, $oldwindow) = @_;
135
136 if ($oldwindow) {
137 my $line = $oldwindow->view()->get_bookmark('trackbar');
138 $oldwindow->view()->remove_line($line) if defined $line;
139 $oldwindow->print(line($oldwindow->{'width'}), MSGLEVEL_NEVER);
140 $oldwindow->view()->set_bookmark_bottom('trackbar');
141 }
142 }
143 );
144
145 sub line {
146 my $width = shift;
147 my $string = $config{'trackbar_string'};
148 $string = '-' unless defined $string;
149
150 # There is a bug in irssi's utf-8 handling on config file settings, as you
151 # can reproduce/see yourself by the following code sniplet:
152 #
153 # my $quake = pack 'U*', 8364; # EUR symbol
154 # Irssi::settings_add_str 'temp', 'temp_foo' => $quake;
155 # Irssi::print length $quake;
156 # # prints 1
157 # Irssi::print length Irssi::settings_get_str 'temp_foo';
158 # # prints 3
159 #
160 #
161 # Trackbar used to have a workaround, but on recent versions of perl/irssi
162 # it does no longer work. Therefore, if you want your trackbar to contain
163 # unicode characters, uncomment the line below for a nice full line, or set
164 # the string to whatever char you want.
165
166 # $string = pack('U*', 0x2500);
167
168
169 my $length = length $string;
170
171 if ($length == 0) {
172 $string = '-';
173 $length = 1;
174 }
175
176 my $times = $width / $length;
177 $times = int(1 + $times) if $times != int($times);
178 $string =~ s/%/%%/g;
179 return $config{'trackbar_style'} . substr($string x $times, 0, $width);
180 }
181
182 # Remove trackbars on upgrade - but this doesn't really work if the scripts are not loaded in the correct order... watch out!
183
184 Irssi::signal_add_first( 'session save' => sub {
185 for my $window (Irssi::windows) {
186 next unless defined $window;
187 my $line = $window->view()->get_bookmark('trackbar');
188 $window->view()->remove_line($line) if defined $line;
189 }
190 }
191 );
192
193 sub goto_trackbar {
194
195 my $window = Irssi::active_win();
196 my $line = $window->view()->get_bookmark('trackbar');
197
198 if ($line) {
199 $window->command("scrollback goto ". strftime("%d %H:%M:%S", localtime($line->{'info'}->{'time'})));
200 } else {
201 $window->printformat(MSGLEVEL_CLIENTCRAP, 'trackbar_not_found');
202 }
203 }
204
205 sub cmd_mark {
206 my $window = Irssi::active_win();
207 # return unless defined $window;
208 my $line = $window->view()->get_bookmark('trackbar');
209 $window->view()->remove_line($line) if defined $line;
210 $window->print(line($window->{'width'}), MSGLEVEL_NEVER);
211 $window->view()->set_bookmark_bottom('trackbar');
212 Irssi::command("redraw");
213 }
214
215 sub trackbar_runsub {
216
217 my ($data, $server, $item) = @_;
218 $data =~ s/\s+$//g;
219
220 if ($data) {
221 Irssi::command_runsub('trackbar', $data, $server, $item);
222 } else {
223 goto_trackbar();
224 }
225 }
226
227 Irssi::command_bind('trackbar', 'trackbar_runsub');
228 Irssi::command_bind('mark', 'cmd_mark');