Text.pm

filter: Statementstmt 81.0% 120 / 148 Branchbran 55.0% 22 / 40 Conditioncond 40.0% 6 / 15 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 95.2% 20 / 21 Podpod n/a totaltotal 72.2% 169 / 234 CC 30 SCAR 25.5
CC
30
Cov
73%
__ANON__
24.2
__ANON__
17.2
__ANON__
6.0
CRAP 12.8
« Text/Cmd.pm ↑ Summary Text/Font/3d.pm »
1 package Ascii::Text;
2  
3 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $VERSION = '0.24';
8  
9 2 ▶use Rope;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Rope::Autoload;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Term::Size::ReadKey;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Module::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Types::Standard qw/Int Str Enum HashRef FileHandle/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 ▶use overload "&{}" => sub {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
16 2 my $self = shift;
17 2 ▶ return sub { $self->render(@_); }
Subroutine __ANON__: called CC=1 SCAR=0.0
18 2 };
19  
20 property max_width => (
21 initable => 1,
22 writeable => 1,
23 type => Int,
24 1 ▶ builder => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
25 1 Term::Size::ReadKey::chars *STDOUT{IO};
26 }
27 2 );
28  
29 2 property override_empty_space => (
30 initable => 1,
31 writeable => 1,
32 type => Int,
33 value => 0
34 );
35
36 2 property pad => (
37 initable => 1,
38 writeable => 1,
39 type => Int,
40 value => 0,
41 );
42  
43 2 property font => (
44 initable => 1,
45 writeable => 1,
46 type => Str,
47 value => 'Boomer'
48 );
49  
50 2 property align => (
51 initable => 1,
52 writeable => 1,
53 type => Enum[qw( left center right )],
54 value => 'left'
55 );
56  
57 2 property color => (
58 initable => 1,
59 type => Str,
60 writeable => 1,
61 );
62  
63 property color_map => (
64 initable => 1,
65 writeable => 1,
66 type => HashRef,
67 1 ▶ builder => sub { return {
Subroutine __ANON__: called CC=1 SCAR=0.0
68 1 black => "\e[30m",
69 red => "\e[31m",
70 green => "\e[32m",
71 yellow => "\e[33m",
72 blue => "\e[34m",
73 magenta => "\e[35m",
74 cyan => "\e[36m",
75 white => "\e[37m",
76 bright_black => "\e[90m",
77 bright_red => "\e[91m",
78 bright_green => "\e[92m",
79 bright_yellow => "\e[93m",
80 bright_blue => "\e[94m",
81 bright_magenta => "\e[95m",
82 bright_cyan => "\e[96m",
83 bright_white => "\e[37m",
84 } }
85 2 );
86  
87 property fh => (
88 initable => 1,
89 writeable => 1,
90 1 ▶ builder => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
91 1 return *STDOUT;
92 }
93 2 );
94  
95 4 ▶function font_class => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
96 4 my $class = sprintf "Ascii::Text::Font::%s", $_[0]->font;
97 4 load $class;
98 4 return $class;
99 2 };
100  
101 1 ▶function stringify => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
102 1 my ($self, $text, $wrap) = @_;
103 1 my $stringify = [];
104 1 $self->render($text, $stringify);
105 1 ▶ return join "", grep { $wrap ? $_ !~ m/^\s+$/ : $_ } @{$stringify};
Branch: 1 branch
BranchTrueFalseTotal
$wrap ? : 0 11 11
106 2 };
107  
108 3 ▶function render => sub {
Subroutine __ANON__: called CC=17 SCAR=31.9
109 3 my ($self, $text, $stringify) = @_;
110 3 $text =~ s/[^ 0-9A-Za-z]//;
111 3 my $class = $self->font_class->new;
112 3 my @words = split /\s+/, $text;
113 3 my %character_map;
114 3 for (@words) {
115 4 my @characters = split //, $_;
116 4 for (@characters) {
117 73 ▶ next if $character_map{$_};
Branch: 1 branch
BranchTrueFalseTotal
if $character_map{$_} 3 70 73
118 70 my $character = "character_$_";
119 70 $character = $class->$character;
120 70 $character_map{$_} = $character;
121 }
122 }
123 3 my ($width, @line) = $self->new_line();
124 3 ▶ while (@words) {
Branch: 1 branch
BranchTrueFalseTotal
if (@words) 4 3 7
125 4 my @characters = split //, shift @words;
126 4 ▶ for (my $i = 0; $i < scalar @characters; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @characters) 73 4 77
127 73 my $character = $character_map{$characters[$i]};
128 73 ▶ for (my $i = 0; $i < scalar @{$character}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @{$character;}) 773 73 846
129 773 push @{$line[$i]}, @{$character->[$i]};
130 }
131 73 $width -= scalar @{$character->[0]};
132  
133 73 my $next = $characters[$i + 1];
134 73 ▶ if ($next && $width < scalar @{$character_map{$next}->[0]}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next and $width < scalar @{$character_map{$next}[0];}) 68 5 73
Condition66%2/3
$next and $width < scalar @{$character_map{$next}[0];}
!ll&&!rl&&r
5068
MC/DC50%1/2
$next and $width < scalar @{$character_map{$next}[0];}
$next $width < scalar @{$character_map{$next}[0];}
Truth table66%2/3
A and B
A= $nextB= $width < scalar @{$character_map{$next}[0];}
ABresult
0X0
100
111
135 68 $self->print_line(\@line, $stringify);
136 68 ($width, @line) = $self->new_line();
137 }
138 }
139 4 ▶ if ($words[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($words[0]) 1 3 4
140 1 @characters = split //, $words[0];
141 1 ▶ my $space = $self->_empty_space($class) || $class->space;
Condition33%1/3
$self->_empty_space($class) || $class->space
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->_empty_space($class) || $class->space
$self->_empty_space($class) $class->space
Truth table33%1/3
A || B
A= $self->_empty_space($class)B= $class->space
ABresult
1X1
011
000
142 1 my $required_width = scalar @{$space->[0]};
143 1 $required_width += @{$character_map{$_}->[0]} for @characters;
144 1 ▶ if ($width > $required_width) {
Branch: 1 branch
BranchTrueFalseTotal
if ($width > $required_width) 0 1 1
145 0 ▶ for (my $i = 0; $i < scalar @{$space}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @{$space;}) 0 0 0
146 0 push @{$line[$i]}, @{$space->[$i]}, " ";
147 }
148 0 $width = $width - scalar @{$space->[0]};
149 0 next;
150 }
151 }
152 4 $self->print_line(\@line, $stringify);
153 4 ($width, @line) = $self->new_line();
154 }
155 3 ▶ if ($self->color && $self->color_map->{$self->color}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->color and $self->color_map->{$self->color}) 0 3 3
Condition33%1/3
$self->color and $self->color_map->{$self->color}
!ll&&!rl&&r
300
MC/DC0%0/2
$self->color and $self->color_map->{$self->color}
$self->color $self->color_map->{$self->color}
Truth table33%1/3
A and B
A= $self->colorB= $self->color_map->{$self->color}
ABresult
0X0
100
111
156 0 ▶ if ($stringify) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stringify) { } 0 0 0
157 0 push @{$stringify}, "\e[0m";
158 } else {
159 0 my $fh = $self->fh;
160 0 print $fh "\e[0m";
161 }
162 }
163 2 };
164  
165 75 ▶function new_line => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
166 75 return ($_[0]->max_width, [],[],[],[],[],[]);
167 2 };
168  
169 72 ▶function print_line => sub {
Subroutine __ANON__: called CC=11 SCAR=28.5
170 72 my ($self, $line, $stringify) = @_;
171 72 my $line_width = @{$line->[0]};
172 72 ▶ my $pad = $self->align eq 'center'
Branch: 2 branches
BranchTrueFalseTotal
$self->align eq 'center' ? : 72 0 72
$self->align eq 'right' ? : 0 0 0
173 ? ($self->max_width - $line_width) / 2
174 : $self->align eq 'right'
175 ? $self->max_width - $line_width
176 : $self->pad;
177 72 ▶ $pad = $pad && $pad > 0 ? " " x $pad : "";
Branch: 1 branch
BranchTrueFalseTotal
$pad && $pad > 0 ? : 0 72 72
Condition33%1/3
$pad and $pad > 0
!ll&&!rl&&r
0720
MC/DC0%0/2
$pad and $pad > 0
$pad $pad > 0
Truth table33%1/3
A and B
A= $padB= $pad > 0
ABresult
0X0
100
111
178 72 my $fh = $self->fh;
179 72 for (@{$line}) {
180 762 ▶ next unless scalar @{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$_;} 0 762 762
181 762 ▶ if ($self->color && $self->color_map->{$self->color}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->color and $self->color_map->{$self->color}) 0 762 762
Condition33%1/3
$self->color and $self->color_map->{$self->color}
!ll&&!rl&&r
76200
MC/DC0%0/2
$self->color and $self->color_map->{$self->color}
$self->color $self->color_map->{$self->color}
Truth table33%1/3
A and B
A= $self->colorB= $self->color_map->{$self->color}
ABresult
0X0
100
111
182 0 ▶ if ($stringify) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stringify) { } 0 0 0
183 0 push @{$stringify}, $self->color_map->{$self->color};
184 } else {
185 0 print $fh $self->color_map->{$self->color};
186 }
187 }
188 762 my $l = $pad;
189 762 $l .= join "", @{$_};
190 762 $l .= "\n";
191 762 ▶ if ($stringify) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stringify) { } 11 751 762
192 11 push @{$stringify}, $l;
193 } else {
194 751 print $fh $l;
195 }
196 }
197 2 };
198  
199  
200 0 ▶function list => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
201 0 my ($self) = @_;
202 0 my $base_path = __FILE__;
203 0 $base_path =~ s/.pm$//;
204 0 $base_path .= '/Font';
205 0 ▶ opendir my $dir, $base_path or die $!;
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dir, $base_path or die $! 0 0 0
206 0 my @files = sort grep { $_ =~ s/\.pm$//; $_ !~ m/^\./ } readdir $dir;
207 0 closedir $dir;
208 0 return @files;
209 2 };
210  
211 1 ▶function _empty_space => sub {
Subroutine __ANON__: called CC=2 SCAR=8.1
212 1 my ($self, $font) = @_;
213 1 ▶ return unless $self->override_empty_space;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->override_empty_space 1 0 1
214 0 return [$font->default_character($self->override_empty_space)];
215 2 };
216  
217 2 1;
218  
219 __END__
« Text/Cmd.pm ↑ Summary Text/Font/3d.pm »