| 1 | package Acme::Cow::TextBalloon; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 4 | my $rcs_id = q$Id: TextBalloon.pm,v 1.1 2001/09/10 23:31:09 tony Exp $; | |||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | =pod | ||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | =head1 NAME | ||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | Acme::Cow::TextBalloon - A balloon of text | ||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | use Acme::Cow::TextBalloon; | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | $x = new Acme::Cow::TextBalloon; | ||||||||||||||||||||
| 18 | $x->add("bunch of text"); | ||||||||||||||||||||
| 19 | $x->wrapcolumn(29); | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | $y = new Acme::Cow::TextBalloon; | ||||||||||||||||||||
| 22 | $y->adjust(0); | ||||||||||||||||||||
| 23 | $y->add("more text"); | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | =head1 DESCRIPTION | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | C<Acme::Cow::TextBalloon> Creates and manipulates balloons of text, | ||||||||||||||||||||
| 28 | optionally printing them. One may notice that the methods in this | ||||||||||||||||||||
| 29 | module are named very similarly to those in C<Acme::Cow>; that's | ||||||||||||||||||||
| 30 | because most of them have to do with the balloon rather than the | ||||||||||||||||||||
| 31 | cow. | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | =cut | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 4 | ▶ | use Text::Tabs; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 36 | 4 | ▶ | use Text::Wrap; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | sub new | ||||||||||||||||||||
| 40 | { | ||||||||||||||||||||
| 41 | 11 | ▶ | my $proto = shift; | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.4
Pod
undocumented
| |||||||||||||||||||||
| 42 | 11 | ▶ | my $class = ref $proto || $proto; | ||||||||||||||||||
|
Condition33%1/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||
| 43 | 11 | my $self = { | |||||||||||||||||||
| 44 | fill => 1, | ||||||||||||||||||||
| 45 | mode => 'say', | ||||||||||||||||||||
| 46 | over => 0, | ||||||||||||||||||||
| 47 | text => [ ], | ||||||||||||||||||||
| 48 | wrap => 40, | ||||||||||||||||||||
| 49 | }; | ||||||||||||||||||||
| 50 | 11 | return bless $self, $class; | |||||||||||||||||||
| 51 | } | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | sub wrapcolumn | ||||||||||||||||||||
| 54 | { | ||||||||||||||||||||
| 55 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
wrapcolumn: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 56 | 0 | ▶ | if (@_) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 57 | 0 | $self->{'wrap'} = $_[0]; | |||||||||||||||||||
| 58 | } | ||||||||||||||||||||
| 59 | 0 | return $self->{'wrap'}; | |||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | sub mode | ||||||||||||||||||||
| 63 | { | ||||||||||||||||||||
| 64 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
mode: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 65 | 0 | return $self->{'mode'}; | |||||||||||||||||||
| 66 | } | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | sub think | ||||||||||||||||||||
| 69 | { | ||||||||||||||||||||
| 70 | 14 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
think: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 71 | 14 | $self->{'mode'} = "think"; | |||||||||||||||||||
| 72 | } | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | sub say | ||||||||||||||||||||
| 75 | { | ||||||||||||||||||||
| 76 | 1 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
say: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 77 | 1 | $self->{'mode'} = "say"; | |||||||||||||||||||
| 78 | } | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | sub print | ||||||||||||||||||||
| 81 | { | ||||||||||||||||||||
| 82 | 3 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
print: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 83 | 3 | $self->{'mode'} = "think"; | |||||||||||||||||||
| 84 | } | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | sub adjust | ||||||||||||||||||||
| 87 | { | ||||||||||||||||||||
| 88 | 2 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
adjust: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||
| 89 | 2 | ▶ | if (@_) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 90 | 2 | $self->{'fill'} = $_[0]; | |||||||||||||||||||
| 91 | } | ||||||||||||||||||||
| 92 | 2 | return $self->{'fill'}; | |||||||||||||||||||
| 93 | } | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | sub over | ||||||||||||||||||||
| 96 | { | ||||||||||||||||||||
| 97 | 2 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
over: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||
| 98 | 2 | ▶ | if (@_) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 99 | 2 | $self->{'over'} = $_[0]; | |||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | 2 | return $self->{'over'}; | |||||||||||||||||||
| 102 | } | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | sub as_list | ||||||||||||||||||||
| 105 | { | ||||||||||||||||||||
| 106 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
as_list: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 107 | 0 | return $self->_construct(); | |||||||||||||||||||
| 108 | } | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | sub as_string | ||||||||||||||||||||
| 111 | { | ||||||||||||||||||||
| 112 | 13 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
as_string: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 113 | 13 | return join('', $self->_construct()); | |||||||||||||||||||
| 114 | } | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | sub add | ||||||||||||||||||||
| 117 | { | ||||||||||||||||||||
| 118 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
add: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 119 | 0 | push @{$self->{'text'}}, @_; | |||||||||||||||||||
| 120 | 0 | return $self->{'text'}; | |||||||||||||||||||
| 121 | } | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | sub text | ||||||||||||||||||||
| 124 | { | ||||||||||||||||||||
| 125 | 3 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
text: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||
| 126 | 3 | ▶ | if (@_) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 127 | 3 | my @l = @_; | |||||||||||||||||||
| 128 | 3 | $self->{'text'} = \@l; | |||||||||||||||||||
| 129 | } | ||||||||||||||||||||
| 130 | 3 | return $self->{'text'}; | |||||||||||||||||||
| 131 | } | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | sub _maxlength | ||||||||||||||||||||
| 134 | { | ||||||||||||||||||||
| 135 | 13 | ▶ | my ($len, $max); | ||||||||||||||||||
|
Subroutine
_maxlength: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 136 | 13 | $max = -1; | |||||||||||||||||||
| 137 | 13 | for my $i (@_) { | |||||||||||||||||||
| 138 | 17 | $len = length $i; | |||||||||||||||||||
| 139 | 17 | ▶ | $max = $len if ($len > $max); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 140 | } | ||||||||||||||||||||
| 141 | 13 | return $max; | |||||||||||||||||||
| 142 | } | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | sub _fill_text | ||||||||||||||||||||
| 145 | { | ||||||||||||||||||||
| 146 | 13 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
_fill_text: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 147 | 13 | for my $i (@{$self->{'text'}}) { | |||||||||||||||||||
| 148 | 17 | $i =~ s/\s+$//; | |||||||||||||||||||
| 149 | } | ||||||||||||||||||||
| 150 | 13 | $Text::Tabs::tabstop = 8; | |||||||||||||||||||
| 151 | 13 | my @expanded = Text::Tabs::expand(@{$self->{'text'}}); | |||||||||||||||||||
| 152 | 13 | ▶ | unless ($self->{'fill'}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 153 | 2 | return @expanded; | |||||||||||||||||||
| 154 | } | ||||||||||||||||||||
| 155 | 11 | $Text::Wrap::columns = $self->{'wrap'}; | |||||||||||||||||||
| 156 | 11 | my @filled = split("\n", Text::Wrap::wrap("", "", @expanded)); | |||||||||||||||||||
| 157 | 11 | $Text::Tabs::tabstop = 1; # Defeat a dumb heuristic. | |||||||||||||||||||
| 158 | 11 | my @final = expand(@filled); | |||||||||||||||||||
| 159 | 11 | return @final; | |||||||||||||||||||
| 160 | } | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | sub _construct | ||||||||||||||||||||
| 163 | { | ||||||||||||||||||||
| 164 | 13 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
_construct: called CC=5 SCAR=16.1
| |||||||||||||||||||||
| 165 | 13 | my $mode = $self->{'mode'}; | |||||||||||||||||||
| 166 | 13 | my @message = $self->_fill_text(); | |||||||||||||||||||
| 167 | 13 | my $max = _maxlength(@message); | |||||||||||||||||||
| 168 | 13 | my $max2 = $max + 2; ## border space fudge. | |||||||||||||||||||
| 169 | 13 | my @border; ## up-left, up-right, down-left, down-right, left, right | |||||||||||||||||||
| 170 | 13 | my @balloon_lines = (); | |||||||||||||||||||
| 171 | 13 | my $shove = " " x $self->{'over'}; | |||||||||||||||||||
| 172 | 13 | my $format = "$shove%s %-${max}s %s\n"; | |||||||||||||||||||
| 173 | 13 | ▶ | if ($mode eq think) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 174 | 5 | @border = qw[ ( ) ( ) ( ) ]; | |||||||||||||||||||
| 175 | } elsif (@message < 2) { | ||||||||||||||||||||
| 176 | 7 | @border = qw[ < > ]; | |||||||||||||||||||
| 177 | } else { | ||||||||||||||||||||
| 178 | 1 | @border = ( "/", "\\", "\\", "/", "|", "|" ); | |||||||||||||||||||
| 179 | } | ||||||||||||||||||||
| 180 | push(@balloon_lines, | ||||||||||||||||||||
| 181 | "$shove " . ("_" x $max2) . "\n" , | ||||||||||||||||||||
| 182 | sprintf($format, $border[0], $message[0], $border[1]), | ||||||||||||||||||||
| 183 | (@message < 2 ? "" : | ||||||||||||||||||||
| 184 | 13 | ▶ | map { sprintf($format, $border[4], $_, $border[5]) } | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 185 | @message[1 .. $#message - 1]), | ||||||||||||||||||||
| 186 | (@message < 2 ? "" : | ||||||||||||||||||||
| 187 | sprintf($format, $border[2], $message[$#message], $border[3])), | ||||||||||||||||||||
| 188 | "$shove " . ("-" x $max2) . "\n" | ||||||||||||||||||||
| 189 | ); | ||||||||||||||||||||
| 190 | 13 | return @balloon_lines; | |||||||||||||||||||
| 191 | } | ||||||||||||||||||||
| 192 | |||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | =pod | ||||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | =head1 AUTHOR | ||||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | Tony Monroe E<lt>tmonroe plus perl at nog dot netE<gt> | ||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | =head1 SEE ALSO | ||||||||||||||||||||
| 201 | |||||||||||||||||||||
| 202 | L<Acme::Cow> | ||||||||||||||||||||
| 203 | |||||||||||||||||||||
| 204 | =cut | ||||||||||||||||||||
| 205 | |||||||||||||||||||||
| 206 | 4 | 1; | |||||||||||||||||||
| 207 | __END__ | ||||||||||||||||||||