# Returns an indented string containing a list. Syntax is: # _sprintlist($listref[, $firstline[, $separator[, $indent # [, $break[, $linelen[, $lineprefix]]]]]) # All lines are indented by $indent spaces (default 0). # If $firstline is given, that string is inserted in the # indentation of the first line (it is truncated to $indent spaces # unless $break is true, in which case the list is pushed to the next # line). Normally the list elements are separated by commas and spaces # ", ". If $separator is given, that string is used as a separator. # Lines are wrapped to $linelen characters, unless it is specified as # some other value. A value of 0 for $linelen makes it not do line wrapping. # If $lineprefix is given, it is printed at the beginning of each line. sub _sprintlist { my $listref=shift or return; my @list=@$listref; my $fline=shift; my $separator=shift || ", "; my $indent=shift; $indent=0 unless defined($indent); my $space=" " x $indent; $fline||=$space; my $break=shift || 0; my $linelen=shift; $linelen=80 unless defined($linelen); my $lp=shift||""; $linelen-=length($lp); if (!$break || length($fline)<=$indent) { $fline=substr("$fline$space", 0, length($space)); } else { # length($fline)>$indent $fline="$fline\n$space"; } my $str=""; my $line=""; foreach (@list) { $line.=$separator if $line; if ($linelen && length($line)+length($_)+length($space)>=$linelen) { $line=~s/\s*$//; $str.="$space$line\n"; $line=""; } $line.="$_"; } $str.="$space$line"; $str=~s/^$space/$fline/; $str=~s/^/$lp/mg if $lp; return $str; } # Gets a string, and returns it nicely formatted and word-wrapped. It # uses _sprintlist as a backend. Its syntax is the same as # _sprintlist, except that is gets a string instead of a list ref, and # that $separator is not used because we automatically break on white # space. # Syntax: _sprintstr($string[, $firstline[, $indent[, $break[, $linelen # [, $lineprefix]]]]]); # See _sprintlist for the meaning of each parameter. sub _sprintstr { my ($str, $fl, $ind, $br, $len, $lp)=@_; $ind||=0; # Split string into \n-separated parts. my @strs=($str=~/([^\n]+\n?|[^\n]*\n)/g); # Now process each line separately my $s; my $result; foreach $s (@strs) { # Store end of lines at the end of the string my $eols=($s=~/(\n*)$/)[0]; # Split in words. my @words=(split(/\s+/, $s)); $result.=_sprintlist(\@words,$fl," ",$ind,$br,$len, $lp).$eols; # The $firstline is only needed at the beginning of the first string. $fl=undef; } return $result; }