| 1 | package App::KGB::Painter; | ||||||||||
| 2 | |||||||||||
| 3 | =head1 NAME | ||||||||||
| 4 | |||||||||||
| 5 | App::KGB::Painter -- add color to KGB notifications | ||||||||||
| 6 | |||||||||||
| 7 | =head1 DESCRIPTION | ||||||||||
| 8 | |||||||||||
| 9 | B<App::KGB::Painter> is a simple class encapsulating coloring of KGB messages. | ||||||||||
| 10 | |||||||||||
| 11 | =cut | ||||||||||
| 12 | |||||||||||
| 13 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | 1 | our $VERSION = 1.27; | |||||||||
| 17 | |||||||||||
| 18 | 1 | ▶ | use base 'Class::Accessor::Fast'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | |||||||||||
| 20 | 1 | __PACKAGE__->mk_accessors( qw(item_colors color_codes simulate) ); | |||||||||
| 21 | |||||||||||
| 22 | 1 | our %color_codes = ( | |||||||||
| 23 | bold => "\002", # ^B | ||||||||||
| 24 | normal => "\017", # ^O | ||||||||||
| 25 | underline => "\037", # ^_ | ||||||||||
| 26 | reverse => "\026", # ^V | ||||||||||
| 27 | black => "\00301", | ||||||||||
| 28 | navy => "\00302", | ||||||||||
| 29 | green => "\00303", | ||||||||||
| 30 | red => "\00304", | ||||||||||
| 31 | brown => "\00305", | ||||||||||
| 32 | purple => "\00306", | ||||||||||
| 33 | orange => "\00307", | ||||||||||
| 34 | yellow => "\00308", | ||||||||||
| 35 | lime => "\00309", | ||||||||||
| 36 | teal => "\00310", | ||||||||||
| 37 | aqua => "\00311", | ||||||||||
| 38 | blue => "\00312", | ||||||||||
| 39 | fuchsia => "\00313", | ||||||||||
| 40 | silver => "\00314", | ||||||||||
| 41 | white => "\00316", | ||||||||||
| 42 | reset => "\017", | ||||||||||
| 43 | ); | ||||||||||
| 44 | |||||||||||
| 45 | 1 | our %item_colors = ( | |||||||||
| 46 | revision => undef, | ||||||||||
| 47 | path => 'teal', | ||||||||||
| 48 | author => 'purple', | ||||||||||
| 49 | branch => 'brown', | ||||||||||
| 50 | project => 'blue', | ||||||||||
| 51 | module => 'green', | ||||||||||
| 52 | web => 'silver', | ||||||||||
| 53 | separator => undef, | ||||||||||
| 54 | |||||||||||
| 55 | addition => 'green', | ||||||||||
| 56 | modification => 'teal', | ||||||||||
| 57 | deletion => 'bold red', | ||||||||||
| 58 | replacement => 'brown', | ||||||||||
| 59 | |||||||||||
| 60 | prop_change => 'underline', | ||||||||||
| 61 | ); | ||||||||||
| 62 | |||||||||||
| 63 | =head1 CONSTRUCTOR | ||||||||||
| 64 | |||||||||||
| 65 | =head2 new | ||||||||||
| 66 | |||||||||||
| 67 | my $p = App::KGB::Painter->new({ color_codes => { ... }, item_colors => { ... } } ); | ||||||||||
| 68 | |||||||||||
| 69 | B<color_codes> is a hash with the special symbols interpreted as coloring | ||||||||||
| 70 | commands by IRC clients. | ||||||||||
| 71 | |||||||||||
| 72 | B<item_colors> is another hash describing what colors to apply to different parts of | ||||||||||
| 73 | the messages. | ||||||||||
| 74 | |||||||||||
| 75 | =cut | ||||||||||
| 76 | |||||||||||
| 77 | 0 | ▶ | sub new { | ||||||||
|
Subroutine
new: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||
| 78 | 0 | my $self = shift->SUPER::new(@_); | |||||||||
| 79 | |||||||||||
| 80 | # default colors | ||||||||||
| 81 | 0 | ▶ | $self->color_codes( \%color_codes ) unless $self->color_codes; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | 0 | my $c = $self->color_codes; | |||||||||
| 83 | 0 | ▶ | while ( my ($k,$v) = each %color_codes ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | 0 | ▶ | $c->{$k} = $v unless exists $c->{$k}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | # default styles | ||||||||||
| 88 | 0 | ▶ | $self->item_colors( \%item_colors ) unless $self->item_colors; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | 0 | my $s = $self->item_colors; | |||||||||
| 90 | 0 | ▶ | while ( my ($k,$v) = each %item_colors ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 91 | 0 | ▶ | $s->{$k} = $v unless exists $s->{$k}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | 0 | return $self; | |||||||||
| 95 | } | ||||||||||
| 96 | |||||||||||
| 97 | =head1 METHODS | ||||||||||
| 98 | |||||||||||
| 99 | =over | ||||||||||
| 100 | |||||||||||
| 101 | =item B<colorize> I<category> I<text> | ||||||||||
| 102 | |||||||||||
| 103 | Applies the colors of the style I<category> to the given I<text>. | ||||||||||
| 104 | |||||||||||
| 105 | =cut | ||||||||||
| 106 | |||||||||||
| 107 | 0 | ▶ | sub colorize { | ||||||||
|
Subroutine
colorize: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||
| 108 | 0 | my ( $self, $category, $text ) = @_; | |||||||||
| 109 | |||||||||||
| 110 | 0 | ▶ | return $text if $self->simulate; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 111 | |||||||||||
| 112 | 0 | my $color = $self->item_colors->{$category}; | |||||||||
| 113 | |||||||||||
| 114 | 0 | ▶ | unless ($color) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 115 | 0 | warn | |||||||||
| 116 | "Not coloring '$text' due to unknown color '$color' for category '$category'" | ||||||||||
| 117 | if 0; | ||||||||||
| 118 | 0 | return $text; | |||||||||
| 119 | } | ||||||||||
| 120 | |||||||||||
| 121 | 0 | my $c = $self->color_codes; | |||||||||
| 122 | |||||||||||
| 123 | 0 | for ( split( /\s+/, $color ) ) { | |||||||||
| 124 | 0 | ▶ | $text = $c->{$_} . $text if $c->{$_}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 125 | } | ||||||||||
| 126 | |||||||||||
| 127 | 0 | $text .= $c->{reset}; | |||||||||
| 128 | |||||||||||
| 129 | 0 | warn "Colored ($category/$color): $text" if 0; | |||||||||
| 130 | 0 | return $text; | |||||||||
| 131 | } | ||||||||||
| 132 | |||||||||||
| 133 | 1 | our %action_items = ( | |||||||||
| 134 | A => 'addition', | ||||||||||
| 135 | M => 'modification', | ||||||||||
| 136 | D => 'deletion', | ||||||||||
| 137 | R => 'replacement', | ||||||||||
| 138 | ); | ||||||||||
| 139 | |||||||||||
| 140 | =item B<colorize_change> I<change> | ||||||||||
| 141 | |||||||||||
| 142 | Given a change, applies colors to its files depending on the type of the change | ||||||||||
| 143 | -- update, addition, deletion or property change. | ||||||||||
| 144 | |||||||||||
| 145 | =cut | ||||||||||
| 146 | |||||||||||
| 147 | 0 | ▶ | sub colorize_change { | ||||||||
|
Subroutine
colorize_change: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 148 | 0 | my $self = shift; | |||||||||
| 149 | 0 | my $c = shift; | |||||||||
| 150 | |||||||||||
| 151 | 0 | my $action_item = $action_items{ $c->action }; | |||||||||
| 152 | 0 | ▶ | unless ($action_item) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 153 | 0 | warn $c->action . " is an unknown action\n"; | |||||||||
| 154 | 0 | return $c->path; | |||||||||
| 155 | } | ||||||||||
| 156 | |||||||||||
| 157 | 0 | my $text = $self->colorize( $action_item, $c->path ); | |||||||||
| 158 | |||||||||||
| 159 | 0 | ▶ | $text = $self->colorize( 'prop_change', $text ) if $c->prop_change; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 160 | |||||||||||
| 161 | 0 | return $text; | |||||||||
| 162 | } | ||||||||||
| 163 | |||||||||||
| 164 | =back | ||||||||||
| 165 | |||||||||||
| 166 | =head1 COPYRIGHT & LICENSE | ||||||||||
| 167 | |||||||||||
| 168 | Copyright (c) 2013, 2018, Damyan Ivanov L<dmn@debian.org>. | ||||||||||
| 169 | |||||||||||
| 170 | This module is free software; you can redistribute it and/or modify it under | ||||||||||
| 171 | the terms of the GNU General Public License as published by the Free Software | ||||||||||
| 172 | Foundation; either version 2 of the License, or (at your option) any later | ||||||||||
| 173 | version. | ||||||||||
| 174 | |||||||||||
| 175 | This program is distributed in the hope that it will be useful, but WITHOUT | ||||||||||
| 176 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||||||||||
| 177 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||||||||||
| 178 | details. | ||||||||||
| 179 | |||||||||||
| 180 | You should have received a copy of the GNU General Public License along with | ||||||||||
| 181 | this program; if not, write to the Free Software Foundation, Inc., 51 | ||||||||||
| 182 | Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||||||||
| 183 | |||||||||||
| 184 | =cut | ||||||||||
| 185 | |||||||||||
| 186 | 1 | 1; | |||||||||