2 |
|
|
3 |
use strict; |
use strict; |
4 |
use Term::Screen; |
use Term::Screen; |
5 |
use Carp qw/confess/; |
use Carp qw/cluck/; |
6 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
7 |
|
|
8 |
my $data = <<'EOF'; |
my $data = <<'EOF'; |
23 |
|
|
24 |
EOF |
EOF |
25 |
|
|
26 |
open(my $ps, "ps ax |") || die "can't do ps ax: $!"; |
$data .= ( rand(10) < 5 ? '{s}' : '' ) . "foobar $_\n" foreach ( 1 .. 300 ); |
|
while(<$ps>) { |
|
|
$data .= '{s}'.$_; |
|
|
$data .= $_; |
|
|
} |
|
|
close($ps); |
|
27 |
|
|
28 |
$data .= "\n--EOF--"; |
$data .= "\n--EOF--"; |
29 |
|
|
45 |
my $status_text = ''; |
my $status_text = ''; |
46 |
my $error_text = ''; |
my $error_text = ''; |
47 |
|
|
48 |
|
my $status_lines = 3; |
49 |
|
|
50 |
# find which lines are selectable in input file |
# find which lines are selectable in input file |
51 |
my $selectable_line; |
my $selectable_line; |
52 |
|
|
53 |
for my $l (0 .. $#lines) { |
for my $l (0 .. $#lines) { |
54 |
if ($lines[$l] !~ s/^{s}//) { |
if ($lines[$l] =~ s/^{s}//) { |
55 |
$selectable_line->{$l}++; |
$selectable_line->{$l}++; |
56 |
} |
} |
57 |
} |
} |
71 |
|
|
72 |
sub chunk { |
sub chunk { |
73 |
my $t = shift; |
my $t = shift; |
74 |
return unless length($t) > 2; |
cluck "expected line" unless defined $t; |
75 |
return substr($t,1,$scr->cols); |
return substr($t,0,$scr->cols); |
76 |
} |
} |
77 |
|
|
78 |
sub redraw_line { |
sub redraw_line { |
79 |
my ($l,$line) = @_; |
my ($l,$line) = @_; |
80 |
|
|
81 |
if ( defined $selectable_line->{ $l } ) { |
if ( defined $selectable_line->{ $l + $top_screen_line } ) { |
82 |
$scr->at($l,0)->bold()->puts( full_line( chunk($line) ) )->normal(); |
$scr->at($l,0)->bold()->puts( full_line( chunk($line) ) )->normal(); |
83 |
} else { |
} else { |
84 |
$scr->at($l,0)->puts( full_line( chunk($line) ) ); |
$scr->at($l,0)->puts( full_line( chunk($line) ) ); |
86 |
} |
} |
87 |
|
|
88 |
sub redraw { |
sub redraw { |
89 |
for my $l (0 .. $scr->rows - 3) { |
for my $l (0 .. $scr->rows - $status_lines) { |
90 |
my $line = $lines[ $l + $top_screen_line ]; |
my $line = $lines[ $l + $top_screen_line ]; |
91 |
redraw_line( $l, $line ); |
redraw_line( $l, $line ); |
92 |
last if ($l == $#lines); |
last if ($l == $#lines); |
93 |
} |
} |
94 |
|
selected(0); |
95 |
} |
} |
96 |
|
|
97 |
sub status { |
sub status { |
98 |
my $pcnt = int(($pos || 0) * 100 / ($#lines || 1)); |
my $pcnt = int(($pos || 0) * 100 / ($#lines || 1)); |
99 |
my $pos_txt = sprintf('%d, %d%% ',$pos,$pcnt); |
my $pos_txt = sprintf('%d, %d%% ',$pos,$pcnt); |
100 |
|
|
101 |
$scr->at($scr->rows - 2,0)->reverse()->puts( |
$scr->at($scr->rows - $status_lines + 1,0)->reverse()->puts( |
102 |
sprintf(' %-'.($scr->cols - length($pos_txt) - 2).'s ',$status_text) |
sprintf(' %-'.($scr->cols - length($pos_txt) - 2).'s ',$status_text) |
103 |
.$pos_txt)->normal(); |
.$pos_txt)->normal(); |
104 |
$scr->at($scr->rows - 1,0)->puts( |
$scr->at($scr->rows - $status_lines + 2,0)->puts( |
105 |
sprintf('%-'.$scr->cols.'s', $error_text) |
sprintf('%-'.$scr->cols.'s', $error_text) |
106 |
); |
); |
107 |
} |
} |
113 |
|
|
114 |
redraw_line( $screen_line, $lines[$pos] ); |
redraw_line( $screen_line, $lines[$pos] ); |
115 |
|
|
116 |
my $last_screen_line = $scr->rows - 3; |
my $last_screen_line = $scr->rows - $status_lines; |
117 |
|
|
118 |
if ( $d < 0 && $screen_line == 0 ) { |
if ( $d < 0 && $screen_line == 0 ) { |
119 |
if ( $pos > 0 ) { |
if ( $pos > 0 ) { |
124 |
} |
} |
125 |
redraw; |
redraw; |
126 |
} elsif ( $d > 0 && $screen_line == $last_screen_line ) { |
} elsif ( $d > 0 && $screen_line == $last_screen_line ) { |
127 |
if ( $pos <= $#lines ) { |
if ( $pos < $#lines ) { |
128 |
$top_screen_line++; |
$top_screen_line++; |
129 |
$pos++; |
$pos++; |
130 |
} else { |
} else { |
155 |
|
|
156 |
$error_text = ""; |
$error_text = ""; |
157 |
|
|
158 |
my $lines_on_screen = $scr->rows - 3; |
my $lines_on_screen = $scr->rows - $status_lines; |
159 |
|
|
160 |
if ($key eq 'ku') { |
if ($key eq 'ku') { |
161 |
selected( -1 ); |
selected( -1 ); |
165 |
# first line on screen? |
# first line on screen? |
166 |
if ( $pos == $top_screen_line ) { |
if ( $pos == $top_screen_line ) { |
167 |
$top_screen_line -= $lines_on_screen; |
$top_screen_line -= $lines_on_screen; |
168 |
$top_screen_line = 0; |
$top_screen_line = 0 if $top_screen_line < 0; |
|
$pos = $top_screen_line; |
|
169 |
redraw; |
redraw; |
|
selected( $pos ); |
|
|
} else { |
|
|
selected( -( $pos - $top_screen_line ) ); |
|
170 |
} |
} |
171 |
|
selected( -( $pos - $top_screen_line ) ); |
172 |
} elsif ($key eq 'pgdn' ) { |
} elsif ($key eq 'pgdn' ) { |
173 |
# last line on screen? |
# last line on screen? |
174 |
if ( $pos - $top_screen_line == $lines_on_screen ) { |
if ( $pos - $top_screen_line == $lines_on_screen ) { |
175 |
$top_screen_line += $lines_on_screen; |
$top_screen_line += $lines_on_screen; |
176 |
$top_screen_line = $#lines - $lines_on_screen if $top_screen_line > $#lines - $lines_on_screen; |
$top_screen_line = $#lines - $lines_on_screen if $top_screen_line >= $#lines - $lines_on_screen; |
|
$pos = $top_screen_line; |
|
177 |
redraw; |
redraw; |
|
selected( $pos ); |
|
|
} else { |
|
|
selected( $top_screen_line + $scr->rows - $pos - 3 ); |
|
178 |
} |
} |
179 |
|
selected( $top_screen_line + $lines_on_screen - $pos ); |
180 |
} |
} |
181 |
|
|
182 |
$status_text = sprintf("pos: %-3d sel_pos: %-3d top_screen_line: %-3d", $pos, $sel_pos, $top_screen_line ); |
$status_text = sprintf("pos: %-3d sel_pos: %-3d top_screen_line: %-3d", $pos, $sel_pos, $top_screen_line ); |
186 |
$status_text .= sprintf("key: %s [%03d][%02x]", $key =~ m/\w/ ? $key : '?' , ord($key), ord($key) ); |
$status_text .= sprintf("key: %s [%03d][%02x]", $key =~ m/\w/ ? $key : '?' , ord($key), ord($key) ); |
187 |
} |
} |
188 |
|
|
189 |
status; |
# CTRL+L |
190 |
|
redraw if ord($key) eq 0x0c; |
191 |
|
|
192 |
redraw if lc($key) eq 'r'; |
# Enter |
193 |
|
if ( ord($key) eq 0x0d && $sel_pos > 0 ) { |
194 |
|
$error_text = "execute: " . $lines[ $sel_pos ]; |
195 |
|
} |
196 |
|
|
197 |
exit if (lc($key) eq 'q'); |
exit if (lc($key) eq 'q'); |
198 |
|
|
199 |
|
status; |
200 |
|
|
201 |
} |
} |
202 |
|
|
203 |
$scr->clrscr(); |
$scr->clrscr(); |