Better NetInfo behaviour.
[p5sagit/p5-mst-13.2.git] / lib / Text / Wrap.pm
1 package Text::Wrap;
2
3 use vars qw(@ISA @EXPORT @EXPORT_OK $VERSION $columns $debug $min_wrap_width);
4 use strict;
5 use Exporter;
6
7 $VERSION = "97.03";
8 @ISA = qw(Exporter);
9 @EXPORT = qw(wrap);
10 @EXPORT_OK = qw($columns $tabstop $min_wrap_width fill);
11
12 use Text::Tabs qw(expand unexpand $tabstop);
13
14
15 BEGIN   {
16     $columns = 76;  # <= screen width
17     $debug = 0;
18     # minimum wrap width (leaders will be shortened to accomodate this)
19     $min_wrap_width = int($columns/5);
20 }
21
22 sub wrap
23 {
24     my ($ip, $xp, @t) = @_;
25
26     my @rv;
27     my $t = expand(join(" ",@t));
28
29     my $xll = $columns - length(expand($xp)) - 1;
30     while ($xll < $min_wrap_width) {
31         chop $xp;
32         $xll = $columns - length(expand($xp)) - 1;
33     }
34
35     my $ll = $columns - length(expand($ip)) - 1;
36     while ($ll < $min_wrap_width) {
37         chop $ip;
38         $ll = $columns - length(expand($ip)) - 1;
39     }
40     my $lead = $ip;
41     my $nl = "";
42
43     $t =~ s/^\s+//;
44     while(length($t) > $ll) {
45         # remove up to a line length of things that
46         # aren't new lines and tabs.
47         if ($t =~ s/^([^\n]{0,$ll})(\s|\Z(?!\n))//) {
48             my ($l,$r) = ($1,$2);
49             $l =~ s/\s+$//;
50             print "WRAP  $lead$l..($r)\n" if $debug;
51             push @rv, unexpand($lead . $l), "\n";
52                 
53         } elsif ($t =~ s/^([^\n]{$ll})//) {
54             print "SPLIT $lead$1..\n" if $debug;
55             push @rv, unexpand($lead . $1),"\n";
56         }
57         # reset the leader
58         $lead = $xp;
59         $ll = $xll;
60         $t =~ s/^\s+//;
61     } 
62     print "TAIL  $lead$t\n" if $debug;
63     push @rv, $lead.$t if $t ne "";
64     return join '', @rv;
65 }
66
67
68 sub fill 
69 {
70         my ($ip, $xp, @raw) = @_;
71         my @para;
72         my $pp;
73
74         for $pp (split(/\n\s+/, join("\n",@raw))) {
75                 $pp =~ s/\s+/ /g;
76                 my $x = wrap($ip, $xp, $pp);
77                 push(@para, $x);
78         }
79
80         # if paragraph_indent is the same as line_indent, 
81         # separate paragraphs with blank lines
82
83         return join ($ip eq $xp ? "\n\n" : "\n", @para);
84 }
85
86 1;
87 __END__
88
89 =head1 NAME
90
91 Text::Wrap - line wrapping to form simple paragraphs
92
93 =head1 SYNOPSIS 
94
95         use Text::Wrap
96
97         print wrap($initial_tab, $subsequent_tab, @text);
98
99         use Text::Wrap qw(wrap $columns $tabstop fill);
100
101         $columns = 132;
102         $tabstop = 4;
103
104         print fill($initial_tab, $subsequent_tab, @text);
105         print fill("", "", `cat book`);
106
107 =head1 DESCRIPTION
108
109 Text::Wrap::wrap() is a very simple paragraph formatter.  It formats a
110 single paragraph at a time by breaking lines at word boundries.
111 Indentation is controlled for the first line ($initial_tab) and
112 all subsquent lines ($subsequent_tab) independently.  $Text::Wrap::columns
113 should be set to the full width of your output device (default is 76).
114 $Text::Wrap::min_wrap_width controls the minimum number of columns that
115 are reserved for the wrapped text (default is 15).  Indentation will
116 be reduced to accomodate this value.
117
118 Text::Wrap::fill() is a simple multi-paragraph formatter.  It formats
119 each paragraph separately and then joins them together when it's done.  It
120 will destory any whitespace in the original text.  It breaks text into
121 paragraphs by looking for whitespace after a newline.  In other respects
122 it acts like wrap().
123
124 =head1 EXAMPLE
125
126         print wrap("\t","","This is a bit of text that forms 
127                 a normal book-style paragraph");
128
129 =head1 BUGS
130
131 It's not clear what the correct behavior should be when Wrap() is
132 presented with a word that is longer than a line.  The previous 
133 behavior was to die.  Now the word is now split at line-length.
134
135 =head1 AUTHOR
136
137 David Muir Sharnoff <muir@idiom.com> with help from Tim Pierce and
138 others. Updated by Jacqui Caren.
139
140 =cut