/[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 6 by dpavlin, Thu Oct 25 13:24:02 2007 UTC revision 10 by dpavlin, Thu Oct 25 15:12:43 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 50  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;
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  }  }
# Line 75  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 {
84          my ($l,$line) = @_;          my ($l,$line) = @_;
85    
86          if ( defined $selectable_line->{ $l } ) {          if ( defined $selectable_line->{ $l + $top_screen_line } ) {
87                  $scr->at($l,0)->bold()->puts( full_line( chunk($line) ) )->normal();                  $scr->at($l,0)->bold()->puts( full_line( chunk($line) ) )->normal();
88          } else {          } else {
89                  $scr->at($l,0)->puts( full_line( chunk($line) ) );                  $scr->at($l,0)->puts( full_line( chunk($line) ) );
# Line 90  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 + $top_screen_line ];                  my $line = $lines[ $l + $top_screen_line ];
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 116  sub selected { Line 118  sub selected {
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 ) {
# Line 127  sub selected { Line 129  sub selected {
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                          $top_screen_line++;                          $top_screen_line++;
134                          $pos++;                          $pos++;
135                  } else {                  } else {
# Line 158  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 - 3;          my $lines_on_screen = $scr->rows - $status_lines;
164    
165          if ($key eq 'ku') {          if ($key eq 'ku') {
166                  selected( -1 );                  selected( -1 );
# Line 168  while(my $key = $scr->getch()) { Line 170  while(my $key = $scr->getch()) {
170                  # first line on screen?                  # first line on screen?
171                  if ( $pos == $top_screen_line ) {                  if ( $pos == $top_screen_line ) {
172                          $top_screen_line -= $lines_on_screen;                          $top_screen_line -= $lines_on_screen;
173                          $top_screen_line = 0;                          $top_screen_line = 0 if $top_screen_line < 0;
                         $pos = $top_screen_line;  
174                          redraw;                          redraw;
                         selected( $pos );  
                 } else {  
                         selected( -( $pos - $top_screen_line ) );  
175                  }                  }
176                    selected( -( $pos - $top_screen_line ) );
177          } elsif ($key eq 'pgdn' ) {          } elsif ($key eq 'pgdn' ) {
178                  # last line on screen?                  # last line on screen?
179                  if ( $pos - $top_screen_line == $lines_on_screen ) {                  if ( $pos - $top_screen_line == $lines_on_screen ) {
180                          $top_screen_line += $lines_on_screen;                          $top_screen_line += $lines_on_screen;
181                          $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;  
182                          redraw;                          redraw;
                         selected( $pos );  
                 } else {  
                         selected( $top_screen_line + $scr->rows - $pos - 3 );  
183                  }                  }
184                    selected( $top_screen_line + $lines_on_screen - $pos );
185          }          }
186    
187          $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 );
# Line 197  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.6  
changed lines
  Added in v.10

  ViewVC Help
Powered by ViewVC 1.1.26