/[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 1 by dpavlin, Thu Oct 25 11:22:18 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/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: $!";  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    $data .= "\n--EOF--";
34    
35  my @lines = split(/\n/, $data);  my $scr;
36    
37  my $scr = new Term::Screen || die "can't init Term::Screen";  # leave sane terminal if script dies
38  $scr->clrscr()->noecho();  $SIG{__DIE__} = sub {
39        eval { system('stty sane'); };
40    };
41    
42    
43    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
49    my $sel_pos = 0;
50    my $status_text = '';
51    my $error_text = '';
52    
53    
54  # find which lines are selectable in input file  # find which lines are selectable in input file
55  my $selectable_line;  my $selectable_line;
56    
57  for my $l (0 .. $#lines) {  for my $l (0 .. $#lines) {
58          next if (length($lines[$l]) < 2);          if ($lines[$l] !~ s/^{s}//) {
         my $foo = ' ';  
         if ($lines[$l] !~ m/^\s/o) {  
59                  $selectable_line->{$l}++;                  $selectable_line->{$l}++;
                 $foo = '*';  
60          }          }
61          warn "$l: $foo $lines[$l]\n";  }
62    
63    # select first selectable line
64    
65    if ( $selectable_line ) {
66            $pos = $sel_pos = (sort { $a <=> $b } keys %$selectable_line)[0];
67            warn "selected first selectable line $sel_pos";
68  }  }
69    
70  sub full_line {  sub full_line {
71          my $t = shift;          my $t = shift;
72          my $l = length($t);          $t = '' unless defined $t;
73          return $t . (" " x ($scr->cols - length($t)));          return $t . (" " x ($scr->cols - length($t)));
74  }  }
75    
76  sub chunk {  sub chunk {
77          my $t = shift;          my $t = shift;
78          my $o = '';          cluck "expected line" unless defined $t;
79          $o = substr($t,1,$scr->cols) if length($t) > 1;          return substr($t,0,$scr->cols);
80          return $o . ( ' ' x ( $scr->cols - length($o) - 1 ) );  }
81    
82    sub redraw_line {
83            my ($l,$line) = @_;
84    
85            if ( defined $selectable_line->{ $l } ) {
86                    $scr->at($l,0)->bold()->puts( full_line( chunk($line) ) )->normal();
87            } else {
88                    $scr->at($l,0)->puts( full_line( chunk($line) ) );
89            }
90  }  }
91    
92  sub redraw {  sub redraw {
93          for my $l (0 .. $scr->rows) {          for my $l (0 .. $scr->rows - 3) {
94                  my $line = $lines[ $l + $o ] || '';                  my $line = $lines[ $l + $top_screen_line ];
95                  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) ) );  
                 }  
96                  last if ($l == $#lines);                  last if ($l == $#lines);
97          }          }
98            selected(0);
99  }  }
100    
 # default: select first line  
 my $sel_pos = 0;  
 my $status_text = '';  
 my $error_text = '';  
 $pos = 0;  
   
101  sub status {  sub status {
102          my $pcnt = int(($pos || 0) * 100 / ($#lines || 1));          my $pcnt = int(($pos || 0) * 100 / ($#lines || 1));
103          my $pos_txt = sprintf('%d, %d%% ',$pos,$pcnt);          my $pos_txt = sprintf('%d, %d%% ',$pos,$pcnt);
# Line 95  sub status { Line 111  sub status {
111  }  }
112    
113  sub selected {  sub selected {
114          my $d = shift || return;          my $d = shift || 0;
115    
116          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;  
117    
118          my $max_row = $scr->rows - 3;          redraw_line( $screen_line, $lines[$pos] );
119    
120          if ($pos < 1) {          my $last_screen_line = $scr->rows - 3;
121                  $error_text = "Already at Begin.";  
122                  $pos = 0;          if ( $d < 0 && $screen_line == 0 ) {
123                    if ( $pos > 0 ) {
124                            $top_screen_line--;
125                            $pos--;
126                    } else {
127                            $error_text = "Already at Begin.";
128                    }
129                  redraw;                  redraw;
130          } elsif ($pos > $max_row) {          } elsif ( $d > 0 && $screen_line == $last_screen_line ) {
131                  $o = $pos - $max_row;   # put selected line on last                  if ( $pos < $#lines ) {
132                  $error_text = "Already at End.";                          $top_screen_line++;
133                            $pos++;
134                    } else {
135                            $error_text = "Already at End.";
136                    }
137                  redraw;                  redraw;
138            } else {
139                    $pos += $d;
140          }          }
141    
142          $scr->at($pos-$o,0)->reverse()->puts(chunk($lines[$pos]))->normal();          my $line = $lines[$pos];
143            if ( defined $selectable_line->{ $pos } ) {
144                    $scr->at($pos - $top_screen_line,0)->reverse->bold()->puts( full_line( chunk($line) ) )->normal();
145                    $sel_pos = $pos;
146            } else {
147                    $scr->at($pos - $top_screen_line,0)->reverse->puts( full_line( chunk($line) ) );
148                    $sel_pos = -1;
149            }
150          status;          status;
151  }  }
152    
153  $status_text = "let's see does it work?";  $scr = new Term::Screen || die "can't init Term::Screen";
154    $scr->clrscr()->noecho();
155  redraw;  redraw;
156  selected;  selected;
157    
158  while(my $key = $scr->getch()) {  while(my $key = $scr->getch()) {
159    
         $status_text = "key: $key pos: $pos sel_pos: $sel_pos";  
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_screen_line: %-3d", $pos, $sel_pos, $top_screen_line );
187            if ( length($key) > 1 ) {
188                    $status_text .= " key: $key";
189            } else {
190                    $status_text .= sprintf("key: %s [%03d][%02x]", $key =~ m/\w/ ? $key : '?' , ord($key), ord($key) );
191          }          }
192    
193          status;          status;
194    
195            # CTRL+L
196            redraw if ord($key) eq 0x0c;
197    
198          exit if (lc($key) eq 'q');          exit if (lc($key) eq 'q');
199  }  }
200    

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

  ViewVC Help
Powered by ViewVC 1.1.26