blib/lib/Text/Padding.pm

filter: Statementstmt 100% 37 / 37 Branchbran 70.0% 7 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 95.0% 57 / 60 CC 6 SCAR 7.3
CC
6
Cov
94%
center
4.0
left
2.0
right
2.0
CRAP 2.1
↑ Summary
1 #
2 # This file is part of Text-Padding
3 #
4 # This software is copyright (c) 2011 by Jerome Quelin.
5 #
6 # This is free software; you can redistribute it and/or modify it under
7 # the same terms as the Perl 5 programming language system itself.
8 #
9 2 ▶use 5.012;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 package Text::Padding;
14 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 $Text::Padding::VERSION = '1.110170';
16 }
17 # ABSTRACT: simple way of formatting a text
18  
19 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use MooseX::Has::Sugar;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use Text::Truncate;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23  
24 # -- public attributes
25  
26  
27 2 has ellipsis => ( rw, isa=>"Str", default=>"\x{2026}" );
28  
29  
30 # -- public methods
31  
32  
33 4 ▶sub center {
Subroutine center: called CC=4 SCAR=13.9
Pod documented
34 4 my ($self, $text, $maxlength) = @_;
35 4 ▶ return " " x $maxlength if length($text) == 0; # empty string
Branch: 1 branch
BranchTrueFalseTotal
if length $text == 0 0 4 4
36  
37 # don't fill more than what's required
38 4 $text = truncstr( $text, $maxlength, $self->ellipsis );
39  
40 4 my $diff = $maxlength - length($text);
41 4 ▶ return $text if $diff == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $diff == 0 2 2 4
42 2 my $pad = " " x ($diff/2);
43 2 $text = $pad . $text . $pad;
44 2 ▶ $text .= " " if $diff % 2;
Branch: 1 branch
BranchTrueFalseTotal
if $diff % 2 1 1 2
45 2 return $text;
46 }
47  
48  
49  
50 3 ▶sub left {
Subroutine left: called CC=2 SCAR=7.0
Pod documented
51 3 my ($self, $text, $maxlength) = @_;
52 3 ▶ return " " x $maxlength if length($text) == 0; # empty string
Branch: 1 branch
BranchTrueFalseTotal
if length $text == 0 0 3 3
53  
54 # truncate and left align
55 3 $text = truncstr( $text, $maxlength, $self->ellipsis );
56 3 return sprintf "%-${maxlength}s", $text;
57 }
58  
59  
60  
61 3 ▶sub right {
Subroutine right: called CC=2 SCAR=7.0
Pod documented
62 3 my ($self, $text, $maxlength) = @_;
63 3 ▶ return " " x $maxlength if length($text) == 0; # empty string
Branch: 1 branch
BranchTrueFalseTotal
if length $text == 0 0 3 3
64  
65 # truncate and right align
66 3 $text = truncstr( $text, $maxlength, $self->ellipsis );
67 3 return sprintf "%${maxlength}s", $text;
68 }
69  
70  
71 2 1;
72  
73  
74 =pod
75  
76 =head1 NAME
77  
78 Text::Padding - simple way of formatting a text
79  
80 =head1 VERSION
81  
82 version 1.110170
83  
84 =head1 SYNOPSIS
85  
86 my $pad = Text::Padding->new;
87 my $string = 'foo bar baz';
88 my $left = $pad->left ( $string, 20 );
89 my $centered = $pad->center( $string, 20 );
90 my $right = $pad->right ( $string, 20 );
91  
92 =head1 DESCRIPTION
93  
94 This module provides a simple way to align a text on the left, right or
95 center. If left & right are easy to achieve (see C<sprintf()>), i found
96 no cpan module that achieved a simple text centering. Well, of course,
97 L<Perl6::Form> provides it, but it's incredibly slow during startup /
98 destroy time. And L<Text::Reform> was segfaulting during destroy time.
99  
100 Hence this module, which aims to provide only those 3 methods.
101  
102 =head1 ATTRIBUTES
103  
104 =head2 ellipsis
105  
106 When a string is too long to fit the wanted length, the methods are
107 truncating it. To indicate that the string has been choped, the last
108 character is replaced by an ellipsis (\x{2026} by default). However,
109 it's possible to change this character by whatever ones wants: empty
110 string to disable this behaviour, multi-char string is supported, etc.
111 See L<Text::Truncate> for more information.
112  
113 =head1 METHODS
114  
115 =head2 center
116  
117 my $centered = $pad->center( $str, $length );
118  
119 Return a C<$length>-long string where C<$str> is centered, using white
120 spaces on left & right. C<$str> is truncated if too long to fit (see the
121 C<ellipsis> attribute).
122  
123 =head2 left
124  
125 my $left = $pad->left( $str, $length );
126  
127 Return a C<$length>-long string where C<$str> is left-aligned, right
128 being padded with white spaces. C<$str> is truncated if too long to fit
129 (see the C<ellipsis> attribute).
130  
131 =head2 right
132  
133 my $right = $pad->right( $str, $length );
134  
135 Return a C<$length>-long string where C<$str> is right-aligned, left
136 being padded with white spaces. C<$str> is truncated if too long to fit
137 (see the C<ellipsis> attribute).
138  
139 =head1 SEE ALSO
140  
141 L<Text::Reform>, L<Perl6::Form>, L<Text::Truncate>.
142  
143 You can also look for information on this module at:
144  
145 =over 4
146  
147 =item * AnnoCPAN: Annotated CPAN documentation
148  
149 L<http://annocpan.org/dist/Text-Padding>
150  
151 =item * CPAN Ratings
152  
153 L<http://cpanratings.perl.org/d/Text-Padding>
154  
155 =item * Open bugs
156  
157 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Text-Padding>
158  
159 =item * Git repository
160  
161 L<http://github.com/jquelin/text-padding.git>.
162  
163 =back
164  
165 =head1 AUTHOR
166  
167 Jerome Quelin
168  
169 =head1 COPYRIGHT AND LICENSE
170  
171 This software is copyright (c) 2011 by Jerome Quelin.
172  
173 This is free software; you can redistribute it and/or modify it under
174 the same terms as the Perl 5 programming language system itself.
175  
176 =cut
177  
178  
179 __END__
↑ Summary