summaryrefslogtreecommitdiff
path: root/lib/Text/SimpleTable/AutoWidth.pm
blob: 3287f0406666a80b9382bf66e4c789b5941733cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
use strict;
use warnings;

package Text::SimpleTable::AutoWidth;
$Text::SimpleTable::AutoWidth::VERSION = '0.09';
use Moo;

# ABSTRACT: Text::SimpleTable::AutoWidth - Simple eyecandy ASCII tables with auto-width selection


has 'fixed_width' => ( is => 'rw', default => 0 );  # isa => 'Int'
has 'max_width'   => ( is => 'rw', default => 0 );  # isa => 'Int'

has 'captions' => ( is => 'rw' );                   # isa => 'ArrayRef[Str]'
has 'rows'     => ( is => 'rw' );                   # isa => 'ArrayRef[ArrayRef[Str]]'

our $WIDTH_LIMIT = 200;    # default maximum width


sub row {
    my ( $self, @texts ) = @_;

    if ( $self->rows ) {
        push( @{ $self->rows }, [@texts] );
    }
    else {
        $self->rows( [ [@texts] ] );
    }

    return $self;
}


use List::Util;
use Text::SimpleTable;

sub draw {
    my $self = shift;

    # count of columns will be same as count of captions, or same
    # as count of columns in first row, if there is no captions
    my $columns =
         ( $self->captions && @{ $self->captions } )
      || ( $self->rows && @{ $self->rows->[0] } )
      || 0;

    return unless $columns;

    # table will not be wider than limits
    my $limit =
        $self->max_width   ? $self->max_width
      : $self->fixed_width ? $self->fixed_width
      :                      $WIDTH_LIMIT;

    # by default, each column should have at least 2 symbols:
    # one informative and one for '-' (if we'll need to wrap)
    my @max_width = (2) x $columns;

    # calculate max width of each column
    for my $row ( ( $self->captions ? $self->captions : () ), @{ $self->rows } ) {
        my @row_width = map { length } @$row;
        $#row_width = $columns - 1 if $#row_width >= $columns;

        # find new width
        # we will do this in two passes
        my @new_width = @max_width;

        # first pass:
        # find new width for all columns, that we can
        # make wider without need to wrap anything
        for my $idx ( 0 .. $#row_width ) {
            if ( $max_width[$idx] < $row_width[$idx] ) {
                $new_width[$idx] = $row_width[$idx];

                # check for limits
                my $total = $columns + 1    # for each '|'
                  + $columns * 2            # for spaces around each value
                  + List::Util::reduce { $a + $b } @new_width;

                # restore old value, if new value will lead to wrap
                $new_width[$idx] = $max_width[$idx]
                  if $total > $limit;
            }
        }

        # second pass:
        # find new width for all columns, that we can
        # make wider and need to wrap something
        for my $idx ( 0 .. $#row_width ) {
            if ( $new_width[$idx] < $row_width[$idx] ) {
                my $total = $columns + 1    # for each '|'
                  + $columns * 2            # for spaces around each value
                  + List::Util::reduce { $a + $b } @new_width;

                $new_width[$idx] += $limit - $total;
                last;
            }
        }

        # save new result
        @max_width = @new_width;

        # check for limits
        my $total = $columns + 1            # for each '|'
          + $columns * 2                    # for spaces around each value
          + List::Util::reduce { $a + $b } @max_width;

        last if $total >= $limit;
    }

    # check for fixed_width
    if ( $self->fixed_width ) {
        my $total = $columns + 1            # for each '|'
          + $columns * 2                    # for spaces around each value
          + List::Util::reduce { $a + $b } @max_width;

        $max_width[-1] += $self->fixed_width - $total
          unless $total == $self->fixed_width;
    }

    # prepare drawer
    my @params = @max_width;

    if ( $self->captions ) {
        my $idx = 0;
        for (@params) {
            $_ = [ $_, $self->captions->[ $idx++ ] ];
        }
    }
    my $tab = Text::SimpleTable->new(@params);

    # put rows into drawer
    $tab->row(@$_) for @{ $self->rows };

    return $tab->draw();
}

__PACKAGE__->meta->make_immutable();


1;    # End of Text::SimpleTable::AutoWidth

__END__

=pod

=encoding UTF-8

=head1 NAME

Text::SimpleTable::AutoWidth - Text::SimpleTable::AutoWidth - Simple eyecandy ASCII tables with auto-width selection

=head1 VERSION

version 0.09

=head1 SYNOPSIS

    use Text::SimpleTable::AutoWidth;

    my $t1 = Text::SimpleTable::AutoWidth->new();
    $t1->row( 'foobarbaz', 'yadayadayada' );
    print $t1->draw;

    .-----------+--------------.
    | foobarbaz | yadayadayada |
    '-----------+--------------'


    my $t2 = Text::SimpleTable::AutoWidth->new();
    $t2->captions( 'Foo', 'Bar' );
    $t2->row( 'foobarbaz', 'yadayadayada' );
    $t2->row( 'barbarbarbarbar', 'yada' );
    print $t2->draw;

    .-----------------+--------------.
    | Foo             | Bar          |
    +-----------------+--------------+
    | foobarbaz       | yadayadayada |
    | barbarbarbarbar | yada         |
    '-----------------+--------------'

=head1 DESCRIPTION

Simple eyecandy ASCII tables with auto-selection columns width,
as seen in L<Catalyst>.

=head1 METHODS

=head2 new(@attrs)

Inherited constructor from Moo.
You can set following attributes:

=head3 fixed_width

Set fixed width for resulting table. By default it's 0,
that's mean "don't fix width", so width of result table
will depend on input data.

Be warned, that fixed_width will include not only width of your data,
but also all surronding characters, like spaces across values,
table drawings (like '|') and hypen (if wrapping is needed).

=head3 max_width

Set maximum width for resulting table. By default it's 0,
that's mean "use default value". Default value is stored in
$Text::SimpleTable::AutoWidth::WIDTH_LIMIT, and can be changed
at any moment. Default value for WIDTH_LIMIT is 200.

Be warned, that max_width will include not only width of your data,
but also all surronding characters, like spaces across values,
table drawings (like '|') and hypen (if wrapping is needed).

NB: if you set fixed_width and max_width at same time, then you'll
get table with fixed width, but not wider than max_width characters.

=head3 captions

ArrayRef[Str] for captions in resulting table.

=head3 rows

ArrayRef[ArrayRef[Str]] for values in each row.
You can use next method to add individual rows into table.

=head2 row(@texts)

Add new row to table. Return $self, so you can write something like this:

    print Text::SimpleTable::AutoWidth
        ->new( max_width => 55, captions => [qw/ Name Age /] )
        ->row( 'Mother', 59 )
        ->row( 'Dad', 58 )
        ->row( 'me', 32 )
        ->draw();

=head2 draw()

Draw table. Really, just calculate column width, and then call Text::SimpleTable->draw().

=head1 GIT REPOSITORY

git clone git://github.com/cub-uanic/Text-SimpleTable-AutoWidth.git

=head1 SEE ALSO

L<Text::SimpleTable>, L<Moo>, L<Catalyst>

=head1 AUTHOR

Oleg Kostyuk, C<< <cub#cpan.org> >>

=head1 COPYRIGHT & LICENSE

Copyright by Oleg Kostyuk.

This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.

=head1 AUTHOR

Oleg Kostyuk <cub.uanic@gmail.com>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2015 by Oleg Kostyuk.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut