File Coverage

blib/lib/Unicode/Block/Ascii.pm
Criterion Covered Total %
statement 16 18 88.8
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 22 24 91.6


line stmt bran cond sub pod time code
1             package Unicode::Block::Ascii;
2              
3             # Pragmas.
4 3     3   42774 use base qw(Unicode::Block);
  3         5  
  3         2274  
5 3     3   20 use strict;
  3         5  
  3         105  
6 3     3   14 use warnings;
  3         20  
  3         108  
7              
8             # Modules.
9 3     3   2628 use Error::Pure qw(err);
  3         40448  
  3         80  
10 3     3   179 use Readonly;
  3         5  
  3         93  
11 3     3   122041 use Text::UnicodeBox;
  0            
  0            
12             use Text::UnicodeBox::Control qw(:all);
13              
14             # Constants.
15             Readonly::Scalar our $SPACE => q{ };
16              
17             # Version.
18             our $VERSION = 0.01;
19              
20             # Get output.
21             sub get {
22             my $self = shift;
23              
24             # Get width.
25             $self->_get_chars;
26             $self->{'_width'} = 16 + $self->{'_base_width'}
27             + (16 * $self->{'_char_width'});
28              
29             # Check width.
30             if (defined $self->{'title'}
31             && (length $self->{'title'}) > $self->{'_width'}) {
32              
33             err 'Long title.';
34             }
35              
36             # Box objext.
37             my $box = Text::UnicodeBox->new;
38              
39             # Title.
40             if (defined $self->{'title'}) {
41              
42             # Compute title.
43             my $spaces = $self->{'_width'} - length $self->{'title'};
44             my $left = int($spaces / 2);
45             my $right = $self->{'_width'} - $left - length $self->{'title'};
46             my $title = ($SPACE x $left).$self->{'title'}.($SPACE x $right);
47              
48             # Add title.
49             $box->add_line(
50             BOX_START('top' => 'light', 'bottom' => 'light'),
51             $title,
52             BOX_END(),
53             );
54             }
55              
56             # Header.
57             my @headers = $SPACE x $self->{'_base_width'}, BOX_RULE;
58             foreach my $header_char (0 .. 9, 'A' .. 'F') {
59             if (@headers) {
60             push @headers, BOX_RULE;
61             }
62             my $table_header_char = $header_char;
63             if ($self->{'_char_width'} > 1) {
64             $table_header_char
65             = ($SPACE x ($self->{'_char_width'} - 1)).
66             $header_char;
67             }
68             push @headers, $table_header_char;
69             }
70             my @title;
71             if (! defined $self->{'title'}) {
72             @title = ('top' => 'light');
73             }
74             $box->add_line(
75             BOX_START(@title, 'bottom' => 'light'), @headers, BOX_END(),
76             );
77              
78             # Columns.
79             my @cols;
80             foreach my $item (@{$self->{'_chars'}}) {
81             if (@cols) {
82             push @cols, BOX_RULE;
83             } else {
84             push @cols, $SPACE.$item->base.$SPACE, BOX_RULE;
85             my $last_num = hex $item->last_hex;
86             if ($last_num > 0) {
87             push @cols, ($SPACE, BOX_RULE) x $last_num;
88             }
89             }
90             my $char = $item->char;
91             if ($item->width < $self->{'_char_width'}) {
92             $char = ($SPACE x ($self->{'_char_width'}
93             - $item->width)).$char;
94             }
95             push @cols, $char;
96             if ($item->last_hex eq 'f') {
97             $box->add_line(
98             BOX_START('bottom' => 'light'),
99             @cols,
100             BOX_END(),
101             );
102             @cols = ();
103             }
104             }
105             if (@cols) {
106             my $spaces = @cols / 2;
107             $box->add_line(
108             BOX_START('bottom' => 'light'),
109             @cols, BOX_RULE,
110             ($SPACE, BOX_RULE) x (16 - $spaces),
111             $SPACE,
112             BOX_END,
113             );
114             }
115             return $box->render;
116             }
117              
118             # Get chars and compute char width.
119             sub _get_chars {
120             my $self = shift;
121             $self->{'_chars'} = [];
122             $self->{'_char_width'} = 1;
123             $self->{'_base_width'} = 0;
124             while (my $item = $self->next) {
125              
126             # Look for maximal character width in table.
127             if ($item->width > $self->{'_char_width'}) {
128             $self->{'_char_width'} = $item->width;
129             }
130              
131             # Look for maximal base length in table.
132             if ((length $item->base) + 2 > $self->{'_base_width'}) {
133             $self->{'_base_width'} = (length $item->base) + 2;
134             }
135              
136             # Add character.
137             push @{$self->{'_chars'}}, $item;
138             }
139             return;
140             }
141              
142             1;
143              
144             __END__
145              
146             =pod
147              
148             =encoding utf8
149              
150             =head1 NAME
151              
152             Unicode::Block::Ascii - Ascii output of unicode block.
153              
154             =head1 SYNOPSIS
155              
156             use Unicode::Block::Ascii;
157             my $obj = Unicode::Block::Ascii->new(%parameters);
158             my $output = $obj->get;
159             my $item = $obj->next;
160              
161             =head1 METHODS
162              
163             =over 8
164              
165             =item C<new(%parameters)>
166              
167             Constructor.
168              
169             =over 8
170              
171             =item * C<char_from>
172              
173             Character from.
174             Default value is '0000'.
175              
176             =item * C<char_to>
177              
178             Character to.
179             Default value is '007f'.
180              
181             =item * C<title>
182              
183             Title of block.
184             Default value is undef.
185              
186             =back
187              
188             =item C<get()>
189              
190             Get output.
191             Return string with ascii table of Unicode::Block object.
192              
193             =item C<next()>
194              
195             Get next character.
196             Returns Unicode::Block::Item object for character, if character exists.
197             Returns undef, if character doesn't exist.
198              
199             =back
200              
201             =head1 ERRORS
202              
203             new():
204             From Class::Utils::set_params_pub():
205             Unknown parameter '%s'.
206              
207             get():
208             Long title.
209              
210             =head1 EXAMPLE
211              
212             # Pragmas.
213             use strict;
214             use warnings;
215              
216             # Modules.
217             use Encode qw(encode_utf8);
218             use Unicode::Block::Ascii;
219             use Unicode::Block::List;
220              
221             # Arguments.
222             if (@ARGV < 1) {
223             print STDERR "Usage: $0 block_name\n";
224             exit 1;
225             }
226             my $block_name = $ARGV[0];
227              
228             # List object.
229             my $obj = Unicode::Block::List->new;
230              
231             # Get Unicode::Block for block name.
232             my $block = $obj->block($block_name);
233              
234             # Get ASCII object.
235             my $block_ascii = Unicode::Block::Ascii->new(%{$block});
236              
237             # Print to output.
238             print encode_utf8($block_ascii->get)."\n";
239            
240             # Output:
241             # Usage: /tmp/o1NG0vm_Wf block_name
242              
243             # Output with 'Block Elements' argument:
244             # ┌────────────────────────────────────────┐
245             # │ Block Elements │
246             # ├────────┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┤
247             # │ │0│1│2│3│4│5│6│7│8│9│A│B│C│D│E│F│
248             # ├────────┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤
249             # │ U+258x │▀│▁│▂│▃│▄│▅│▆│▇│█│▉│▊│▋│▌│▍│▎│▏│
250             # ├────────┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤
251             # │ U+259x │▐│░│▒│▓│▔│▕│▖│▗│▘│▙│▚│▛│▜│▝│▞│▟│
252             # └────────┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┘
253              
254             =head1 DEPENDENCIES
255              
256             L<Error::Pure>,
257             L<Readonly>,
258             L<Text::UnicodeBox>,
259             L<Text::UnicodeBox::Control>,
260             L<Unicode::Block>.
261              
262             =head1 REPOSITORY
263              
264             L<https://github.com/tupinek/Unicode-Block-Ascii>
265              
266             =head1 AUTHOR
267              
268             Michal Å paček L<mailto:skim@cpan.org>
269              
270             L<http://skim.cz>
271              
272             =head1 LICENSE AND COPYRIGHT
273              
274             BSD license.
275              
276             =head1 VERSION
277              
278             0.01
279              
280             =cut