http://qs1969.pair.com?node_id=53240


in reply to newer snippet browser

Here is what I came up with after a quick run through at your request:

#!/usr/bin/perl -w =head Changes Be more consistant with your spacing. I don't see the use for which() since you are just duplicating code th +at the kernel will do but less portably (under Win32, you need to spl +it on ';', not on ':'). Used a more "standard" method for picking an editor. Since you only have one enclosing loop, you don't need the STUDY: labe +l, but I don't mind you keeping it since it makes it easy to find tha +t loop. On non-Unix systems, $ENV{HOME} may not be set. Declaring subs in the middle of code just makes them hard to find. Added a default for $ENV{SANDBOX} s/[A-Za-z]/0/g more efficient as tr/A-Za-z/0/ Allow easy highlight with a color other than 'GREEN' Replace repeated highlighting code with a subroutine Treat $hide_help and $clear as Booleans instead of integers Your first "next" doesn't do anything (I just commented it out) Your first "redo" doesn't do anything (commented out) Don't have a [my] variable with the same name as a global, STUDYPATH close SCRIPT when you are done with it =cut use strict; use Term::ANSIColor 'color'; my $EDITOR = $ENV{VISUAL} || $ENV{EDITOR} || $ENV{EDIT} or $^O =~ /^MSWin/ ? 'notepad' : 'vi'; my $index = @ARGV ? shift(@ARGV) :0; my $clear = 1; my $hide_help = 0; my $highlight = $ENV{STUDYCOLOR} || 'GREEN'; my $homedir = $ENV{HOME} || '.'; my $sandbox = $ENV{SANDBOX} || 'sandbox'; my $STUDYPATH = "$homedir/$sandbox"; sub change_snippet_dir{ my $sandbox= shift; my $path= "$homedir/$sandbox"; if( -d $path ) { $STUDYPATH= $path; } } sub highlight { return color($highlight), @_, color('reset'); } STUDY: while (1) { my @files= glob "$STUDYPATH/*"; if( $clear ) { system ('clear'); } else { $clear++; } $index =~ tr/A-Za-z/0/; $index= 0 if $index < (0-$#files)-1 || $index > $#files; printf( "%50s <- (%5s)\n", $files[$index], $index ); print " ",highlight("[C]"),"HANGE editor. Currently $EDITOR ",highlight("[E]"),"DIT to open the snippet in an editor ",highlight("[G]"),"oTo to select file index: -", ($#files+1)," to + $#files ",highlight("[H]"),"ELP to (un)hide this menu ",highlight("[N]"),"EXT to skip this snippet ",highlight("[R]"),"EDO to revisit the previous snippet ",highlight("[Q]"),"UIT to exit e",highlight("[X]"),"ecute Any other key +",highlight(" [ENTER] "),"to print out the snippet -> " unless $hide_help; my $input = <>; if ($input =~ /^N/i){ ++$index; # next; This doesn't do anything }elsif($input =~ /^G/i){ print "enter a number from -",$#files+1," to $#files -> "; $index = <>; chomp $index; }elsif($input =~ /^H/i){ $hide_help= ! $hide_help; }elsif($input =~ /^C/i){ $EDITOR = <>; chomp $EDITOR; # redo STUDY; }elsif($input =~ /^R/i){ --$index; }elsif($input =~ /^(SANDBOX)/i){ print "$homedir/"; my $sandbox=<>; chomp $sandbox; change_snippet_dir($sandbox); }elsif($input =~ /^E/i){ system( $EDITOR, $files[$index] ); }elsif($input =~ /^X/i){ do $files[$index]; print "\n"; $clear= 0; }elsif($input =~ /^Q/i){ last; }else{ $clear= 0; open (SCRIPT,"< $files[$index]") or warn "can't open file: $!"; print while <SCRIPT>; close SCRIPT; } }
        - tye (but my friends call me "Tye")

Replies are listed 'Best First'.
Re: (tye)Re: newer snippet browser
by mkmcconn (Chaplain) on Jan 21, 2001 at 05:51 UTC

    This is just terrific stuff, tye. Like tilly's criticisms, I understand what you wrote, and I can agree with everything you said. I'm grateful that you both took so much time to make detailed comments. I'll try to make good use of your advice, not just in improving this simple program but in thinking more carefully about portability, maintainability, and choosing the appropriate data structures.

    Hopefully the light will come up, little by little, and you'll see some smarter programs from me over time!
    mkmcconn