home *** CD-ROM | disk | FTP | other *** search
- #! /usr/local/bin/perl
-
- {
- package main;
- require 'mrm-export.pl';
-
- &export ('columnize', $EXPORT_FUNCTION, 'columnize', 'line_length');
-
- $EXPORT_FUNCTION; # silence perl -w
- }
-
- package columnize;
-
- # Function to return a string array, such that when each element is
- # printed, it is printed as a series of columns, each equally spaced.
- #
- # Arg 1: The number of leading spaces to print on each line.
- # Arg 2: The number of spaces to leave between each column.
- # Arg 3: The line width to use or 0, to use the current terminal's line size.
- # Arg 4: The minimun column width to use
- # Arg 5- Items to columnize.
-
- sub columnize {
- local ($leading_spaces) = shift (@_);
- local ($num_spaces) = shift (@_);
- local ($user_line_size) = shift (@_);
- local ($max_length) = shift (@_);
- local (@ret) = ();
- local ($cur_col) = 1;
- local ($initial) = ("\t" x ($leading_spaces / 8)) . (' ' x ($leading_spaces % 8));
- local ($element);
- local ($ncols);
-
- if ($#_ >= $[) {
- foreach $element (@_) {
- $max_length = length ($element) if (length ($element) > $max_length);
- }
-
- $ncols = &line_length ($user_line_size) - 1 - $leading_spaces;
- $ncols += ($num_spaces - 1) if ($num_spaces > 0);
- $ncols /= ($max_length + $num_spaces);
- $ncols = 1 if ($ncols <= 0);
-
- foreach $element (@_[ $[ .. $#_ - 1 ]) {
- if (++$cur_col >= $ncols) {
- $cur_col = 1;
- push (@ret, "$initial$element\n");
- $initial = ' ' x $leading_spaces;
-
- } else {
- push (@ret, ($initial . $element . (' ' x ($max_length - length ($element)))));
- $initial = ' ' x $num_spaces;
- }
- }
-
- push (@ret, "$initial$_[$#_]\n");
- }
-
- return @ret;
- }
-
-
- # Function to get the line length.
-
- sub line_length {
- local ($user_line_size) = shift (@_);
-
- return $user_line_size if (defined ($user_line_size) && $user_line_size > 0);
-
- if (! defined ($line_length) || ! $line_length) {
-
- if (defined ($ENV{'COLUMNS'}) && $ENV{'COLUMNS'} > 0) {
- $line_length = $ENV{'COLUMNS'} + 0;
-
- } else {
- local ($winsize) = "\0\0\0\0\0\0\0\0";
- local ($TIOCGWINSZ) = 0x40087468; # on OSF/1 at least
- local (@winsize2);
-
- $line_length = 79;
- if (-t STDIN && ioctl (STDIN, $TIOCGWINSZ, $winsize)) {
- @winsize2 = unpack ('SSSS', $winsize);
-
- $line_length = $winsize2[1] if ($winsize2[1] > 0);
- }
- }
- }
-
- return $line_length;
- }
-
-
- # Return 1, so require succeeds.
-
- 1;
-