~ubuntu-branches/ubuntu/trusty/libtext-aligner-perl/trusty

« back to all changes in this revision

Viewing changes to lib/Text/Aligner.pm

  • Committer: Bazaar Package Importer
  • Author(s): Zed Pobre
  • Date: 2011-06-10 12:26:48 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20110610122648-exkawt5o0xre4w5v
Tags: 0.07-1
* New upstream version
  * Color support added
* Standards-Version: 3.9.2
* Switch from CDBS to DH

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Text::Aligner - Align text in columns
2
2
package Text::Aligner;
3
3
use strict;
4
 
 
5
4
use warnings;
6
5
 
7
6
BEGIN    {
8
7
    use Exporter ();
9
8
    use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
10
 
    $VERSION     = 0.03;
 
9
    $VERSION     = '0.07';
11
10
    @ISA         = qw (Exporter);
12
 
    #Give a hoot don't pollute, do not export more than needed by default
13
11
    @EXPORT      = qw ();
14
12
    @EXPORT_OK   = qw ( align);
15
13
    %EXPORT_TAGS = ();
71
69
    ( $p, $w - $p);
72
70
}
73
71
 
 
72
use Term::ANSIColor;
 
73
*colorstrip = \ &Term::ANSIColor::colorstrip;
 
74
# early versions of Term::ANSIColor don't have colorstrip
 
75
defined &colorstrip or *colorstrip = sub { shift };
 
76
 
74
77
# return left and right field widths for an object
75
78
sub _measure {
76
79
    my $al = shift;
77
80
    my $obj = shift;
78
81
    $obj = '' unless defined $obj;
79
82
    my ( $wmeth, $pmeth) = @{ $al}{ qw( width pos)};
 
83
 
 
84
    # support colorized strings
 
85
    $obj = colorstrip($obj) unless ref $obj;
 
86
 
80
87
    my $w = ref $wmeth ? $wmeth->( $obj) : $obj->$wmeth;
81
88
    my $p = ref $pmeth ? $pmeth->( $obj) : $obj->$pmeth;
82
89
    $_ ||= 0 for $w, $p;
159
166
        my $regex = $_; # lexical copy!
160
167
        $pos = sub {
161
168
            local $_ = shift;
162
 
            m/$regex/ ? $-[ 0] : length; # assume match after string
 
169
            return m/$regex/ ? $-[ 0] : length; # assume match after string
163
170
        };
164
171
    } else {
165
172
        s/^left/0/;
180
187
    ( $width, $pos);
181
188
}
182
189
 
183
 
# decide if a string is a number. (see perlfaq4).  This needs to become
184
 
# more flexible for auto-alignment
185
 
sub _is_number { defined( $_[ 0]) and $_[ 0] =~ /^-?\d+\.?\d*$/ }
 
190
# decide if a string is a number. (see perlfaq4).
 
191
sub _is_number {
 
192
    my ($x) = @_;
 
193
    return 0 unless defined $x;
 
194
    return 0 if $x !~ /\d/;
 
195
    return 1 if $x =~ /^-?\d+\.?\d*$/;
 
196
    $x = colorstrip($x);
 
197
    $x =~ /^-?\d+\.?\d*$/
 
198
}
186
199
 
187
200
package Text::Aligner::Auto;
188
201
# Combined numeric and left alignment.  Numbers are aligned numerically,
309
322
An undefined argument is interpreted as an empty string without
310
323
complaint.
311
324
 
 
325
Alignment respects colorizing escape sequences a la Term::ANSICOLOR,
 
326
which means it knows that thses sequences don't take up space on
 
327
the screen.
 
328
 
312
329
=head1 ALIGNMENT
313
330
 
314
331
The first argument of the align() function is an alignment style, a
332
349
may be the right place.  A string-only column ends up right-aligned
333
350
(unless there are points present).
334
351
 
335
 
The "auto" style seperates numeric strings (that are composed of
 
352
The "auto" style separates numeric strings (that are composed of
336
353
"-", ".", and digits in the usual manner) and aligns them numerically.
337
354
Other strings are left aligned with the number that sticks out
338
355
farthest to the left.  This gives left alignment for string-only
402
419
 
403
420
    Anno Siegel
404
421
    CPAN ID: ANNO
405
 
    siegel@zrz.tu-berlin.de
406
 
    http://www.tu-berlin.de/~siegel
407
422
 
408
423
=head1 COPYRIGHT
409
424