Here's what I've learned since posting snippet browser. There are numerous improvements based on bbfu's comments, in that thread, but lots of things done for which I hardly know the consequences. Criticism (as well as generosity) would be appreciated. (Trying the READMORE tag - here goes...nothing?)
It's more like a full program now - and I'm pleased to say, it took less time to write this, than it did to write the original version (thanks to obsessively reading all of you ).
N.B. again, that this probably only runs on Linux. Also, as in the previous version, as written it requires a $SANDBOX environment variable, to designate a subdirectory under $HOME.
Thank you for indulging a beginner using you all as a sounding board.
- New stuff revised Sat Jan 20 at 04:16
- cleaned up a few things following tilly's advice below (for which I'm very grateful).
- Improved subroutines for color.
- Change menu colors.
- You can now specify the editor, and a subroutine called which looks it up in your $PATH. Defaults to "less" if you enter something bogus, by calling which recursively.
- Has a menued console interface - with color!
- The menu is hideable.
- Arbitrarily select snippets by designating an array index.
- Change the Sandbox directory interactively (using a hidden option: type "SANDBOX".)
- Display a snippet to the screen or
- execute the snippet from the menu.
#!/usr/bin/perl -w use strict; my $EDITOR = 'elvis'; my $index = @ARGV ? shift(@ARGV) :0; my $clear = 1; my $hide_help = 0; my $homedir = $ENV{HOME}; my $sandbox = $ENV{SANDBOX}; my $STUDYPATH = "$homedir/$sandbox"; $EDITOR = which($EDITOR); my $color = colorize('GREEN'); my $reset = colorize('reset'); while (1){ my @files = glob "$STUDYPATH/*"; system ('clear') unless $clear == 0; $clear++; $index =~ s/[A-Za-z]/0/g; $index = 0 if $index < (0-$#files)-1 || $index > $#files; printf ( "%50s <- (%5s)\n", $files[$index], $index); print " ",$color,"[C]",$reset,"hange editor. Currently $EDITOR ",$color,"[E]",$reset,"dit to open the snippet in an editor ",$color,"[G]",$reset,"oto to select file index: -", ($#files+1)," + to $#files ",$color,"[H]",$reset,"elp to (un)hide this menu ",$color,"[N]",$reset,"ext to skip this snippet ",$color,"[R]",$reset,"edo to revisit the previous snippet co",$color,"[L]",$reset,"or to change the menu item color ",$color,"[Q]",$reset,"UIT to exit ",$color,"[T]",$reset,"ext to change the menu text color e",$color,"[X]",$reset,"ecute Any other key +",$color," <ENTER> ",$reset,"to print out the snipp +et -> " unless $hide_help==1; my $input = <>; if ($input =~ /^N/i){ ++$index; next; }elsif($input =~ /^G/i){ print "enter a number from -",$#files+1," to $#files -> "; $index = <>; chomp $index; }elsif($input =~ /^H/i){ $hide_help = $hide_help?0:1; }elsif($input =~ /^C/i){ $EDITOR = <>; chomp $EDITOR; $EDITOR = which ($EDITOR); # redo STUDY; }elsif($input =~ /^R/i){ --$index; }elsif($input =~ /^L/i){ print "change menu item color to: "; my $newcolor = <>; chomp $newcolor; $color = colorize($newcolor); }elsif($input =~ /^T/i){ print "change menu text color to: "; my $textcolor = <>; chomp $textcolor; $reset = colorize($textcolor); }elsif($input =~ /^(SANDBOX)/i){ print "$homedir/"; my $sandbox = <>; chomp $sandbox; $STUDYPATH = 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>; } } sub which{ my $editor = shift; foreach (my @PATH = split(":",$ENV{PATH})) { if (-x "$_/$editor") { return "$_/$editor"; last; } } which ('less') if not -x "$editor"; } sub colorize{ my $a = shift; my $newcolor; eval "use Term::ANSIColor 'color'"; return if $@; for (my @valid_colors = split /,/ => qq( blue bold,yellow bold,reset, green bold,red bold,white, bold blue,bold yellow,bold white, bold green,bold red, red,green,yellow,blue)) { s/^\s+//s; if ( $a =~ /($_)/i) { return $newcolor = color($_); } } } sub change_snippet_dir{ my $sandbox = shift; my $STUDYPATH = "$homedir/$sandbox"; -d $STUDYPATH ? return $STUDYPATH: return "$ENV{HOME}/$ENV{SANDBOX}"; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re (tilly) 1: newer snippet browser
by tilly (Archbishop) on Jan 20, 2001 at 19:08 UTC | |
(tye)Re: newer snippet browser
by tye (Sage) on Jan 20, 2001 at 22:54 UTC | |
by mkmcconn (Chaplain) on Jan 21, 2001 at 05:51 UTC |