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'; |
30 |
} |
} |
31 |
close($ps); |
close($ps); |
32 |
|
|
33 |
|
$data .= "\n--EOF--"; |
34 |
|
|
35 |
my $scr; |
my $scr; |
36 |
|
|
37 |
# leave sane terminal if script dies |
# leave sane terminal if script dies |
42 |
|
|
43 |
my @lines = split(/\n/, $data); |
my @lines = split(/\n/, $data); |
44 |
|
|
45 |
my $o = 0; # offset in original text |
my $top_screen_line = 0; # offset in original text |
46 |
my $pos = 0; |
my $pos = 0; |
47 |
|
|
48 |
# default: select first line |
# default: select first line |
50 |
my $status_text = ''; |
my $status_text = ''; |
51 |
my $error_text = ''; |
my $error_text = ''; |
52 |
|
|
53 |
|
my $status_lines = 3; |
54 |
|
|
55 |
# find which lines are selectable in input file |
# find which lines are selectable in input file |
56 |
my $selectable_line; |
my $selectable_line; |
57 |
|
|
58 |
for my $l (0 .. $#lines) { |
for my $l (0 .. $#lines) { |
59 |
if ($lines[$l] !~ s/^{s}//) { |
if ($lines[$l] =~ s/^{s}//) { |
60 |
$selectable_line->{$l}++; |
$selectable_line->{$l}++; |
61 |
} |
} |
62 |
} |
} |
76 |
|
|
77 |
sub chunk { |
sub chunk { |
78 |
my $t = shift; |
my $t = shift; |
79 |
return unless length($t) > 2; |
cluck "expected line" unless defined $t; |
80 |
return substr($t,1,$scr->cols); |
return substr($t,0,$scr->cols); |
81 |
} |
} |
82 |
|
|
83 |
sub redraw_line { |
sub redraw_line { |
91 |
} |
} |
92 |
|
|
93 |
sub redraw { |
sub redraw { |
94 |
for my $l (0 .. $scr->rows - 3) { |
for my $l (0 .. $scr->rows - $status_lines) { |
95 |
my $line = $lines[ $l + $o ]; |
my $line = $lines[ $l + $top_screen_line ]; |
|
next if (length($line) < 2); |
|
96 |
redraw_line( $l, $line ); |
redraw_line( $l, $line ); |
97 |
last if ($l == $#lines); |
last if ($l == $#lines); |
98 |
} |
} |
99 |
|
selected(0); |
100 |
} |
} |
101 |
|
|
102 |
sub status { |
sub status { |
103 |
my $pcnt = int(($pos || 0) * 100 / ($#lines || 1)); |
my $pcnt = int(($pos || 0) * 100 / ($#lines || 1)); |
104 |
my $pos_txt = sprintf('%d, %d%% ',$pos,$pcnt); |
my $pos_txt = sprintf('%d, %d%% ',$pos,$pcnt); |
105 |
|
|
106 |
$scr->at($scr->rows - 2,0)->reverse()->puts( |
$scr->at($scr->rows - $status_lines + 1,0)->reverse()->puts( |
107 |
sprintf(' %-'.($scr->cols - length($pos_txt) - 2).'s ',$status_text) |
sprintf(' %-'.($scr->cols - length($pos_txt) - 2).'s ',$status_text) |
108 |
.$pos_txt)->normal(); |
.$pos_txt)->normal(); |
109 |
$scr->at($scr->rows - 1,0)->puts( |
$scr->at($scr->rows - $status_lines + 2,0)->puts( |
110 |
sprintf('%-'.$scr->cols.'s', $error_text) |
sprintf('%-'.$scr->cols.'s', $error_text) |
111 |
); |
); |
112 |
} |
} |
114 |
sub selected { |
sub selected { |
115 |
my $d = shift || 0; |
my $d = shift || 0; |
116 |
|
|
117 |
my $screen_line = $pos - $o; |
my $screen_line = $pos - $top_screen_line; |
118 |
|
|
119 |
redraw_line( $screen_line, $lines[$pos] ); |
redraw_line( $screen_line, $lines[$pos] ); |
120 |
|
|
121 |
my $last_screen_line = $scr->rows - 3; |
my $last_screen_line = $scr->rows - $status_lines; |
122 |
|
|
123 |
if ( $d < 0 && $screen_line == 0 ) { |
if ( $d < 0 && $screen_line == 0 ) { |
124 |
if ( $pos > 0 ) { |
if ( $pos > 0 ) { |
125 |
$o--; |
$top_screen_line--; |
126 |
|
$pos--; |
127 |
} else { |
} else { |
128 |
$error_text = "Already at Begin."; |
$error_text = "Already at Begin."; |
129 |
} |
} |
130 |
redraw; |
redraw; |
131 |
} elsif ( $d > 0 && $screen_line == $last_screen_line ) { |
} elsif ( $d > 0 && $screen_line == $last_screen_line ) { |
132 |
if ( $pos <= $#lines ) { |
if ( $pos < $#lines ) { |
133 |
$o++; |
$top_screen_line++; |
134 |
|
$pos++; |
135 |
} else { |
} else { |
136 |
$error_text = "Already at End."; |
$error_text = "Already at End."; |
137 |
} |
} |
142 |
|
|
143 |
my $line = $lines[$pos]; |
my $line = $lines[$pos]; |
144 |
if ( defined $selectable_line->{ $pos } ) { |
if ( defined $selectable_line->{ $pos } ) { |
145 |
$scr->at($pos - $o,0)->reverse->bold()->puts( full_line( chunk($line) ) )->normal(); |
$scr->at($pos - $top_screen_line,0)->reverse->bold()->puts( full_line( chunk($line) ) )->normal(); |
146 |
$sel_pos = $pos; |
$sel_pos = $pos; |
147 |
} else { |
} else { |
148 |
$scr->at($pos - $o,0)->reverse->puts( full_line( chunk($line) ) ); |
$scr->at($pos - $top_screen_line,0)->reverse->puts( full_line( chunk($line) ) ); |
149 |
$sel_pos = -1; |
$sel_pos = -1; |
150 |
} |
} |
151 |
status; |
status; |
160 |
|
|
161 |
$error_text = ""; |
$error_text = ""; |
162 |
|
|
163 |
|
my $lines_on_screen = $scr->rows - $status_lines; |
164 |
|
|
165 |
if ($key eq 'ku') { |
if ($key eq 'ku') { |
166 |
selected( -1 ); |
selected( -1 ); |
167 |
} elsif ($key eq 'kd') { |
} elsif ($key eq 'kd') { |
168 |
selected( +1 ); |
selected( +1 ); |
169 |
|
} elsif ($key eq 'pgup' ) { |
170 |
|
# first line on screen? |
171 |
|
if ( $pos == $top_screen_line ) { |
172 |
|
$top_screen_line -= $lines_on_screen; |
173 |
|
$top_screen_line = 0 if $top_screen_line < 0; |
174 |
|
redraw; |
175 |
|
} |
176 |
|
selected( -( $pos - $top_screen_line ) ); |
177 |
|
} elsif ($key eq 'pgdn' ) { |
178 |
|
# last line on screen? |
179 |
|
if ( $pos - $top_screen_line == $lines_on_screen ) { |
180 |
|
$top_screen_line += $lines_on_screen; |
181 |
|
$top_screen_line = $#lines - $lines_on_screen if $top_screen_line >= $#lines - $lines_on_screen; |
182 |
|
redraw; |
183 |
|
} |
184 |
|
selected( $top_screen_line + $lines_on_screen - $pos ); |
185 |
} |
} |
186 |
|
|
187 |
$status_text = sprintf("pos: %-3d sel_pos: %-3d top offset: %-3d", $pos, $sel_pos, $o ); |
$status_text = sprintf("pos: %-3d sel_pos: %-3d top_screen_line: %-3d", $pos, $sel_pos, $top_screen_line ); |
188 |
if ( length($key) > 1 ) { |
if ( length($key) > 1 ) { |
189 |
$status_text .= " key: $key"; |
$status_text .= " key: $key"; |
190 |
} else { |
} else { |
193 |
|
|
194 |
status; |
status; |
195 |
|
|
196 |
redraw if lc($key) eq 'r'; |
# CTRL+L |
197 |
|
redraw if ord($key) eq 0x0c; |
198 |
|
|
199 |
exit if (lc($key) eq 'q'); |
exit if (lc($key) eq 'q'); |
200 |
} |
} |