Go Back   The macosxhints Forums > OS X Help Requests > UNIX - General



Reply
 
Thread Tools Rating: Thread Rating: 19 votes, 5.00 average. Display Modes
Old 08-11-2002, 07:22 PM   #21
vickishome
MVP
 
Join Date: Jul 2002
Location: Texas
Posts: 1,075
Re: one more tweak...maybe?

Quote:
Originally posted by rusto
I've gotten used to having my directories in color...

If we get to vote, I'll second the request for color.
__________________
Vicki
• 15" MacBook Pro 2.66GHz i7, Mavericks 10.9.1, 8GB RAM
• iPad 4G WiFi 64GB
• iPhone 5 64GB
• 15" MacBook Pro 2.4GHz, Tiger 10.4, 4GB RAM
• G5 Dual 2GHz, Panther 10.3, 1.5GB RAM
• G4 Dual 1GHz, Tiger 10.4, 1.5GB RAM

Using Macs since 1986!
vickishome is offline   Reply With Quote
Old 08-11-2002, 09:12 PM   #22
rusto
MVP
 
Join Date: Jan 2002
Location: Boston, MA
Posts: 1,489
btw, I followed this method I used to get color in my ls command:

http://www.resexcellence.com/terminal/05-09-01.shtml
__________________
:: 3.4GHz Core i7 iMac 4GB RAM :: Black MacBook SR :: 10.7.2 :: iPhone 4 / iOS 5 ::
rusto is offline   Reply With Quote
Old 08-12-2002, 01:22 AM   #23
hayne
Site Admin
 
Join Date: Jan 2002
Location: Montreal
Posts: 32,473
[pedantic] should close LS

Okay, it doesn't matter in this script since it will exit soon and so clean itself up anyway, but I feel that if you 'open' something, you ought to 'close' it when you are done. So I would add a statement:
close LS;
after the line: my @what = <LS>;

Sorry to be so pedantic, but I worry that someone might go writing a Perl script where they do an 'open' inside a loop and don't realize that they need to 'close'.
hayne is offline   Reply With Quote
Old 08-12-2002, 01:59 AM   #24
pmccann
Major Leaguer
 
Join Date: Jan 2002
Location: Adelaide, South Australia
Posts: 470
***Post edited to reflect the fact that the required perl module is part of the default install. Apologies to anyone misled by my earlier claim that it had to be downloaded and installed separately.***

