home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / perl / 7025 < prev    next >
Encoding:
Text File  |  1992-11-17  |  4.5 KB  |  135 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!gumby!wupost!darwin.sura.net!uvaarpa!mmdf
  3. From: Alan Stebbens <aks%anywhere@hub.ucsb.edu>
  4. Subject: Re: Compact Folders Display 
  5. Message-ID: <1992Nov17.223112.13967@uvaarpa.Virginia.EDU>
  6. Sender: mmdf@uvaarpa.Virginia.EDU (Mail System)
  7. Reply-To: aks%anywhere@hub.ucsb.edu
  8. Organization: The Internet
  9. Date: Tue, 17 Nov 1992 22:31:12 GMT
  10. Lines: 123
  11.  
  12. |     On some of my accounts I have more MH folders than fit on a screen
  13. |     at one time.  I like to be able to see them all at once so I've
  14. |     written a small program ...
  15.  
  16. I liked this idea, but I also like doing things in Perl, and wanted to
  17. view the output a little differently, as show below.  Also, the sorting
  18. is done vertically, so its easier to scan for a particular folder (I
  19. have over 300!).  They layout is different, and IMHO, easier to read,
  20. and will fit in the current window/terminal (uses 'stty size').
  21.  
  22. Here's a slice of sample output:
  23.  
  24.    10:ccse/rates              none:info/sun/spots          none:proj                   
  25.   166:ccse/staff                14:info/tcpip               127:proj/ECI               
  26.   103:ccse/support               3:info/techmail             49:proj/ECI/admin         
  27.    41:ccse/sw                 none:mail                      17:proj/ECI/arch          
  28.   276:ccse/systems              36:mail/admin                17:proj/ECI/hw            
  29.  
  30. mhfolders
  31. ============================= cut here ===================================
  32. #!/bin/perl
  33. # $Date: 1992/11/17 21:59:58 $ $Revision: 1.2 $
  34. # Alan K. Stebbens <aks@hub.ucsb.edu>, CCSE, UCSB
  35. #
  36. ($prog = $0) =~ s=^.*/==;    # remove head -> program name
  37.  
  38. sub usage { 
  39.     die <<EOF;
  40. usage: $prog [-vhen] [folderlist]
  41. Prints out list of MH folders in a compact format.  If a folder list
  42. is given, limit output to those folders beginning with these names.
  43. The options:
  44.   -h    print folders sorted horizontally, across columns, then down.
  45.   -v    print folders sorted vertically, down columns, then across.
  46.   -e    print only empty folders
  47.   -n    print only non-empty folders
  48. The only default option is "-v". 
  49. EOF
  50. }
  51.  
  52. $vert = $horiz = $empty = $nonempty = '';
  53.  
  54. while ($_ = shift) {
  55.     $vert++     if /^-.*v/;
  56.     $horiz++    if /^-.*h/;
  57.     $empty++    if /^-.*e/;
  58.     $nonempty++ if /^-.*n/;
  59.     &usage   if /^-/ && !/^-[enhv]/;
  60.     push(@select,$_) if !/^-/;
  61. }
  62. $vert || $horiz || ($vert = 1);
  63.  
  64. @folders = ();
  65. %folders = ();
  66. $maxlen = 0;
  67. $maxcount = 0;
  68. $maccur = 0;
  69. $folderRegexp = $#select >= $[ ? '^'.join('|^',@select) : '';
  70.  
  71. open(FOLDERS,"folders|") || die "Can't open pipe to folders: $!\n";
  72. $trash = <FOLDERS>;    # throw away first line (header)
  73.  
  74. while (<FOLDERS>) {    # scan the output
  75.     ($folder,$count,$beg,$end,$cur,$others) = ();
  76.     if (/(\S+) +has +(\d+|no) messages/) {
  77.     ($folder,$count) = ($1,$2);
  78.     next if $folderRegexp && $folder !~ m=$folderRegexp=;
  79.     # ($beg,$end) = ($1,$2) if /messages \( *(\d+)\- *(\d+)\)/;
  80.     $cur = /; cur= *(\d+)[;.]/ ? $1 : '';
  81.     !$cur || ($cur = '');
  82.     $count = 0 if $count eq 'no';
  83.     next if $nonempty && $count == 0;
  84.     next if $empty && $count != 0;
  85.     $folders{$folder} = $count.' '.cur;
  86.     #push(@folders,join(' ',$folder,$count,$cur));
  87.     $maxlen = length($folder) if $maxlen < length($folder);
  88.     $maxcount = $count if $maxcount < $count;
  89.     $maxcur = $cur if $maxcur < $cur;
  90.     }
  91. }    
  92. close FOLDERS;
  93.  
  94. @folders = sort keys %folders;
  95. $width = $1 if `stty size 0<&2` =~ /\d+ (\d+)/;
  96. $width || ($width = 80);
  97. $colwidth = $maxlen + length($maxcount) + length($maxcur) + 2;
  98. $cols = int($width / $colwidth);
  99. $cols > 0 || ($cols = 1);
  100. $rows = int($#folders / $cols + .99);
  101. $| = 1;
  102. #$fmt = sprintf("%%-%ds:%%-%ds ",$maxlen,(length($maxcount)+length($maxcur)+1));
  103. $fmt = sprintf("%%%ds:%%-%ds ",(length($maxcount)+length($maxcur)+1),$maxlen);
  104.  
  105. sub printFolder {
  106.     $folder = $folders[$i + $j];
  107.     ($count,$cur) = split(' ',$folders{$folder});
  108.     $info = $count == 0 ?  'none' :
  109.         $cur && cur != 0 ? sprintf("%d>%d",$count,$cur) :
  110.         sprintf("%d",$count);
  111.     printf $fmt,$info,$folder;
  112. }
  113.  
  114. if ($horiz) {
  115.     for ($i = 0; $i <= $#folders; $i += $cols) {
  116.     for ($j = 0; $j < $cols && $i + $j <= $#folders; $j++) {
  117.         &printFolder;
  118.     }
  119.     print "\n";
  120.     }
  121. } elsif ($vert) {
  122.     for ($i = 0; $i <= $rows; $i++) {
  123.     for ($j = 0; $i + $j <= $#folders; $j += $rows) {
  124.         &printFolder;
  125.     }
  126.     print "\n";
  127.     }
  128. }
  129. ============================= cut here ===================================
  130.  
  131. Alan Stebbens        <aks@hub.ucsb.edu>             (805) 893-3221
  132.      Center for Computational Sciences and Engineering (CCSE)
  133.           University of California, Santa Barbara (UCSB)
  134.            3111 Engineering I, Santa Barbara, CA 93106
  135.