/[iselect]/iselect.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /iselect.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 5 by dpavlin, Thu Oct 25 13:04:52 2007 UTC revision 8 by dpavlin, Thu Oct 25 14:52:56 2007 UTC
# Line 2  Line 2 
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';
# Line 30  while(<$ps>) { Line 30  while(<$ps>) {
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
# Line 40  $SIG{__DIE__} = sub { Line 42  $SIG{__DIE__} = sub {
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
# Line 48  my $sel_pos = 0; Line 50  my $sel_pos = 0;
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;
# Line 73  sub full_line { Line 76  sub full_line {
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 {
# Line 88  sub redraw_line { Line 91  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  }  }
# Line 111  sub status { Line 114  sub status {
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--;                          $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++;                          $pos++;
135                  } else {                  } else {
136                          $error_text = "Already at End.";                          $error_text = "Already at End.";
# Line 139  sub selected { Line 142  sub selected {
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;
# Line 157  while(my $key = $scr->getch()) { Line 160  while(my $key = $scr->getch()) {
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 {
# Line 172  while(my $key = $scr->getch()) { Line 193  while(my $key = $scr->getch()) {
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  }  }

Legend:
Removed from v.5  
changed lines
  Added in v.8

  ViewVC Help
Powered by ViewVC 1.1.26