/[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 11 by dpavlin, Thu Oct 25 15:19:51 2007 UTC
# Line 2  Line 2 
2    
3  use strict;  use strict;
4  use Term::Screen;  use Term::Screen;
5    use Carp qw/cluck/;
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: $!";  $data .= ( rand(10) < 5 ? '{s}' : '' ) . "foobar $_\n" foreach ( 1 .. 300 );
 while(<$ps>) {  
         $data .= '+'.$_;  
         $data .= ' '.$_;  
 }  
 close($ps);  
27    
28    $data .= "\n--EOF--";
29    
30  my @lines = split(/\n/, $data);  my $scr;
31    
32  my $scr = new Term::Screen || die "can't init Term::Screen";  # leave sane terminal if script dies
33  $scr->clrscr()->noecho();  $SIG{__DIE__} = sub {
34        eval { system('stty sane'); };
35    };
36    
37    
38    my @lines = split(/\n/, $data);
39    
40  my $o = 0;      # offset in original text  my $top_screen_line = 0;        # offset in original text
41  my $pos = 0;  my $pos = 0;
42    
43    # default: select first line
44    my $sel_pos = 0;
45    my $status_text = '';
46    my $error_text = '';
47    
48    my $status_lines = 3;
49    
50  # find which lines are selectable in input file  # find which lines are selectable in input file
51  my $selectable_line;  my $selectable_line;
52    
53  for my $l (0 .. $#lines) {  for my $l (0 .. $#lines) {
54          next if (length($lines[$l]) < 2);          if ($lines[$l] =~ s/^{s}//) {
         my $foo = ' ';  
         if ($lines[$l] !~ m/^\s/o) {  
55                  $selectable_line->{$l}++;                  $selectable_line->{$l}++;
                 $foo = '*';  
56          }          }
57          warn "$l: $foo $lines[$l]\n";  }
58    
59    # select first selectable line
60    
61    if ( $selectable_line ) {
62            $pos = $sel_pos = (sort { $a <=> $b } keys %$selectable_line)[0];
63            warn "selected first selectable line $sel_pos";
64  }  }
65    
66  sub full_line {  sub full_line {
67          my $t = shift;          my $t = shift;
68          my $l = length($t);          $t = '' unless defined $t;
69          return $t . (" " x ($scr->cols - length($t)));          return $t . (" " x ($scr->cols - length($t)));
70  }  }
71    
72  sub chunk {  sub chunk {
73          my $t = shift;          my $t = shift;
74          my $o = '';          cluck "expected line" unless defined $t;
75          $o = substr($t,1,$scr->cols) if length($t) > 1;          return substr($t,0,$scr->cols);
76          return $o . ( ' ' x ( $scr->cols - length($o) - 1 ) );  }
77    
78    sub redraw_line {
79            my ($l,$line) = @_;
80    
81            if ( defined $selectable_line->{ $l + $top_screen_line } ) {
82                    $scr->at($l,0)->bold()->puts( full_line( chunk($line) ) )->normal();
83            } else {
84                    $scr->at($l,0)->puts( full_line( chunk($line) ) );
85            }
86  }  }
87    
88  sub redraw {  sub redraw {
89          for my $l (0 .. $scr->rows) {          for my $l (0 .. $scr->rows - $status_lines) {
90                  my $line = $lines[ $l + $o ] || '';                  my $line = $lines[ $l + $top_screen_line ];
91                  next if (length($line) < 2);                  redraw_line( $l, $line );
                 if (substr($line,0,1) !~ m/^\s/o) {  
                         $scr->at($l,0)->bold()->puts( full_line( chunk($line) ) )->normal();  
                 } else {  
                         $scr->at($l,0)->puts( full_line( chunk($line) ) );  
                 }  
92                  last if ($l == $#lines);                  last if ($l == $#lines);
93          }          }
94            selected(0);
95  }  }
96    
 # default: select first line  
 my $sel_pos = 0;  
 my $status_text = '';  
 my $error_text = '';  
 $pos = 0;  
   
97  sub status {  sub status {
98          my $pcnt = int(($pos || 0) * 100 / ($#lines || 1));          my $pcnt = int(($pos || 0) * 100 / ($#lines || 1));
99          my $pos_txt = sprintf('%d, %d%% ',$pos,$pcnt);          my $pos_txt = sprintf('%d, %d%% ',$pos,$pcnt);
100                    
101          $scr->at($scr->rows - 2,0)->reverse()->puts(          $scr->at($scr->rows - $status_lines + 1,0)->reverse()->puts(
102                  sprintf(' %-'.($scr->cols - length($pos_txt) - 2).'s ',$status_text)                  sprintf(' %-'.($scr->cols - length($pos_txt) - 2).'s ',$status_text)
103          .$pos_txt)->normal();          .$pos_txt)->normal();
104          $scr->at($scr->rows - 1,0)->puts(          $scr->at($scr->rows - $status_lines + 2,0)->puts(
105                  sprintf('%-'.$scr->cols.'s', $error_text)                  sprintf('%-'.$scr->cols.'s', $error_text)
106          );          );
107  }  }
108    
109  sub selected {  sub selected {
110          my $d = shift || return;          my $d = shift || 0;
111    
112          if ( $selectable_line->{ $pos } ) {          my $screen_line = $pos - $top_screen_line;
                 $scr->at($pos-$o,0)->bold()->puts( chunk($lines[$pos]) )->normal();  
         } else {  
                 $scr->at($pos-$o,0)->puts( chunk($lines[$pos]) )->normal();  
         }  
         $pos += $d;  
113    
114          my $max_row = $scr->rows - 3;          redraw_line( $screen_line, $lines[$pos] );
115    
116          if ($pos < 1) {          my $last_screen_line = $scr->rows - $status_lines;
117                  $error_text = "Already at Begin.";  
118                  $pos = 0;          if ( $d < 0 && $screen_line == 0 ) {
119                    if ( $pos > 0 ) {
120                            $top_screen_line--;
121                            $pos--;
122                    } else {
123                            $error_text = "Already at Begin.";
124                    }
125                  redraw;                  redraw;
126          } elsif ($pos > $max_row) {          } elsif ( $d > 0 && $screen_line == $last_screen_line ) {
127                  $o = $pos - $max_row;   # put selected line on last                  if ( $pos < $#lines ) {
128                  $error_text = "Already at End.";                          $top_screen_line++;
129                            $pos++;
130                    } else {
131                            $error_text = "Already at End.";
132                    }
133                  redraw;                  redraw;
134            } else {
135                    $pos += $d;
136          }          }
137    
138          $scr->at($pos-$o,0)->reverse()->puts(chunk($lines[$pos]))->normal();          my $line = $lines[$pos];
139            if ( defined $selectable_line->{ $pos } ) {
140                    $scr->at($pos - $top_screen_line,0)->reverse->bold()->puts( full_line( chunk($line) ) )->normal();
141                    $sel_pos = $pos;
142            } else {
143                    $scr->at($pos - $top_screen_line,0)->reverse->puts( full_line( chunk($line) ) );
144                    $sel_pos = -1;
145            }
146          status;          status;
147  }  }
148    
149  $status_text = "let's see does it work?";  $scr = new Term::Screen || die "can't init Term::Screen";
150    $scr->clrscr()->noecho();
151  redraw;  redraw;
152  selected;  selected;
153    
154  while(my $key = $scr->getch()) {  while(my $key = $scr->getch()) {
155    
         $status_text = "key: $key pos: $pos sel_pos: $sel_pos";  
156          $error_text = "";          $error_text = "";
157    
158            my $lines_on_screen = $scr->rows - $status_lines;
159    
160          if ($key eq 'ku') {          if ($key eq 'ku') {
161                  selected( -1 );                  selected( -1 );
162          } elsif ($key eq 'kd') {          } elsif ($key eq 'kd') {
163                  selected( +1 );                  selected( +1 );
164            } elsif ($key eq 'pgup' ) {
165                    # first line on screen?
166                    if ( $pos == $top_screen_line ) {
167                            $top_screen_line -= $lines_on_screen;
168                            $top_screen_line = 0 if $top_screen_line < 0;
169                            redraw;
170                    }
171                    selected( -( $pos - $top_screen_line ) );
172            } elsif ($key eq 'pgdn' ) {
173                    # last line on screen?
174                    if ( $pos - $top_screen_line == $lines_on_screen ) {
175                            $top_screen_line += $lines_on_screen;
176                            $top_screen_line = $#lines - $lines_on_screen if $top_screen_line >= $#lines - $lines_on_screen;
177                            redraw;
178                    }
179                    selected( $top_screen_line + $lines_on_screen - $pos );
180          }          }
181    
182          status;          $status_text = sprintf("pos: %-3d sel_pos: %-3d top_screen_line: %-3d", $pos, $sel_pos, $top_screen_line );
183            if ( length($key) > 1 ) {
184                    $status_text .= " key: $key";
185            } else {
186                    $status_text .= sprintf("key: %s [%03d][%02x]", $key =~ m/\w/ ? $key : '?' , ord($key), ord($key) );
187            }
188    
189            # CTRL+L
190            redraw if ord($key) eq 0x0c;
191    
192            # Enter
193            if ( ord($key) eq 0x0d && $sel_pos > 0 ) {
194                    $error_text = "execute: " . $lines[ $sel_pos ];
195            }
196    
197          exit if (lc($key) eq 'q');          exit if (lc($key) eq 'q');
198    
199            status;
200    
201  }  }
202    
203  $scr->clrscr();  $scr->clrscr();

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

  ViewVC Help
Powered by ViewVC 1.1.26