App-RoboBot

 view release on metacpan or  search on metacpan

lib/App/RoboBot/Plugin/Bot/Output.pm  view on Meta::CPAN

package App::RoboBot::Plugin::Bot::Output;
$App::RoboBot::Plugin::Bot::Output::VERSION = '4.004';
use v5.20;

use namespace::autoclean;

use Moose;
use MooseX::SetOnce;

use Data::Dumper;
use Number::Format;
use Scalar::Util qw( looks_like_number );

extends 'App::RoboBot::Plugin';

=head1 bot.output

Provides string formatting and output/display functions.

=cut

has '+name' => (
    default => 'Bot::Output',
);

has '+description' => (
    default => 'Provides string formatting and output/display functions.',
);

=head2 clear

=head3 Description

Clears current contents of the output buffer without displaying them.

This applies only to normal output - error messages will still be dispayed to
the user should occur.

=head2 join

=head3 Description

Joins together arguments into a single string, using the first argument as the
delimiter.

=head3 Usage

<delimiter string> <list>

=head3 Examples

    :emphasize-lines: 2

    (join ", " (seq 1 10))
    "1, 2, 3, 4, 5, 6, 7, 8, 9, 10"

=head2 split

=head3 Description

Splits a string into a list based on the delimiter provided. Delimiters may be
a regular expression or fixed string.

=head3 Usage

<delimiter> <string>

=head3 Examples

    :emphasize-lines: 2

    "[,\s]+" "1, 2, 3,4,    5"

lib/App/RoboBot/Plugin/Bot/Output.pm  view on Meta::CPAN

    eval {
        @list = split(($pattern =~ m{^m?[/\{|\[](.*)[/\}|\]]$}s ? m{$1} : $pattern), $string);
    };

    if ($@) {
        $message->response->raise('Invalid pattern provided for splitting.');
        return;
    }

    return @list;
}

sub str_lower {
    my ($self, $message, $command, $rpl, $str) = @_;

    return unless defined $str;
    return lc($str);
}

sub str_upper {
    my ($self, $message, $command, $rpl, $str) = @_;

    return uc($str);
}

sub print_str {
    my ($self, $message, $command, $rpl, @args) = @_;

    # Do nothing if we received nothing.
    return unless @args && @args > 0;

    # If we received only a single scalar value, send that unaltered as the message,
    # and return it to any outer expression.
    if (@args == 1 && !ref($args[0])) {
        $message->response->push($args[0]);
        return @args;
    }

    # For everything else, traverse the input and pretty-print it on a single
    # line with appropriate expression/type markup.
    my $output = '';
    _print_el($self->bot, \$output, $_) for @args;

    $output =~ s{(^\s+|\s+$)}{}ogs;

    $output = "($output)" if @args > 1;

    $message->response->push($output);
    return @args;
}

sub _print_el {
    my ($bot, $output, $el) = @_;

    if (!defined $el) {
        $$output .= " undef";
    } elsif (ref($el) eq 'HASH') {
        _print_map($bot, $output, $el);
    } elsif (ref($el) eq 'ARRAY') {
        _print_list($bot, $output, $el);
    } elsif (looks_like_number($el)) {
        $$output .= " $el";
    } else {
        $el =~ s{"}{\\"}g;
        $el =~ s{\n}{\\n}gs;
        $el =~ s{\r}{\\r}gs;
        $el =~ s{\t}{\\t}gs;
        $$output .= sprintf(' "%s"', $el);
    }

    return;
}

sub _print_list {
    my ($bot, $output, $list) = @_;

    $$output .= ' (';

    if (!ref($list->[0]) && (exists $bot->commands->{lc($list->[0])} || exists $bot->macros->{lc($list->[0])})) {
        $$output .= shift @{$list};
    }

    _print_el($bot, $output, $_) for @{$list};

    $$output .= ')';
}

sub _print_map {
    my ($bot, $output, $map) = @_;

    $$output .= ' {';

    foreach my $k (keys %{$map}) {
        $$output .= " $k";
        _print_el($bot, $output, $map->{$k});
    }

    $$output .= ' }';
}

sub format_str {
    my ($self, $message, $command, $rpl, $format, @args) = @_;

    my $str;

    eval { $str = sprintf($format, @args) };

    if ($@) {
        $message->response->raise(sprintf('Error: %s', $@));
        return;
    }

    return $str;
}

sub format_num {
    my ($self, $message, $command, $rpl, @args) = @_;

    return $self->nf->format_number(@args);
}



( run in 1.098 second using v1.01-cache-2.11-cpan-39bf76dae61 )