5.002 beta 1
[p5sagit/p5-mst-13.2.git] / lib / Text / Wrap.pm
1
2 package Text::Wrap;
3
4 #
5 # This is a very simple paragraph formatter.  It formats one 
6 # paragraph at a time by wrapping and indenting text.
7 #
8 # Usage:
9 #
10 #       use Text::Wrap;
11 #
12 #       print wrap($initial_tab,$subsequent_tab,@text);
13 #
14 # You can also set the number of columns to wrap before:
15 #
16 #       $Text::Wrap::columns = 135; # <= width of screen
17 #
18 #       use Text::Wrap qw(wrap $columns); 
19 #       $columns = 70;
20 #       
21 #
22 # The first line will be printed with $initial_tab prepended.  All
23 # following lines will have $subsequent_tab prepended.
24 #
25 # Example:
26 #
27 #       print wrap("\t","","This is a bit of text that ...");
28 #
29 # David Muir Sharnoff <muir@idiom.com>
30 # Version: 9/21/95
31 #
32
33 require Exporter;
34
35 @ISA = (Exporter);
36 @EXPORT = qw(wrap);
37 @EXPORT_OK = qw($columns);
38
39 BEGIN   {
40         $Text::Wrap::columns = 76;  # <= screen width
41 }
42
43 use Text::Tabs;
44 use strict;
45
46 sub wrap
47 {
48         my ($ip, $xp, @t) = @_;
49
50         my $r;
51         my $t = expand(join(" ",@t));
52         my $lead = $ip;
53         my $ll = $Text::Wrap::columns - length(expand($lead)) - 1;
54         if ($t =~ s/^([^\n]{0,$ll})\s//) {
55                 $r .= unexpand($lead . $1 . "\n");
56                 $lead = $xp;
57                 my $ll = $Text::Wrap::columns - length(expand($lead)) - 1;
58                 while ($t =~ s/^([^\n]{0,$ll})\s//) {
59                         $r .= unexpand($lead . $1 . "\n");
60                 }
61         } 
62         die "couldn't wrap '$t'" 
63                 if length($t) > $ll;
64         $r .= $t;
65         return $r;
66 }
67
68 1;