Acme-Cow

 view release on metacpan or  search on metacpan

Cow/TextBalloon.pm  view on Meta::CPAN

{
    my $self = shift;
    if (@_) {
	my @l = @_;
	$self->{'text'} = \@l;
    }
    return $self->{'text'};
}

sub _maxlength 
{
    my ($len, $max);
    $max = -1;
    for my $i (@_) {
        $len = length $i;
        $max = $len if ($len > $max);
    }
    return $max;
}

sub _fill_text
{
    my $self = shift;
    for my $i (@{$self->{'text'}}) {
	$i =~ s/\s+$//;
    }
    $Text::Tabs::tabstop = 8;
    my @expanded = Text::Tabs::expand(@{$self->{'text'}});
    unless ($self->{'fill'}) {
	return @expanded;
    } 
    $Text::Wrap::columns = $self->{'wrap'};
    my @filled = split("\n", Text::Wrap::wrap("", "", @expanded));
    $Text::Tabs::tabstop = 1;	# Defeat a dumb heuristic.
    my @final = expand(@filled);
    return @final;
}

sub _construct
{
    my $self = shift;
    my $mode = $self->{'mode'};
    my @message = $self->_fill_text();
    my $max = _maxlength(@message);
    my $max2 = $max + 2;        ## border space fudge.
    my @border; ## up-left, up-right, down-left, down-right, left, right
    my @balloon_lines = ();
    my $shove = " " x $self->{'over'};
    my $format = "$shove%s %-${max}s %s\n";
    if ($mode eq think) {
        @border = qw[ ( ) ( ) ( ) ];
    } elsif (@message < 2) {
        @border = qw[ < > ];
    } else {
	@border = ( "/", "\\", "\\", "/", "|", "|" );
    }
    push(@balloon_lines,
        "$shove " . ("_" x $max2) . "\n" ,
        sprintf($format, $border[0], $message[0], $border[1]),
        (@message < 2 ? "" :
            map { sprintf($format, $border[4], $_, $border[5]) }
                @message[1 .. $#message - 1]),
        (@message < 2 ? "" :
            sprintf($format, $border[2], $message[$#message], $border[3])),
        "$shove " . ("-" x $max2) . "\n"
    );
    return @balloon_lines;
}


=pod

=head1 AUTHOR

Tony Monroe E<lt>tmonroe plus perl at nog dot netE<gt>

=head1 SEE ALSO

L<Acme::Cow>

=cut

1;
__END__



( run in 0.562 second using v1.01-cache-2.11-cpan-d8267643d1d )