[[Re the "close" statement: I'm really at a loss to see what it adds, and how you'd get into trouble here by not adding explicit closes. Well, such behaviour would definitely cause some problems with more exotic pipes and networking scripts, but here it doesn't really matter. If you happen to reopen on the same filehandle the old handle is closed first, so you're covered there. Maybe I lack imagination... Maybe I'm just lazy... Maybe I expect things to "just work"... Ah well... It certainly never *harms* to have it there, so if symmetry's your friend then you'll probably want a close on the back of every open.]]

The colour request. Hmm. Not quite as straightforward as I might have hoped, but not too bad. My final substantial posts on this thread attempt to throw a little colour into the mix; with the emphasis on "little", in that the colour specifications are very limited, but might just be enough to satisfy those of you hankering for blue or green or red or magenta or yellow in various thicknesses and styles! See the examples in the script below, and the full list of colours and styles by reading:

% perldoc term::ansicolor

Those of you who use transparency in the terminal will see exactly how the
"on_white" colouring works. (Note that I've use "green on_white" for directory names, but "transparent" blue for file names, just to show the difference.) Those parts of the terminal window not drawn to will stay transparent, the pieces under the "on_white" output become solid. You can of course simply remove the "underlay", and your terminal will remain just as transparent as it ever was.

The other slightly "interesting" aspect is that this sort of thing doesn't play very nicely withe paging programs like "less", which don't acknowledge the presence of the escape sequences correctly. Term::ANSIColor talks about generating the sequences at the start and end of each line: that doesn't work for the terminal in OS X (or for an xterm under XDarwin. Maybe the problem lies with "less"? Anyway, I've given up trying to chase what's happening, so here it is (in the next post), with that particular wart exposed. (Just use "page up" to see long listings!) For what it's worth, the standard "coloriser" that I use --obtained by simply adding "set color" to my .cshrc and then using ls-F (no space before the -F) to list in color-- also fails under the pipe to less, but in a slightly more elegant fashion.

Cheers,
Paul

Oh yeah, thanks to Erik for his welcome additions

Last edited by pmccann; 08-12-2002 at 03:40 AM.
pmccann is offline   Reply With Quote
Old 08-12-2002, 02:01 AM   #25
pmccann
Major Leaguer
 
Join Date: Jan 2002
Location: Adelaide, South Australia
Posts: 470
The script with a bit of colour added

#!/usr/bin/perl -w
use strict;
use Getopt::Std;
$Term::ANSIColor::EACHLINE="\n";
use Term::ANSIColor;
use vars qw($opt_a $opt_f $opt_h $opt_d $opt_s $opt_r $opt_l);
my $screenwidth = 80; # adjust if necessary
my $dir_color = "green on_white";
my $dir_header = "bold white on_green";
my $file_color = "blue ";
my $file_header = "bold white on_blue";

my $dirs_topstring = "+----------------- Directories "; #adjust as desired!
my $files_topstring = "+--------------------- Files ";
getopts('adfshrl'); # These are the valid options: see help message
if (!($opt_f || $opt_d || $opt_h)){ $opt_f=1; $opt_d=1 };
# show both if neither option present (and it's not a help request)
warn <<HELP and exit if $opt_h or $ARGV[1];
usage: lc [-adfsrhl] [directory]

A bare "lc" will separate files and directories, and sort them in a
case insensitive manner down the screen (ie in columns). To sort case
sensitively, or to display results in rows, or to show just files or
just directories use the flags as outlined below.

Flags may be grouped together (so "lc -ad" is the same as "lc -a -d")
--------------------------------------------------------------------------------
-a : Include directory entries whose names begin with a dot (.)
-d : Show directories
-f : Show files (note that a bare "lc" will show both)
-s : Sort case sensitively (eg "Lemon" before "apple")
-r : List the output in rows instead of columns
-h : Display this message
-l : Display link (@) and exec (*) status on files displayed

HELP

$ARGV[0] or $ARGV[0] = ".";
open LS,"ls -aF -- $ARGV[0]|"; # pipe the output of ls to this handle
my @what = <LS>; # catch the output from the ls -F command
$opt_a or @what = grep(!/^\./,@what); # filter out dot files?
my @dirs = grep /\/$/,@what; # directories end with "/"
my @files = grep /[^\/]\n$/,@what; # the rest (incl. links)

if (@dirs && $opt_d){ # process dirs, if they exist and are requested
my @sdirs = map {substr $_,0,-2} @dirs; # kill / and newline at once
bannerprint($dirs_topstring);
displaylist(\@sdirs,$dir_color);
}

if (@files && $opt_f){ # show files, if they exist and are requested
chomp @files;
$opt_l or s/\*|\@$// foreach (@files); # remove exec/link status?
bannerprint($files_topstring);
displaylist(\@files,$file_color);
}

### Subroutines used above ###
sub bannerprint{
my $starter = shift;
my $topstring = $starter.("-"x($screenwidth-length($starter)-1))."+\n";
print colored $topstring,$dir_header if ($topstring=~/Direct/);
print colored $topstring,$file_header if ($topstring=~/File/);
print color 'reset'
}

sub displaylist{
my $array_ref = shift;
my $color = shift;
my $disp_len = getlen($array_ref)+1;
my $num_disp_perline = int(80/${disp_len});
my @list = @{$array_ref};
@list = sort {lc($a) cmp lc($b)} @list unless $opt_s;
if ($opt_r){ # for some perverted reason they want rows... OK!!
my $j=0;
my $out_stuff;
foreach (@list){
$out_stuff.=sprintf "%-${disp_len}s", $_; $j++;
$out_stuff.= "\n" if $j % ${num_disp_perline} == 0;
}
$out_stuff.= "\n";
print colored $out_stuff, $color;
print color 'reset';
}else{ # here's one revolting way to print out columns
my $length = @list;
my $numrows = int($length/$num_disp_perline);
$numrows++ if ($length/$num_disp_perline - $numrows);
# extra row if doesn't divide evenly (ie there's a remainder)
my $out_stuff;
foreach my $i (1..$numrows){
my $j=0;
my @row = grep {$j++;($i-$j)%$numrows == 0} @list;
foreach (@row){
$out_stuff.=sprintf "%-${disp_len}s",$_;
# print "Outstuff is now : ($j th row)\n$out_stuff\n";
}
$out_stuff.="\n";
}
print colored $out_stuff, $color;
print color 'reset';
}
}

sub getlen{
my $arr_ref = shift;
my $blen = 0; my $len = 0;
foreach (@{$arr_ref}){
$len = length($_);
$blen = $len if $len>$blen;
}
$blen++; # return one greater than the longest word
}

Last edited by pmccann; 08-12-2002 at 09:09 AM.
pmccann is offline   Reply With Quote
Old 08-12-2002, 07:17 AM   #26
eriklager
Triple-A Player
 
Join Date: Jan 2002
Location: Sweden
Posts: 122
Paul,

I guess you missed my post about the bug i created .

If you remove the : in 'adfshrl:' on line 15 (in the script with color, it was line 8 in the original script) you will get the -l flag back working again.
I put the colon there when I tested something and forgot to remove it.

Erik
eriklager is offline   Reply With Quote
Old 08-12-2002, 08:18 AM   #27
sao
Moderator
 
Join Date: Jan 2002
Location: Singapore
Posts: 4,237
pmccann,

I get the following:

'Can't find string terminator "HELP" anywhere before EOF at /Users/pm/bin/lc1 line 18'.

Edited

Got it to work, forgot about deleting the space mentioned by MervTormel.

Works great! Thanks.


Cheers...

Last edited by sao; 08-12-2002 at 10:33 AM.
sao is offline   Reply With Quote
Old 08-12-2002, 09:16 AM   #28
pmccann
Major Leaguer
 
Join Date: Jan 2002
Location: Adelaide, South Australia
Posts: 470
Hi Erik,

yep, don't know how that crept back into the script. I *did* read your earlier message, but must have started working from an earlier version of the script. I've edited the post above in order to insert the colon where all good colons belong... out of sight.

Here's to crap colour then. (And to a frozen feature set :-) The thing's too long already!) Anyway, hope it gives a couple of people ideas for other scripts.

Cheers,
Paul
pmccann is offline   Reply With Quote
Old 08-12-2002, 10:15 AM   #29
rusto
MVP
 
Join Date: Jan 2002
Location: Boston, MA
Posts: 1,489
werx great and is so purdy!

__________________
:: 3.4GHz Core i7 iMac 4GB RAM :: Black MacBook SR :: 10.7.2 :: iPhone 4 / iOS 5 ::
rusto is offline   Reply With Quote
Old 08-12-2002, 12:20 PM   #30
vickishome
MVP
 
Join Date: Jul 2002
Location: Texas
Posts: 1,075
Very nice! I still had to remove the space after "HELP" on line 36, but no problem there. Thanks for adding the color. I like it.
__________________
Vicki
• 15" MacBook Pro 2.66GHz i7, Mavericks 10.9.1, 8GB RAM
• iPad 4G WiFi 64GB
• iPhone 5 64GB
• 15" MacBook Pro 2.4GHz, Tiger 10.4, 4GB RAM
• G5 Dual 2GHz, Panther 10.3, 1.5GB RAM
• G4 Dual 1GHz, Tiger 10.4, 1.5GB RAM

Using Macs since 1986!
vickishome is offline   Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 05:36 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.