From: Alan Stebbens <aks%anywhere@hub.ucsb.edu>
Subject: Re: Compact Folders Display 
Reply-To: aks%anywhere@hub.ucsb.edu
Organization: The Internet
Date: Tue, 17 Nov 1992 22:31:12 GMT

|     On some of my accounts I have more MH folders than fit on a screen
|     at one time.  I like to be able to see them all at once so I've
|     written a small program ...

I liked this idea, but I also like doing things in Perl, and wanted to
view the output a little differently, as show below.  Also, the sorting
is done vertically, so its easier to scan for a particular folder (I
have over 300!).  They layout is different, and IMHO, easier to read,
and will fit in the current window/terminal (uses 'stty size').

Here's a slice of sample output:

   10:ccse/rates              none:info/sun/spots          none:proj                   
  166:ccse/staff                14:info/tcpip               127:proj/ECI               
  103:ccse/support               3:info/techmail             49:proj/ECI/admin         
   41:ccse/sw                 none:mail                      17:proj/ECI/arch          
  276:ccse/systems              36:mail/admin                17:proj/ECI/hw            

mhfolders
============================= cut here ===================================
#!/bin/perl
# $Date: 1992/11/17 21:59:58 $ $Revision: 1.2 $
# Alan K. Stebbens <aks@hub.ucsb.edu>, CCSE, UCSB
#
($prog = $0) =~ s=^.*/==;	# remove head -> program name

sub usage { 
    die <<EOF;
usage: $prog [-vhen] [folderlist]
Prints out list of MH folders in a compact format.  If a folder list
is given, limit output to those folders beginning with these names.
The options:
  -h	print folders sorted horizontally, across columns, then down.
  -v	print folders sorted vertically, down columns, then across.
  -e	print only empty folders
  -n	print only non-empty folders
The only default option is "-v". 
EOF
}

$vert = $horiz = $empty = $nonempty = '';

while ($_ = shift) {
    $vert++     if /^-.*v/;
    $horiz++    if /^-.*h/;
    $empty++    if /^-.*e/;
    $nonempty++ if /^-.*n/;
    &usage   if /^-/ && !/^-[enhv]/;
    push(@select,$_) if !/^-/;
}
$vert || $horiz || ($vert = 1);

@folders = ();
%folders = ();
$maxlen = 0;
$maxcount = 0;
$maccur = 0;
$folderRegexp = $#select >= $[ ? '^'.join('|^',@select) : '';

open(FOLDERS,"folders|") || die "Can't open pipe to folders: $!\n";
$trash = <FOLDERS>;	# throw away first line (header)

while (<FOLDERS>) {	# scan the output
    ($folder,$count,$beg,$end,$cur,$others) = ();
    if (/(\S+) +has +(\d+|no) messages/) {
	($folder,$count) = ($1,$2);
	next if $folderRegexp && $folder !~ m=$folderRegexp=;
	# ($beg,$end) = ($1,$2) if /messages \( *(\d+)\- *(\d+)\)/;
	$cur = /; cur= *(\d+)[;.]/ ? $1 : '';
	!$cur || ($cur = '');
	$count = 0 if $count eq 'no';
	next if $nonempty && $count == 0;
	next if $empty && $count != 0;
	$folders{$folder} = $count.' '.cur;
	#push(@folders,join(' ',$folder,$count,$cur));
	$maxlen = length($folder) if $maxlen < length($folder);
	$maxcount = $count if $maxcount < $count;
	$maxcur = $cur if $maxcur < $cur;
    }
}	
close FOLDERS;

@folders = sort keys %folders;
$width = $1 if `stty size 0<&2` =~ /\d+ (\d+)/;
$width || ($width = 80);
$colwidth = $maxlen + length($maxcount) + length($maxcur) + 2;
$cols = int($width / $colwidth);
$cols > 0 || ($cols = 1);
$rows = int($#folders / $cols + .99);
$| = 1;
#$fmt = sprintf("%%-%ds:%%-%ds ",$maxlen,(length($maxcount)+length($maxcur)+1));
$fmt = sprintf("%%%ds:%%-%ds ",(length($maxcount)+length($maxcur)+1),$maxlen);

sub printFolder {
    $folder = $folders[$i + $j];
    ($count,$cur) = split(' ',$folders{$folder});
    $info = $count == 0 ?  'none' :
	    $cur && cur != 0 ? sprintf("%d>%d",$count,$cur) :
	    sprintf("%d",$count);
    printf $fmt,$info,$folder;
}

if ($horiz) {
    for ($i = 0; $i <= $#folders; $i += $cols) {
	for ($j = 0; $j < $cols && $i + $j <= $#folders; $j++) {
	    &printFolder;
	}
	print "\n";
    }
} elsif ($vert) {
    for ($i = 0; $i <= $rows; $i++) {
	for ($j = 0; $i + $j <= $#folders; $j += $rows) {
	    &printFolder;
	}
	print "\n";
    }
}
============================= cut here ===================================

Alan Stebbens        <aks@hub.ucsb.edu>             (805) 893-3221
     Center for Computational Sciences and Engineering (CCSE)
          University of California, Santa Barbara (UCSB)
           3111 Engineering I, Santa Barbara, CA 93106
Newsgroups: comp.lang.perl