1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
# temp place for dumping all the stuff that doesn't belong in uberprompt.
use strict;
use warnings;
use Irssi;
use Irssi::TextUI; # for sbar_items_redraw
use Data::Dumper;
# TODO: maybe eval { use Term::Size } and use tthat if poss.
our $VERSION = "0.2";
our %IRSSI =
(
authors => "shabble",
contact => 'shabble+irssi@metavore.org, shabble@#irssi/Freenode',
name => "overlays",
description => "Library script for drawing overlays on irssi UI",
license => "MIT",
changed => "24/7/2010"
);
# overlay := { $num1 => line1, $num2 => line2 }
# line := [ region, region, region ]
# region := { start => x, end => y, ...? }
my $overlays;
my ($term_w, $term_h) = (0, 0);
sub DEBUG () { 1 }
sub update_terminal_size {
my @stty_data = qx/stty -a/;
my $line = $stty_data[0];
# linux
# speed 38400 baud; rows 36; columns 126; line = 0;
if ($line =~ m/rows (\d+); columns (\d+);/) {
$term_h = $1;
$term_w = $2;
# osx
# speed 9600 baud; 40 rows; 235 columns;
} elsif ($line =~ m/(\d+) rows; (\d+) columns;/) {
$term_h = $1;
$term_w = $2;
} else {
# guess?
$term_h = 24;
$term_w = 80;
}
print "Terminal detected as $term_w cols by $term_h rows" if DEBUG;
}
sub _add_overlay_region {
my ($x, $y, $text) = @_;
my $region = { start => $x,
text => $text,
};
my $o_line = $overlays->{$y};
unless (defined $o_line) {
$o_line = [];
$overlays->{$y} = $o_line;
}
# foreach my $cur_region (@$o_line) {
# if (_region_overlaps($cur_region, $region)) {
# # do something.
# print "Region overlaps";
# last;
# }
# }
push @$o_line, $region;
redraw_overlay();
}
# sub _remove_overlay_region {
# my ($line, $start, $end) = @_;
# my $o_line = $overlay->{$line};
# return unless $o_line;
# my $i = 0;
# foreach my $region (@$o_line) {
# if ($region->{start} == $start && $region->{end} == $end) {
# last;
# }
# $i++;
# }
# splice @$o_line, $i, 1, (); # remove it.
# }
sub redraw_overlay {
# TODO: we can't assume the active win is the only one with overlays.
Irssi::active_win->view->redraw();
foreach my $y (sort keys %$overlays) {
my $line = $overlays->{$y};
foreach my $region (@$line) {
Irssi::gui_printtext($region->{start}, $y, $region->{text});
}
}
}
sub augment_redraw {
#print "Redraw called" if DEBUG;
#redraw_overlay();
Irssi::timeout_add_once(10, \&redraw_overlay, 0);
}
sub ctrl_L_intercept {
my $key = shift;
if ($key == 12) { # C-L
print "C-l pressed" if DEBUG;
Irssi::command("redraw");
Irssi::signal_stop();
}
}
sub init {
die "needs uberprompt" unless script_is_loaded('uberprompt');
Irssi::signal_add_last ('command redraw', \&augment_redraw);
Irssi::signal_add_first('gui key pressed', \&ctrl_L_intercept);
Irssi::signal_add ('terminal resized', \&update_terminal_size);
Irssi::signal_add_first('gui print text finished', \&augment_redraw);
my $api_sigs = {
# input signals
'overlay create' => [qw/int int string/], # x, y, str
'overlay remove' => [qw/int int/], # x, y
'overlay clear' => [], # no args
# output signals
};
Irssi::signal_register($api_sigs);
Irssi::signal_add('overlay create', \&_add_overlay_region);
# Irssi::signal_add('overlay remove', \&_add_overlay_region);
Irssi::signal_add('overlay clear', \&_clear_overlay);
Irssi::command_bind('ocr', \&cmd_overlay_create);
Irssi::command_bind('ocl', sub { Irssi::signal_emit('overlay clear'); });
}
sub cmd_overlay_create {
my ($args) = @_;
my ($y, $x, $text) = split(/\s+/, $args, 3);
print "overlaying $text at [$x, $y]";
Irssi::signal_emit('overlay create', $x, $y, $text);
}
sub _clear_overlay {
$overlays = {};
redraw_overlay();
}
sub script_is_loaded {
my $name = shift;
print "Checking if $name is loaded" if DEBUG;
no strict 'refs';
my $retval = defined %{ "Irssi::Script::${name}::" };
use strict 'refs';
return $retval;
}
print "Moo!";
init();
__END__
# sub _draw_overlay_menu {
# my $w = 10;
# my @lines = (
# '%7+' . ('-' x $w) . '+%n',
# sprintf('%%7|%%n%*s%%7|%%n', $w, 'bacon'),
# sprintf('|%*s|', $w, 'bacon'),
# sprintf('|%*s|', $w, 'bacon'),
# sprintf('|%*s|', $w, 'bacon'),
# sprintf('|%*s|', $w, 'bacon'),
# sprintf('|%*s|', $w, 'bacon'),
# '%7+' . ('-' x $w) . '+%n',
# );
# my $i = 10; # start vert offset.
# for my $line (@lines) {
# Irssi::gui_printtext(int ($term_w / 2), $i++, $line);
# }
# }
|