/[iselect]/bin/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 /bin/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 7 by dpavlin, Thu Oct 25 14:11:42 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 73  sub full_line { Line 75  sub full_line {
75    
76  sub chunk {  sub chunk {
77          my $t = shift;          my $t = shift;
78          return unless length($t) > 2;          cluck "expected line" unless defined $t;
79          return substr($t,1,$scr->cols);          return substr($t,0,$scr->cols);
80  }  }
81    
82  sub redraw_line {  sub redraw_line {
# Line 89  sub redraw_line { Line 91  sub redraw_line {
91    
92  sub redraw {  sub redraw {
93          for my $l (0 .. $scr->rows - 3) {          for my $l (0 .. $scr->rows - 3) {
94                  my $line = $lines[ $l + $o ];                  my $line = $lines[ $l + $top_screen_line ];
                 next if (length($line) < 2);  
95                  redraw_line( $l, $line );                  redraw_line( $l, $line );
96                  last if ($l == $#lines);                  last if ($l == $#lines);
97          }          }
98            selected(0);
99  }  }
100    
101  sub status {  sub status {
# Line 111  sub status { Line 113  sub status {
113  sub selected {  sub selected {
114          my $d = shift || 0;          my $d = shift || 0;
115    
116          my $screen_line = $pos - $o;          my $screen_line = $pos - $top_screen_line;
117    
118          redraw_line( $screen_line, $lines[$pos] );          redraw_line( $screen_line, $lines[$pos] );
119    
# Line 119  sub selected { Line 121  sub selected {
121    
122          if ( $d < 0 && $screen_line == 0 ) {          if ( $d < 0 && $screen_line == 0 ) {
123                  if ( $pos > 0 ) {                  if ( $pos > 0 ) {
124                          $o--;                          $top_screen_line--;
125                          $pos--;                          $pos--;
126                  } else {                  } else {
127                          $error_text = "Already at Begin.";                          $error_text = "Already at Begin.";
128                  }                  }
129                  redraw;                  redraw;
130          } elsif ( $d > 0 && $screen_line == $last_screen_line ) {          } elsif ( $d > 0 && $screen_line == $last_screen_line ) {
131                  if ( $pos <= $#lines ) {                  if ( $pos < $#lines ) {
132                          $o++;                          $top_screen_line++;
133                          $pos++;                          $pos++;
134                  } else {                  } else {
135                          $error_text = "Already at End.";                          $error_text = "Already at End.";
# Line 139  sub selected { Line 141  sub selected {
141    
142          my $line = $lines[$pos];          my $line = $lines[$pos];
143          if ( defined $selectable_line->{ $pos } ) {          if ( defined $selectable_line->{ $pos } ) {
144                  $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();
145                  $sel_pos = $pos;                  $sel_pos = $pos;
146          } else {          } else {
147                  $scr->at($pos - $o,0)->reverse->puts( full_line( chunk($line) ) );                  $scr->at($pos - $top_screen_line,0)->reverse->puts( full_line( chunk($line) ) );
148                  $sel_pos = -1;                  $sel_pos = -1;
149          }          }
150          status;          status;
# Line 157  while(my $key = $scr->getch()) { Line 159  while(my $key = $scr->getch()) {
159    
160          $error_text = "";          $error_text = "";
161    
162            my $lines_on_screen = $scr->rows - 3;
163    
164          if ($key eq 'ku') {          if ($key eq 'ku') {
165                  selected( -1 );                  selected( -1 );
166          } elsif ($key eq 'kd') {          } elsif ($key eq 'kd') {
167                  selected( +1 );                  selected( +1 );
168            } elsif ($key eq 'pgup' ) {
169                    # first line on screen?
170                    if ( $pos == $top_screen_line ) {
171                            $top_screen_line -= $lines_on_screen;
172                            $top_screen_line = 0 if $top_screen_line < 0;
173                            redraw;
174                    }
175                    selected( -( $pos - $top_screen_line ) );
176            } elsif ($key eq 'pgdn' ) {
177                    # last line on screen?
178                    if ( $pos - $top_screen_line == $lines_on_screen ) {
179                            $top_screen_line += $lines_on_screen;
180                            $top_screen_line = $#lines - $lines_on_screen if $top_screen_line >= $#lines - $lines_on_screen;
181                            redraw;
182                    }
183                    selected( $top_screen_line + $lines_on_screen - $pos );
184          }          }
185    
186          $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 );
187          if ( length($key) > 1 ) {          if ( length($key) > 1 ) {
188                  $status_text .= " key: $key";                  $status_text .= " key: $key";
189          } else {          } else {
# Line 172  while(my $key = $scr->getch()) { Line 192  while(my $key = $scr->getch()) {
192    
193          status;          status;
194    
195          redraw if lc($key) eq 'r';          # CTRL+L
196            redraw if ord($key) eq 0x0c;
197    
198          exit if (lc($key) eq 'q');          exit if (lc($key) eq 'q');
199  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.26