blib/lib/Unicode/Block/Ascii.pm

filter: Statementstmt 50.0% 40 / 80 Branchbran 23.0% 6 / 26 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 9 / 9 Podpod 100% 1 / 1 totaltotal 47.1% 57 / 121 SCAR 42.5
CC
17
Cov
43%
get
154.8
_get_chars
4.0
BEGIN
1.0
CRAP 70.2
↑ Summary
1 package Unicode::Block::Ascii;
2  
3 5 ▶use base qw(Unicode::Block);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use Error::Pure qw(err);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Text::UnicodeBox;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Text::UnicodeBox::Control qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # Constants.
13 5 Readonly::Scalar our $SPACE => q{ };
14  
15 5 our $VERSION = 0.03;
16  
17 # Get output.
18 sub get {
19 1 ▶ my $self = shift;
Subroutine get: called CC=14 SCAR=50.4
Pod documented
20  
21 # Get width.
22 1 $self->_get_chars;
23 $self->{'_width'} = 16 + $self->{'_base_width'}
24 1 + (16 * $self->{'_char_width'});
25  
26 # Check width.
27 1 ▶ if (defined $self->{'title'}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"title"} and length $self->{"title"} > $self->{"_width"}) 1 0 1
Condition33%1/3
defined $self->{"title"} and length $self->{"title"} > $self->{"_width"}
!ll&&!rl&&r
001
MC/DC0%0/2
defined $self->{"title"} and length $self->{"title"} > $self->{"_width"}
defined $self->{"title"} length $self->{"title"} > $self->{"_width"}
Truth table33%1/3
A and B
A= defined $self->{"title"}B= length $self->{"title"} > $self->{"_width"}
ABresult
0X0
100
111
28 && (length $self->{'title'}) > $self->{'_width'}) {
29  
30 1 err 'Long title.';
31 }
32  
33 # Box objext.
34 0 my $box = Text::UnicodeBox->new;
35  
36 # Title.
37 0 ▶ if (defined $self->{'title'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"title"}) 0 0 0
38  
39 # Compute title.
40 0 my $spaces = $self->{'_width'} - length $self->{'title'};
41 0 my $left = int($spaces / 2);
42 0 my $right = $self->{'_width'} - $left - length $self->{'title'};
43 0 my $title = ($SPACE x $left).$self->{'title'}.($SPACE x $right);
44  
45 # Add title.
46 0 $box->add_line(
47 BOX_START('top' => 'light', 'bottom' => 'light'),
48 $title,
49 BOX_END(),
50 );
51 }
52  
53 # Header.
54 0 my @headers = $SPACE x $self->{'_base_width'}, BOX_RULE;
55 0 foreach my $header_char (0 .. 9, 'A' .. 'F') {
56 0 ▶ if (@headers) {
Branch: 1 branch
BranchTrueFalseTotal
if (@headers) 0 0 0
57 0 push @headers, BOX_RULE;
58 }
59 0 my $table_header_char = $header_char;
60 0 ▶ if ($self->{'_char_width'} > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_char_width"} > 1) 0 0 0
61 $table_header_char
62 0 = ($SPACE x ($self->{'_char_width'} - 1)).
63 $header_char;
64 }
65 0 push @headers, $table_header_char;
66 }
67 0 my @title;
68 0 ▶ if (! defined $self->{'title'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"title"}) 0 0 0
69 0 @title = ('top' => 'light');
70 }
71 $box->add_line(
72 0 BOX_START(@title, 'bottom' => 'light'), @headers, BOX_END(),
73 );
74  
75 # Columns.
76 0 my @cols;
77 0 foreach my $item (@{$self->{'_chars'}}) {
78 0 ▶ if (@cols) {
Branch: 1 branch
BranchTrueFalseTotal
if (@cols) { } 0 0 0
79 0 push @cols, BOX_RULE;
80 } else {
81 0 push @cols, $SPACE.$item->base.$SPACE, BOX_RULE;
82 0 my $last_num = hex $item->last_hex;
83 0 ▶ if ($last_num > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_num > 0) 0 0 0
84 0 push @cols, ($SPACE, BOX_RULE) x $last_num;
85 }
86 }
87 0 my $char = $item->char;
88 0 ▶ if ($item->width < $self->{'_char_width'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->width < $self->{"_char_width"}) 0 0 0
89 0 $char = ($SPACE x ($self->{'_char_width'}
90 - $item->width)).$char;
91 }
92 0 push @cols, $char;
93 0 ▶ if ($item->last_hex eq 'f') {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->last_hex eq "f") 0 0 0
94 0 $box->add_line(
95 BOX_START('bottom' => 'light'),
96 @cols,
97 BOX_END(),
98 );
99 0 @cols = ();
100 }
101 }
102 0 ▶ if (@cols) {
Branch: 1 branch
BranchTrueFalseTotal
if (@cols) 0 0 0
103 0 my $spaces = @cols / 2;
104 0 $box->add_line(
105 BOX_START('bottom' => 'light'),
106 @cols, BOX_RULE,
107 ($SPACE, BOX_RULE) x (16 - $spaces),
108 $SPACE,
109 BOX_END,
110 );
111 }
112 0 return $box->render;
113 }
114  
115 # Get chars and compute char width.
116 sub _get_chars {
117 1 ▶ my $self = shift;
Subroutine _get_chars: called CC=4 SCAR=13.9
118 1 $self->{'_chars'} = [];
119 1 $self->{'_char_width'} = 1;
120 1 $self->{'_base_width'} = 0;
121 1 ▶ while (my $item = $self->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $item = $self->next) 128 1 129
122  
123 # Look for maximal character width in table.
124 128 ▶ if ($item->width > $self->{'_char_width'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->width > $self->{"_char_width"}) 0 128 128
125 0 $self->{'_char_width'} = $item->width;
126 }
127  
128 # Look for maximal base length in table.
129 128 ▶ if ((length $item->base) + 2 > $self->{'_base_width'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length($item->base) + 2 > $self->{"_base_width"}) 1 127 128
130 1 $self->{'_base_width'} = (length $item->base) + 2;
131 }
132  
133 # Add character.
134 128 push @{$self->{'_chars'}}, $item;
135 }
136 1 return;
137 }
138  
139 5 1;
140  
141 __END__
142  
143 =pod
144  
145 =encoding utf8
146  
147 =head1 NAME
148  
149 Unicode::Block::Ascii - Ascii output of unicode block.
150  
151 =head1 SYNOPSIS
152  
153 use Unicode::Block::Ascii;
154 my $obj = Unicode::Block::Ascii->new(%parameters);
155 my $output = $obj->get;
156 my $item = $obj->next;
157  
158 =head1 METHODS
159  
160 =over 8
161  
162 =item C<new(%parameters)>
163  
164 Constructor.
165  
166 =over 8
167  
168 =item * C<char_from>
169  
170 Character from.
171 Default value is '0000'.
172  
173 =item * C<char_to>
174  
175 Character to.
176 Default value is '007f'.
177  
178 =item * C<title>
179  
180 Title of block.
181 Default value is undef.
182  
183 =back
184  
185 =item C<get()>
186  
187 Get output.
188 Return string with ascii table of Unicode::Block object.
189  
190 =item C<next()>
191  
192 Get next character.
193 Returns Unicode::Block::Item object for character, if character exists.
194 Returns undef, if character doesn't exist.
195  
196 =back
197  
198 =head1 ERRORS
199  
200 new():
201 From Class::Utils::set_params_pub():
202 Unknown parameter '%s'.
203  
204 get():
205 Long title.
206  
207 =head1 EXAMPLE1
208  
209 use strict;
210 use warnings;
211  
212 use Encode qw(encode_utf8);
213 use Unicode::Block::Ascii;
214 use Unicode::Block::List;
215  
216 # Arguments.
217 if (@ARGV < 1) {
218 print STDERR "Usage: $0 block_name\n";
219 exit 1;
220 }
221 my $block_name = $ARGV[0];
222  
223 # List object.
224 my $obj = Unicode::Block::List->new;
225  
226 # Get Unicode::Block for block name.
227 my $block = $obj->block($block_name);
228  
229 # Get ASCII object.
230 my $block_ascii = Unicode::Block::Ascii->new(%{$block});
231  
232 # Print to output.
233 print encode_utf8($block_ascii->get)."\n";
234
235 # Output:
236 # Usage: /tmp/o1NG0vm_Wf block_name
237  
238 # Output with 'Block Elements' argument:
239 # ┌────────────────────────────────────────┐
240 # │ Block Elements │
241 # ├────────┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┤
242 # │ │0│1│2│3│4│5│6│7│8│9│A│B│C│D│E│F│
243 # ├────────┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤
244 # │ U+258x │▀│▁│▂│▃│▄│▅│▆│▇│█│▉│▊│▋│▌│▍│▎│▏│
245 # ├────────┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤
246 # │ U+259x │▐│░│▒│▓│▔│▕│▖│▗│▘│▙│▚│▛│▜│▝│▞│▟│
247 # └────────┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┘
248  
249 =head1 EXAMPLE2
250  
251 use strict;
252 use warnings;
253
254 use Curses::UI;
255 use Encode qw(encode_utf8);
256 use Unicode::Block::Ascii;
257 use Unicode::Block::List;
258
259 # Get unicode block list.
260 my $list = Unicode::Block::List->new;
261 my @unicode_block_list = $list->list;
262
263 # Window.
264 my $cui = Curses::UI->new;
265 my $win = $cui->add('window_id', 'Window');
266 $win->set_binding(\&exit, "\cQ", "\cC");
267
268 # Popup menu.
269 my $popupbox = $win->add(
270 'mypopupbox', 'Popupmenu',
271 '-labels' => {
272 map { $_, $_ } @unicode_block_list,
273 },
274 '-onchange' => sub {
275 my $self = shift;
276 $cui->leave_curses;
277 my $block = $list->block($self->get);
278 my $block_ascii = Unicode::Block::Ascii->new(%{$block});
279 print encode_utf8($block_ascii->get)."\n";
280 exit 0;
281 },
282 '-values' => \@unicode_block_list,
283 );
284 $popupbox->focus;
285
286 # Loop.
287 $cui->mainloop;
288  
289 # Output after select 'Geometric Shapes' item:
290 # ┌────────────────────────────────────────────────────────┐
291 # │ Geometric Shapes │
292 # ├────────┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┬──┤
293 # │ │ 0│ 1│ 2│ 3│ 4│ 5│ 6│ 7│ 8│ 9│ A│ B│ C│ D│ E│ F│
294 # ├────────┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┤
295 # │ U+25ax │ ■│ □│ ▢│ ▣│ ▤│ ▥│ ▦│ ▧│ ▨│ ▩│ ▪│ ▫│ ▬│ ▭│ ▮│ ▯│
296 # ├────────┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┤
297 # │ U+25bx │ ▰│ ▱│ ▲│ △│ ▴│ ▵│ ▶│ ▷│ ▸│ ▹│ ►│ ▻│ ▼│ ▽│ ▾│ ▿│
298 # ├────────┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┤
299 # │ U+25cx │ ◀│ ◁│ ◂│ ◃│ ◄│ ◅│ ◆│ ◇│ ◈│ ◉│ ◊│ ○│ ◌│ ◍│ ◎│ ●│
300 # ├────────┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┤
301 # │ U+25dx │ ◐│ ◑│ ◒│ ◓│ ◔│ ◕│ ◖│ ◗│ ◘│ ◙│ ◚│ ◛│ ◜│ ◝│ ◞│ ◟│
302 # ├────────┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┤
303 # │ U+25ex │ ◠│ ◡│ ◢│ ◣│ ◤│ ◥│ ◦│ ◧│ ◨│ ◩│ ◪│ ◫│ ◬│ ◭│ ◮│ ◯│
304 # ├────────┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┼──┤
305 # │ U+25fx │ ◰│ ◱│ ◲│ ◳│ ◴│ ◵│ ◶│ ◷│ ◸│ ◹│ ◺│ ◻│ ◼│◽│◾│ ◿│
306 # └────────┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┴──┘
307  
308 =head1 DEPENDENCIES
309  
310 L<Error::Pure>,
311 L<Readonly>,
312 L<Text::UnicodeBox>,
313 L<Text::UnicodeBox::Control>,
314 L<Unicode::Block>.
315  
316 =head1 REPOSITORY
317  
318 L<https://github.com/tupinek/Unicode-Block-Ascii>
319  
320 =head1 AUTHOR
321  
322 Michal Josef Špaček L<mailto:skim@cpan.org>
323  
324 L<http://skim.cz>
325  
326 =head1 LICENSE AND COPYRIGHT
327  
328 © 2013-2018 Michal Josef Špaček
329 BSD 2-Clause License
330  
331 =head1 VERSION
332  
333 0.03
334  
335 =cut
↑ Summary