home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / perl / 7561 / mrm-columnize.pl next >
Encoding:
Perl Script  |  1992-12-21  |  2.3 KB  |  96 lines

  1. #! /usr/local/bin/perl
  2.  
  3. {
  4.     package main;
  5.     require 'mrm-export.pl';
  6.  
  7.     &export ('columnize', $EXPORT_FUNCTION, 'columnize', 'line_length');
  8.  
  9.     $EXPORT_FUNCTION;    # silence perl -w
  10. }
  11.  
  12. package columnize;
  13.  
  14. # Function to return a string array, such that when each element is
  15. # printed, it is printed as a series of columns, each equally spaced.
  16. #
  17. # Arg 1:    The number of leading spaces to print on each line.
  18. # Arg 2:    The number of spaces to leave between each column.
  19. # Arg 3:    The line width to use or 0, to use the current terminal's line size.
  20. # Arg 4:    The minimun column width to use
  21. # Arg 5-    Items to columnize.
  22.  
  23. sub columnize {
  24.     local ($leading_spaces)    = shift (@_);
  25.     local ($num_spaces)    = shift (@_);
  26.     local ($user_line_size)    = shift (@_);
  27.     local ($max_length)    = shift (@_);
  28.     local (@ret)        = ();
  29.     local ($cur_col)    = 1;
  30.     local ($initial)    = ("\t" x ($leading_spaces / 8)) . (' ' x ($leading_spaces % 8));
  31.     local ($element);
  32.     local ($ncols);
  33.  
  34.     if ($#_ >= $[) {
  35.         foreach $element (@_) {
  36.             $max_length = length ($element)        if (length ($element) > $max_length);
  37.         }
  38.  
  39.         $ncols = &line_length ($user_line_size) - 1 - $leading_spaces;
  40.         $ncols += ($num_spaces - 1)            if ($num_spaces > 0);
  41.         $ncols /= ($max_length + $num_spaces);
  42.         $ncols = 1                    if ($ncols <= 0);
  43.  
  44.         foreach $element (@_[ $[ .. $#_ - 1 ]) {
  45.             if (++$cur_col >= $ncols) {
  46.                 $cur_col = 1;
  47.                 push (@ret, "$initial$element\n");
  48.                 $initial = ' ' x $leading_spaces;
  49.  
  50.             } else {
  51.                 push (@ret, ($initial . $element . (' ' x ($max_length - length ($element)))));
  52.                 $initial = ' ' x $num_spaces;
  53.             }
  54.         }
  55.  
  56.         push (@ret, "$initial$_[$#_]\n");
  57.     }
  58.  
  59.     return @ret;
  60. }
  61.  
  62.  
  63. # Function to get the line length.
  64.  
  65. sub line_length {
  66.     local ($user_line_size)    = shift (@_);
  67.  
  68.     return $user_line_size                if (defined ($user_line_size) && $user_line_size > 0);
  69.  
  70.     if (! defined ($line_length) || ! $line_length) {
  71.  
  72.         if (defined ($ENV{'COLUMNS'}) && $ENV{'COLUMNS'} > 0) {
  73.             $line_length = $ENV{'COLUMNS'} + 0;
  74.  
  75.         } else {
  76.             local ($winsize)    = "\0\0\0\0\0\0\0\0";
  77.             local ($TIOCGWINSZ)    = 0x40087468;        # on OSF/1 at least
  78.             local (@winsize2);
  79.  
  80.             $line_length = 79;
  81.             if (-t STDIN && ioctl (STDIN, $TIOCGWINSZ, $winsize)) {
  82.                 @winsize2 = unpack ('SSSS', $winsize);
  83.  
  84.                 $line_length = $winsize2[1]    if ($winsize2[1] > 0);
  85.             }
  86.         }
  87.     }
  88.  
  89.     return $line_length;
  90. }
  91.  
  92.  
  93. # Return 1, so require succeeds.
  94.  
  95. 1;
  96.