#!/usr/bin/perl -wn BEGIN { $LINE_WIDTH = 75; $METHOD = \&middle_out; $CONDENSE = 1; use Getopt::Long; Getopt::Long::Configure('bundling'); GetOptions( 'c|condense' => sub { $CONDENSE = 1 }, 'C|nocondense' => sub { $CONDENSE = 1 }, 'm|middle' => sub { $METHOD = \&middle_out }, 'l|left' => sub { $METHOD = \&left_to_right }, 'r|right' => sub { $METHOD = \&right_to_left }, 'w|width=i' => sub { $LINE_WIDTH = $_[1]; die "$0: invalid WIDTH (must be between 1 and 255)\n" if($LINE_WIDTH < 1 or $LINE_WIDTH > 255); }, 'help' => sub { my $self = $0; $self =~ s/^.*\///; print << "USAGE"; Usage: $self [OPTION]... [FILE]... Fully justifies a stream of text using one of three different methods. -c, --condense condense multiple spaces before justifying (default) -C, --nocondense -m, --middle use the middle-out method (default) -l, --left use the left-to-right method -r, --right use the right-to-left method -w, --width=WIDTH set the line-width to WIDTH (default is 75) --help display this help and exit The different methods describe where it starts expanding spaces to get lines to be the appropriate width. For example, middle-out starts in the middle of the line, expanding spaces in both directions. The middle-out method usually looks best, although right-to-left is some- times better. USAGE exit(0); }, ); } if($CONDENSE and !/^ /) { s/ +$//g; s/(?(\@space_table, length($last_line)); $cntr = 0; $last_line =~ s/(?[$pr--]++; last if(++$str_len > $LINE_WIDTH); } # forward unless($pf == $tbl_len) { $tbl_ref->[$pf++]++; last if(++$str_len > $LINE_WIDTH); } } } sub left_to_right { my $tbl_ref = shift; my $tbl_len = @$tbl_ref; my $str_len = shift; return unless($tbl_len); my $pf = 0; while($str_len <= $LINE_WIDTH) { $pf = 0 if($pf == $tbl_len); $tbl_ref->[$pf++]++; ++$str_len; } } sub right_to_left { my $tbl_ref = shift; my $tbl_len = @$tbl_ref; my $str_len = shift; return unless($tbl_len); my $pr = $tbl_len-1; while($str_len <= $LINE_WIDTH) { $pr = $tbl_len-1 if($pr == -1); $tbl_ref->[$pr--]++; ++$str_len; } }