--- iselect.pl 2007/10/25 11:22:18 1 +++ iselect.pl 2007/10/25 11:43:48 2 @@ -28,15 +28,25 @@ } close($ps); +my $scr; -my @lines = split(/\n/, $data); +# leave sane terminal if script dies +$SIG{__DIE__} = sub { + eval { system('stty sane'); }; +}; -my $scr = new Term::Screen || die "can't init Term::Screen"; -$scr->clrscr()->noecho(); + +my @lines = split(/\n/, $data); my $o = 0; # offset in original text my $pos = 0; +# default: select first line +my $sel_pos = 0; +my $status_text = ''; +my $error_text = ''; + + # find which lines are selectable in input file my $selectable_line; @@ -50,6 +60,13 @@ warn "$l: $foo $lines[$l]\n"; } +# select first selectable line + +if ( $selectable_line ) { + $sel_pos = (sort keys %$selectable_line)[0]; + warn "selected first selectable line $pos"; +} + sub full_line { my $t = shift; my $l = length($t); @@ -76,12 +93,6 @@ } } -# default: select first line -my $sel_pos = 0; -my $status_text = ''; -my $error_text = ''; -$pos = 0; - sub status { my $pcnt = int(($pos || 0) * 100 / ($#lines || 1)); my $pos_txt = sprintf('%d, %d%% ',$pos,$pcnt); @@ -95,7 +106,7 @@ } sub selected { - my $d = shift || return; + my $d = shift || 0; if ( $selectable_line->{ $pos } ) { $scr->at($pos-$o,0)->bold()->puts( chunk($lines[$pos]) )->normal(); @@ -120,13 +131,21 @@ status; } +$scr = new Term::Screen || die "can't init Term::Screen"; +$scr->clrscr()->noecho(); $status_text = "let's see does it work?"; redraw; selected; while(my $key = $scr->getch()) { - $status_text = "key: $key pos: $pos sel_pos: $sel_pos"; + $status_text = sprintf("pos: %-3d sel_pos: %-3d", $pos, $sel_pos ); + if ( length($key) > 1 ) { + $status_text .= " key: $key"; + } else { + $status_text .= sprintf("key: %s [%03d][%02x]", $key, ord($key), ord($key) ); + } + $error_text = ""; if ($key eq 'ku') {