blib/lib/Bot/Cobalt/Plugin/Figlet.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Bot::Cobalt::Plugin::Figlet;
2 # ABSTRACT: Bot::Cobalt plugin for displaying figlets
3 $Bot::Cobalt::Plugin::Figlet::VERSION = '0.001';
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Bot::Cobalt;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 0 ▶use Bot::Cobalt::Common;
Subroutine BEGIN: called CC=1 SCAR=2.6
9  
10 use List::AllUtils qw(shuffle);
11 use Path::Tiny;
12 use Text::FIGlet;
13  
14 sub new { bless {}, shift }
15 sub fontdir { path( shift->{fontdir} )->absolute }
16 sub width { shift->{maxwidth} }
17 sub prev { shift->{previous} }
18  
19 sub fonts {
20 my $self = shift;
21 my $extrx = qr/\.[tf]lf$/;
22
23 return map { $_->basename($extrx) } $self->fontdir->children($extrx);
24 }
25  
26 sub Cobalt_register {
27 my $self = shift;
28 my $core = shift;
29 my $conf = $core->get_plugin_cfg($self);
30  
31 $self->{fontdir} = $conf->{fontdir} || '/usr/games/lib/figlet';
32 $self->{maxwidth} = $conf->{maxwidth} || -1;
33  
34 if (not -d $self->fontdir) {
35 logger->warn("Unable to find figlet fonts at " . $self->fontdir);
36 return PLUGIN_EAT_NONE;
37 }
38  
39 register( $self, 'SERVER', 'public_cmd_figlet', 'public_cmd_prev_figlet' );
40  
41 logger->info("Registered, commands: !figlet, !prev_figlet");
42  
43 return PLUGIN_EAT_NONE;
44 }
45  
46 sub Cobalt_unregister {
47 my $self = shift;
48 my $core = shift;
49  
50 logger->info("Unregistered");
51  
52 return PLUGIN_EAT_NONE;
53 }
54  
55 sub Bot_public_cmd_prev_figlet {
56 my $self = shift;
57 my $core = shift;
58 my $msg = ${ shift() };
59  
60 broadcast( 'message', $msg->context, $msg->channel, $self->prev )
61 if $self->prev;
62  
63 return PLUGIN_EAT_ALL;
64 }
65  
66 sub Bot_public_cmd_figlet {
67 my $self = shift;
68 my $core = shift;
69  
70 my $msg = ${ shift() };
71 my $context = $msg->context;
72 my $channel = $msg->target;
73 my @split = split(/ /, $msg->stripped);
74  
75 shift @split;
76  
77 my $str = join(' ', @split);
78 my $font = (shuffle($self->fonts))[0];
79 my $figlet = Text::FIGlet->new(-d => $self->fontdir, -f => $font);
80 my $text = $figlet->figify(-A => $str, -w => $self->width);
81  
82 $self->{previous} = $font;
83  
84 broadcast( 'message', $context, $channel, $_)
85 foreach split /\n/, $text;
86  
87 return PLUGIN_EAT_ALL;
88 }
89  
90 1;
91  
92 __END__
↑ Summary