7 |
use Carp qw/cluck confess/; |
use Carp qw/cluck confess/; |
8 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
9 |
|
|
10 |
our $VERSION = '0.00'; |
use base qw/Class::Accessor/; |
11 |
|
__PACKAGE__->mk_accessors( qw/ |
12 |
|
screen |
13 |
|
lines |
14 |
|
error_text |
15 |
|
status_text |
16 |
|
|
17 |
|
debug |
18 |
|
/ ); |
19 |
|
|
20 |
|
|
21 |
|
our $VERSION = '0.01'; |
22 |
|
|
23 |
=head1 NAME |
=head1 NAME |
24 |
|
|
26 |
|
|
27 |
=head1 METHODS |
=head1 METHODS |
28 |
|
|
29 |
=cut |
=head2 new |
30 |
|
|
31 |
my $scr; |
my $iselect = Term::ISelect->new({ |
32 |
|
lines => [ |
33 |
|
'first line', |
34 |
|
'{s}second selectable line', |
35 |
|
'', |
36 |
|
'last line', |
37 |
|
], |
38 |
|
debug => 1 |
39 |
|
}); |
40 |
|
|
41 |
|
=head2 open_screen |
42 |
|
|
43 |
|
$iselect->open_screen; |
44 |
|
|
45 |
|
=cut |
46 |
|
|
47 |
# leave sane terminal if script dies |
# leave sane terminal if script dies |
48 |
$SIG{__DIE__} = sub { |
$SIG{__DIE__} = sub { |
49 |
eval { system('stty sane'); }; |
eval { system('stty sane'); }; |
50 |
}; |
}; |
51 |
|
|
52 |
my @lines; |
sub open_screen { |
53 |
|
my $self = shift; |
54 |
|
$self->screen( new Term::Screen ); |
55 |
|
} |
56 |
|
|
57 |
my $top_screen_line = 0; # offset in original text |
my $top_screen_line = 0; # offset in original text |
58 |
my $pos = 0; |
my $pos = 0; |
59 |
|
|
60 |
# default: select first line |
# default: select first line |
61 |
my $sel_pos = 0; |
my $sel_pos = 0; |
|
my $status_text = ''; |
|
|
my $error_text = ''; |
|
62 |
|
|
63 |
my $status_lines = 3; |
my $status_lines = 3; |
64 |
|
|
65 |
my $selectable_line; |
my $selectable_line; |
66 |
|
|
67 |
|
my $nr_lines = 0; |
68 |
|
|
69 |
|
=head2 full_line |
70 |
|
|
71 |
|
Returns line padded up to screen width |
72 |
|
|
73 |
|
$iselect->full_line( "foo bar" ); |
74 |
|
|
75 |
|
=cut |
76 |
|
|
77 |
sub full_line { |
sub full_line { |
78 |
|
my $self = shift; |
79 |
|
|
80 |
|
my $cols = $self->screen->cols; |
81 |
|
|
82 |
my $t = shift; |
my $t = shift; |
83 |
|
|
84 |
|
$t =~ s/{s}//; |
85 |
|
|
86 |
$t = '' unless defined $t; |
$t = '' unless defined $t; |
87 |
return $t . (" " x ($scr->cols - length($t))); |
$t = substr($t,0,$cols) if length($t) > $cols; |
88 |
|
return $t . (" " x ($cols - length($t))); |
89 |
} |
} |
90 |
|
|
91 |
sub chunk { |
|
92 |
my $t = shift; |
=head2 redraw_line |
93 |
cluck "expected line" unless defined $t; |
|
94 |
return substr($t,0,$scr->cols); |
$iselect->redraw_line( $line_on_screen, $content_of_line ); |
95 |
} |
|
96 |
|
=cut |
97 |
|
|
98 |
sub redraw_line { |
sub redraw_line { |
99 |
|
my $self = shift; |
100 |
|
|
101 |
my ($l,$line) = @_; |
my ($l,$line) = @_; |
102 |
|
|
103 |
if ( defined $selectable_line->{ $l + $top_screen_line } ) { |
if ( defined $selectable_line->{ $l + $top_screen_line } ) { |
104 |
$scr->at($l,0)->bold()->puts( full_line( chunk($line) ) )->normal(); |
$self->screen->at($l,0)->bold()->puts( $self->full_line( $line ) )->normal(); |
105 |
} else { |
} else { |
106 |
$scr->at($l,0)->puts( full_line( chunk($line) ) ); |
$self->screen->at($l,0)->puts( $self->full_line( $line ) ) |
107 |
} |
} |
108 |
} |
} |
109 |
|
|
110 |
sub redraw { |
=head2 redraw_screen |
111 |
for my $l (0 .. $scr->rows - $status_lines) { |
|
112 |
|
$iselect->redraw_screen |
113 |
|
|
114 |
|
=cut |
115 |
|
|
116 |
|
sub redraw_screen { |
117 |
|
my $self = shift; |
118 |
|
my @lines = @{ $self->lines }; |
119 |
|
$nr_lines = $#lines; |
120 |
|
for my $l (0 .. $self->screen->rows - $status_lines) { |
121 |
my $line = $lines[ $l + $top_screen_line ]; |
my $line = $lines[ $l + $top_screen_line ]; |
122 |
redraw_line( $l, $line ); |
$self->redraw_line( $l, $line ); |
123 |
last if ($l == $#lines); |
last if ($l == $#lines); |
124 |
} |
} |
125 |
selected(0); |
$self->selected; |
126 |
} |
} |
127 |
|
|
128 |
sub status { |
=head2 redraw_statusline |
129 |
my $pcnt = int(($pos || 0) * 100 / ($#lines || 1)); |
|
130 |
my $pos_txt = sprintf('%d, %d%% ',$pos,$pcnt); |
Redraw status line |
131 |
|
|
132 |
|
$iselect->redraw_statusline; |
133 |
|
|
134 |
|
=cut |
135 |
|
|
136 |
|
sub redraw_statusline { |
137 |
|
my $self = shift; |
138 |
|
|
139 |
|
my $pcnt = int(($pos || 0) * 100 / ( $nr_lines || 1 ) ); |
140 |
|
my $pos_txt = sprintf('%d/%s, %d%% ',$pos,$nr_lines,$pcnt); |
141 |
|
|
142 |
|
my $scr = $self->screen || confess "need screen"; |
143 |
|
|
144 |
|
my $status_text = sprintf("pos: %-3d sel_pos: %-3d top_screen_line: %-3d", $pos, $sel_pos, $top_screen_line ); |
145 |
|
|
146 |
|
$status_text .= ' ' . $self->status_text if $self->status_text; |
147 |
|
|
148 |
$scr->at($scr->rows - $status_lines + 1,0)->reverse()->puts( |
$scr->at($scr->rows - $status_lines + 1,0)->reverse()->puts( |
149 |
sprintf(' %-'.($scr->cols - length($pos_txt) - 2).'s ',$status_text) |
sprintf(' %-'.($scr->cols - length($pos_txt) - 2).'s ',$status_text) |
150 |
.$pos_txt)->normal(); |
.$pos_txt)->normal(); |
151 |
|
|
152 |
$scr->at($scr->rows - $status_lines + 2,0)->puts( |
$scr->at($scr->rows - $status_lines + 2,0)->puts( |
153 |
sprintf('%-'.$scr->cols.'s', $error_text) |
sprintf('%-'.$scr->cols.'s', $self->error_text) |
154 |
); |
) if $self->error_text; |
155 |
} |
} |
156 |
|
|
157 |
|
=head2 selected |
158 |
|
|
159 |
|
Move selection (or refresh it) |
160 |
|
|
161 |
|
$iselect->selected( +1 ); |
162 |
|
$iselect->selected( -1 ); |
163 |
|
$iselect->selected( 0 ); |
164 |
|
|
165 |
|
=cut |
166 |
|
|
167 |
sub selected { |
sub selected { |
168 |
|
my $self = shift; |
169 |
|
|
170 |
my $d = shift || 0; |
my $d = shift || 0; |
171 |
|
|
172 |
my $screen_line = $pos - $top_screen_line; |
my $screen_line = $pos - $top_screen_line; |
173 |
|
|
174 |
redraw_line( $screen_line, $lines[$pos] ); |
$self->redraw_line( $screen_line, $self->lines->[$pos] ); |
175 |
|
|
176 |
my $last_screen_line = $scr->rows - $status_lines; |
my $last_screen_line = $self->screen->rows - $status_lines; |
177 |
|
|
178 |
if ( $d < 0 && $screen_line == 0 ) { |
if ( $d < 0 && $screen_line == 0 ) { |
179 |
if ( $pos > 0 ) { |
if ( $pos > 0 ) { |
180 |
$top_screen_line--; |
$top_screen_line--; |
181 |
$pos--; |
$pos--; |
182 |
|
$self->redraw_screen; |
183 |
} else { |
} else { |
184 |
$error_text = "Already at Begin."; |
$self->error_text( "Already at Begin." ); |
185 |
} |
} |
|
redraw; |
|
186 |
} elsif ( $d > 0 && $screen_line == $last_screen_line ) { |
} elsif ( $d > 0 && $screen_line == $last_screen_line ) { |
187 |
if ( $pos < $#lines ) { |
if ( $pos < $nr_lines ) { |
188 |
$top_screen_line++; |
$top_screen_line++; |
189 |
$pos++; |
$pos++; |
190 |
|
$self->redraw_screen; |
191 |
} else { |
} else { |
192 |
$error_text = "Already at End."; |
$self->error_text( "Already at End." ); |
193 |
} |
} |
|
redraw; |
|
194 |
} else { |
} else { |
195 |
$pos += $d; |
$pos += $d; |
196 |
} |
} |
197 |
|
|
198 |
my $line = $lines[$pos]; |
my $line = $self->lines->[$pos]; |
199 |
if ( defined $selectable_line->{ $pos } ) { |
if ( defined $selectable_line->{ $pos } ) { |
200 |
$scr->at($pos - $top_screen_line,0)->reverse->bold()->puts( full_line( chunk($line) ) )->normal(); |
$self->screen->at($pos - $top_screen_line,0)->reverse->bold()->puts( $self->full_line( $line ) )->normal(); |
201 |
$sel_pos = $pos; |
$sel_pos = $pos; |
202 |
} else { |
} else { |
203 |
$scr->at($pos - $top_screen_line,0)->reverse->puts( full_line( chunk($line) ) ); |
$self->screen->at($pos - $top_screen_line,0)->reverse->puts( $self->full_line( $line ) ); |
204 |
$sel_pos = -1; |
$sel_pos = -1; |
205 |
} |
} |
206 |
status; |
$self->redraw_statusline; |
207 |
} |
} |
208 |
|
|
209 |
|
|
210 |
=head2 screen |
=head2 loop |
211 |
|
|
212 |
Term::ISelect->screen( |
$iselect->loop( |
213 |
sub { |
sub { |
214 |
my $line = shift; |
my $line = shift; |
215 |
warn "got line: $line\n"; |
warn "got line: $line\n"; |
216 |
}, |
} |
|
qw/various lines to be used/, |
|
217 |
); |
); |
218 |
|
|
219 |
=cut |
=cut |
220 |
|
|
221 |
sub screen { |
sub loop { |
222 |
my $class = shift; |
my $self = shift; |
223 |
|
|
224 |
my $callback = shift; |
my $callback = shift; |
225 |
confess "expect callback as first arg" unless ref($callback) eq 'CODE'; |
confess "expect callback as first arg" unless ref($callback) eq 'CODE'; |
226 |
|
|
227 |
@lines = @_; |
my @lines = @{ $self->lines }; |
228 |
|
|
229 |
# find which lines are selectable in input file |
# find which lines are selectable in input file |
230 |
for my $l (0 .. $#lines) { |
for my $l (0 .. $#lines) { |
239 |
warn "selected first selectable line $sel_pos"; |
warn "selected first selectable line $sel_pos"; |
240 |
} |
} |
241 |
|
|
242 |
$scr = new Term::Screen || die "can't init Term::Screen"; |
$self->open_screen unless $self->screen; |
|
$scr->clrscr()->noecho(); |
|
|
redraw; |
|
|
selected; |
|
243 |
|
|
244 |
while(my $key = $scr->getch()) { |
$self->screen->clrscr()->noecho(); |
245 |
|
$self->redraw_screen; |
246 |
|
$self->selected; |
247 |
|
|
248 |
$error_text = ""; |
while(my $key = $self->screen->getch()) { |
249 |
|
|
250 |
my $lines_on_screen = $scr->rows - $status_lines; |
my $lines_on_screen = $self->screen->rows - $status_lines; |
251 |
|
|
252 |
if ($key eq 'ku') { |
if ($key eq 'ku') { |
253 |
selected( -1 ); |
$self->selected( -1 ); |
254 |
} elsif ($key eq 'kd') { |
} elsif ($key eq 'kd') { |
255 |
selected( +1 ); |
$self->selected( +1 ); |
256 |
} elsif ($key eq 'pgup' ) { |
} elsif ($key eq 'pgup' ) { |
257 |
# first line on screen? |
# first line on screen? |
258 |
if ( $pos == $top_screen_line ) { |
if ( $pos == $top_screen_line ) { |
259 |
$top_screen_line -= $lines_on_screen; |
$top_screen_line -= $lines_on_screen; |
260 |
$top_screen_line = 0 if $top_screen_line < 0; |
$top_screen_line = 0 if $top_screen_line < 0; |
261 |
redraw; |
$self->redraw_screen; |
262 |
} |
} |
263 |
selected( -( $pos - $top_screen_line ) ); |
$self->selected( -( $pos - $top_screen_line ) ); |
264 |
} elsif ($key eq 'pgdn' ) { |
} elsif ($key eq 'pgdn' ) { |
265 |
# last line on screen? |
# last line on screen? |
266 |
if ( $pos - $top_screen_line == $lines_on_screen ) { |
if ( $pos - $top_screen_line == $lines_on_screen ) { |
267 |
$top_screen_line += $lines_on_screen; |
$top_screen_line += $lines_on_screen; |
268 |
$top_screen_line = $#lines - $lines_on_screen if $top_screen_line >= $#lines - $lines_on_screen; |
|
269 |
redraw; |
my $max_top_screen_line = |
270 |
|
$nr_lines > $lines_on_screen ? $nr_lines - $lines_on_screen : 0; |
271 |
|
|
272 |
|
$top_screen_line = $max_top_screen_line if $top_screen_line > $max_top_screen_line; |
273 |
|
warn "max_top_screen_line = $max_top_screen_line top_screen_line = $top_screen_line\n"; |
274 |
|
$self->redraw_screen; |
275 |
} |
} |
276 |
selected( $top_screen_line + $lines_on_screen - $pos ); |
$self->selected( $top_screen_line + $lines_on_screen - $pos ); |
277 |
} |
} |
278 |
|
|
|
$status_text = sprintf("pos: %-3d sel_pos: %-3d top_screen_line: %-3d", $pos, $sel_pos, $top_screen_line ); |
|
279 |
if ( length($key) > 1 ) { |
if ( length($key) > 1 ) { |
280 |
$status_text .= " key: $key"; |
$self->status_text("key: $key"); |
281 |
} else { |
} else { |
282 |
$status_text .= sprintf("key: %s [%03d][%02x]", $key =~ m/\w/ ? $key : '?' , ord($key), ord($key) ); |
$self->status_text( sprintf("key: %s [%03d][%02x]", $key =~ m/\w/ ? $key : '?' , ord($key), ord($key) ) ); |
283 |
} |
} |
284 |
|
|
285 |
# CTRL+L |
# CTRL+L |
286 |
redraw if ord($key) eq 0x0c; |
$self->redraw_screen if ord($key) eq 0x0c; |
287 |
|
|
288 |
# Enter |
# Enter |
289 |
if ( ord($key) eq 0x0d && $sel_pos > 0 ) { |
if ( ord($key) eq 0x0d && $sel_pos > 0 ) { |
290 |
$error_text = "execute: " . $lines[ $sel_pos ]; |
$self->error_text( "execute: " . $lines[ $sel_pos ] ); |
291 |
} |
} |
292 |
|
|
293 |
exit if (lc($key) eq 'q'); |
return if (lc($key) eq 'q'); |
294 |
|
|
295 |
status; |
$self->redraw_statusline; |
296 |
|
|
297 |
} |
} |
298 |
|
|
299 |
$scr->clrscr(); |
$self->clrscr(); |
300 |
} |
} |
301 |
|
|
302 |
|
=head1 SEE ALSO |
303 |
|
|
304 |
|
L<http://www.ossp.org/pkg/tool/iselect/> - Interactive Terminal Selection |
305 |
|
written by Ralf S. Engelschall which is original implementation in C |
306 |
|
|
307 |
|
=head1 AUTHOR |
308 |
|
|
309 |
|
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |
310 |
|
|
311 |
|
=head1 COPYRIGHT & LICENSE |
312 |
|
|
313 |
|
Copyright 2006-2007 Dobrica Pavlinusic, All Rights Reserved. |
314 |
|
|
315 |
|
This program is free software; you can redistribute it and/or modify it |
316 |
|
under the same terms as Perl itself. |
317 |
|
|
318 |
|
=cut |
319 |
|
|
320 |
1; |
1; |