summaryrefslogtreecommitdiff
path: root/mmuegel/libs/strings1.pl
blob: a49335a2aaeaad4e01834f1cfdfe9192cf9c967b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
;# NAME
;#    strings1.pl - FUN with strings #1
;#
;# NOTES
;#    I wrote Format_Text_Block when I just started programming Perl so
;#    it is probably not very Perlish code. Center is more like it :-).
;#
;# AUTHOR
;#    Michael S. Muegel (mmuegel@mot.com)
;#
;# RCS INFORMATION
;#    mmuegel
;#    /usr/local/ustart/src/mail-tools/dist/foo/libs/strings1.pl,v 1.1 1993/07/28 08:07:19 mmuegel Exp

package strings1;

;###############################################################################;# Center
;#
;# Center $Text assuming the output should be $Columns wide. $Text can span
;# multiple lines, of course :-). Lines within $Text that contain only 
;# whitespace are not centered and are instead collapsed. This may save time 
;# when printing them later.
;#
;# Arguments:
;#    $Text, $Columns
;#
;# Returns:
;#    $Centered_Text
;###############################################################################
sub main'Center
{
   local ($_, $Columns) = @_;
   local ($*) = 1;

   s@^(.*)$@" " x (($Columns - length ($1)) / 2) . $1@eg;
   s/^[\t ]*$//g;
   return ($_);
};

;###############################################################################
;# Format_Text_Block
;#
;# Formats a text string to be printed to the display or other similar device.
;# Text in $String will be fomratted such that the following hold:
;#
;#    + $String contains the (possibly) multi-line text to print. It is
;#	automatically word-wrapped to fit in $Columns. 
;#
;#    + \n'd are maintained and are not folded.
;#
;#    + $Offset	is pre-pended before each separate line of text. 
;#
;#    + If $Offset_Once	is $TRUE $Offset will only appear on the first line.
;#      All other lines will be indented to match the amount of whitespace of
;#      $Offset.
;#
;#    + If $Bullet_Indent is $TRUE $Offset will only be applied to the begining
;#      of lines as they occured in the original $String. Lines that are created
;#      by this routine will always be indented by blank spaces.
;#
;#    + If $Columns is 0 no word-wrap is done. This might be useful to still
;#      to offset each line in a buffer.
;#
;#    + If $Split_Expr is supplied the string is split on it. If not supplied
;#      the string is split on " \t\/\-\,\." by default.
;#
;#    + If $Offset_Blank is $TRUE then empty lines will have $Offset pre-pended
;#      to them. Otherwise, they will still empty.
;#
;# This is a realy workhorse routine that I use in many places because of its
;# veratility.
;#
;# Arguments:
;#    $String, $Offset, $Offset_Once, $Bullet_Indent, $Columns, $Split_Expr,
;#    $Offset_Blank
;#
;# Returns:
;#    $Buffer
;###############################################################################
sub main'Format_Text_Block
{
   local ($String, $Real_Offset, $Offset_Once, $Bullet_Indent, $Columns, 
      $Split_Expr, $Offset_Blank) = @_;

   local ($New_Line, $Line, $Chars_Per_Line, $Space_Offset, $Buffer,
      $Next_New_Line, $Num_Lines, $Num_Offsets, $Offset);
   local ($*) = 0;
   local ($BLANK_TAG) = "__FORMAT_BLANK__";
   local ($Blank_Offset) = $Real_Offset if ($Offset_Blank);

   # What should we split on?
   $Split_Expr = " \\t\\/\\-\\,\\." if (! $Split_Expr);

   # Pre-process the string - convert blank lines to __FORMAT_BLANK__ sequence
   $String =~ s/\n\n/\n$BLANK_TAG\n/g;
   $String =~ s/^\n/$BLANK_TAG\n/g;
   $String =~ s/\n$/\n$BLANK_TAG/g;

   # If bad $Columns/$Offset combo or no $Columns make a VERRRYYY wide $Column
   $Offset = $Real_Offset;
   $Chars_Per_Line = 16000 if (($Chars_Per_Line = $Columns - length ($Offset)) <= 0);
   $Space_Offset = " " x length ($Offset);

   # Get a buffer
   foreach $Line (split ("\n", $String))
   {
      $Offset = $Real_Offset if ($Bullet_Indent);

      # Find where to split the line
      if ($Line ne $BLANK_TAG)
      { 
         $New_Line = "";
         while ($Line =~ /^([$Split_Expr]*)([^$Split_Expr]+)/)
         {
            if (length ("$New_Line$&") >= $Chars_Per_Line)
            {
               $Next_New_Line = $+;
               $New_Line = "$Offset$New_Line$1";
               $Buffer .= "\n" if ($Num_Lines++);
               $Buffer .= $New_Line;
               $Offset = $Space_Offset if (($Offset) && ($Offset_Once));
               $New_Line = $Next_New_Line;
               ++$Num_Lines;
            }
            else
            {
               $New_Line .= $&;
            };
            $Line = $';
         };

         $Buffer .= "\n" if ($Num_Lines++);
         $Buffer .= "$Offset$New_Line$Line";
         $Offset = $Space_Offset if (($Offset) && ($Offset_Once));
      }

      else
      {
         $Buffer .= "\n$Blank_Offset";
      };
   };

   return ($Buffer);

};

1;