/[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 1 by dpavlin, Thu Oct 25 11:22:18 2007 UTC revision 5 by dpavlin, Thu Oct 25 13:04:52 2007 UTC
# Line 2  Line 2 
2    
3  use strict;  use strict;
4  use Term::Screen;  use Term::Screen;
5    use Carp qw/confess/;
6  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
7    
8  my $data = <<'EOF';  my $data = <<'EOF';
9   First line   First line
10    
11  +first selectable  {s}first selectable
12  + second selectable  {s}second selectable
13    
14   a space....   a space....
15    
16                                      ...infinity and beyond                                      ...infinity and beyond
17    
18    
19  +foo  {s}foo
20  +bar  {s}bar
21    
22   bum   bum
23    
24  EOF  EOF
25    
26  open(my $ps, "ps ax |") || die "can't do ps ax: $!";  open(my $ps, "ps ax |") || die "can't do ps ax: $!";
27  while(<$ps>) {  while(<$ps>) {
28          $data .= '+'.$_;          $data .= '{s}'.$_;
29          $data .= ' '.$_;          $data .= $_;
30  }  }
31  close($ps);  close($ps);
32    
33    my $scr;
34    
35  my @lines = split(/\n/, $data);  # leave sane terminal if script dies
36    $SIG{__DIE__} = sub {
37        eval { system('stty sane'); };
38    };
39    
40  my $scr = new Term::Screen || die "can't init Term::Screen";  
41  $scr->clrscr()->noecho();  my @lines = split(/\n/, $data);
42    
43  my $o = 0;      # offset in original text  my $o = 0;      # offset in original text
44  my $pos = 0;  my $pos = 0;
45    
46    # default: select first line
47    my $sel_pos = 0;
48    my $status_text = '';
49    my $error_text = '';
50    
51    
52  # find which lines are selectable in input file  # find which lines are selectable in input file
53  my $selectable_line;  my $selectable_line;
54    
55  for my $l (0 .. $#lines) {  for my $l (0 .. $#lines) {
56          next if (length($lines[$l]) < 2);          if ($lines[$l] !~ s/^{s}//) {
         my $foo = ' ';  
         if ($lines[$l] !~ m/^\s/o) {  
57                  $selectable_line->{$l}++;                  $selectable_line->{$l}++;
                 $foo = '*';  
58          }          }
59          warn "$l: $foo $lines[$l]\n";  }
60    
61    # select first selectable line
62    
63    if ( $selectable_line ) {
64            $pos = $sel_pos = (sort { $a <=> $b } keys %$selectable_line)[0];
65            warn "selected first selectable line $sel_pos";
66  }  }
67    
68  sub full_line {  sub full_line {
69          my $t = shift;          my $t = shift;
70          my $l = length($t);          $t = '' unless defined $t;
71          return $t . (" " x ($scr->cols - length($t)));          return $t . (" " x ($scr->cols - length($t)));
72  }  }
73    
74  sub chunk {  sub chunk {
75          my $t = shift;          my $t = shift;
76          my $o = '';          return unless length($t) > 2;
77          $o = substr($t,1,$scr->cols) if length($t) > 1;          return substr($t,1,$scr->cols);
78          return $o . ( ' ' x ( $scr->cols - length($o) - 1 ) );  }
79    
80    sub redraw_line {
81            my ($l,$line) = @_;
82    
83            if ( defined $selectable_line->{ $l } ) {
84                    $scr->at($l,0)->bold()->puts( full_line( chunk($line) ) )->normal();
85            } else {
86                    $scr->at($l,0)->puts( full_line( chunk($line) ) );
87            }
88  }  }
89    
90  sub redraw {  sub redraw {
91          for my $l (0 .. $scr->rows) {          for my $l (0 .. $scr->rows - 3) {
92                  my $line = $lines[ $l + $o ] || '';                  my $line = $lines[ $l + $o ];
93                  next if (length($line) < 2);                  next if (length($line) < 2);
94                  if (substr($line,0,1) !~ m/^\s/o) {                  redraw_line( $l, $line );
                         $scr->at($l,0)->bold()->puts( full_line( chunk($line) ) )->normal();  
                 } else {  
                         $scr->at($l,0)->puts( full_line( chunk($line) ) );  
                 }  
95                  last if ($l == $#lines);                  last if ($l == $#lines);
96          }          }
97  }  }
98    
 # default: select first line  
 my $sel_pos = 0;  
 my $status_text = '';  
 my $error_text = '';  
 $pos = 0;  
   
99  sub status {  sub status {
100          my $pcnt = int(($pos || 0) * 100 / ($#lines || 1));          my $pcnt = int(($pos || 0) * 100 / ($#lines || 1));
101          my $pos_txt = sprintf('%d, %d%% ',$pos,$pcnt);          my $pos_txt = sprintf('%d, %d%% ',$pos,$pcnt);
# Line 95  sub status { Line 109  sub status {
109  }  }
110    
111  sub selected {  sub selected {
112          my $d = shift || return;          my $d = shift || 0;
113    
114          if ( $selectable_line->{ $pos } ) {          my $screen_line = $pos - $o;
115                  $scr->at($pos-$o,0)->bold()->puts( chunk($lines[$pos]) )->normal();  
116          } else {          redraw_line( $screen_line, $lines[$pos] );
                 $scr->at($pos-$o,0)->puts( chunk($lines[$pos]) )->normal();  
         }  
         $pos += $d;  
117    
118          my $max_row = $scr->rows - 3;          my $last_screen_line = $scr->rows - 3;
119    
120          if ($pos < 1) {          if ( $d < 0 && $screen_line == 0 ) {
121                  $error_text = "Already at Begin.";                  if ( $pos > 0 ) {
122                  $pos = 0;                          $o--;
123                            $pos--;
124                    } else {
125                            $error_text = "Already at Begin.";
126                    }
127                  redraw;                  redraw;
128          } elsif ($pos > $max_row) {          } elsif ( $d > 0 && $screen_line == $last_screen_line ) {
129                  $o = $pos - $max_row;   # put selected line on last                  if ( $pos <= $#lines ) {
130                  $error_text = "Already at End.";                          $o++;
131                            $pos++;
132                    } else {
133                            $error_text = "Already at End.";
134                    }
135                  redraw;                  redraw;
136            } else {
137                    $pos += $d;
138          }          }
139    
140          $scr->at($pos-$o,0)->reverse()->puts(chunk($lines[$pos]))->normal();          my $line = $lines[$pos];
141            if ( defined $selectable_line->{ $pos } ) {
142                    $scr->at($pos - $o,0)->reverse->bold()->puts( full_line( chunk($line) ) )->normal();
143                    $sel_pos = $pos;
144            } else {
145                    $scr->at($pos - $o,0)->reverse->puts( full_line( chunk($line) ) );
146                    $sel_pos = -1;
147            }
148          status;          status;
149  }  }
150    
151  $status_text = "let's see does it work?";  $scr = new Term::Screen || die "can't init Term::Screen";
152    $scr->clrscr()->noecho();
153  redraw;  redraw;
154  selected;  selected;
155    
156  while(my $key = $scr->getch()) {  while(my $key = $scr->getch()) {
157    
         $status_text = "key: $key pos: $pos sel_pos: $sel_pos";  
158          $error_text = "";          $error_text = "";
159    
160          if ($key eq 'ku') {          if ($key eq 'ku') {
# Line 135  while(my $key = $scr->getch()) { Line 163  while(my $key = $scr->getch()) {
163                  selected( +1 );                  selected( +1 );
164          }          }
165    
166            $status_text = sprintf("pos: %-3d sel_pos: %-3d top offset: %-3d", $pos, $sel_pos, $o );
167            if ( length($key) > 1 ) {
168                    $status_text .= " key: $key";
169            } else {
170                    $status_text .= sprintf("key: %s [%03d][%02x]", $key =~ m/\w/ ? $key : '?' , ord($key), ord($key) );
171            }
172    
173          status;          status;
174    
175            redraw if lc($key) eq 'r';
176    
177          exit if (lc($key) eq 'q');          exit if (lc($key) eq 'q');
178  }  }
179    

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

  ViewVC Help
Powered by ViewVC 1.1.26