| 1 | package FirePHP::SimpleTable; | ||
| 2 | |||
| 3 | =pod | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | FirePHP::SimpleTable | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | use FirePHP::SimpleTable; | ||
| 12 | my $foo = FirePHP::SimpleTable->new(); | ||
| 13 | |||
| 14 | =head1 DESCRIPTION | ||
| 15 | |||
| 16 | B<FirePHP::SimpleTable> is a sub-class of L<Text::SimpleTable> that | ||
| 17 | can be used to write a table to the FirePHP console | ||
| 18 | |||
| 19 | =cut | ||
| 20 | |||
| 21 | use strict; | ||
| 22 | use warnings; | ||
| 23 | |||
| 24 | use base qw/Text::SimpleTable/; | ||
| 25 | |||
| 26 | use Carp; | ||
| 27 | use Scalar::Util qw/looks_like_number blessed/; | ||
| 28 | |||
| 29 | =head1 METHODS | ||
| 30 | |||
| 31 | =head2 $self->draw | ||
| 32 | |||
| 33 | Returns an array ref suitable for use with L<FirePHP::Dispatcher>'s | ||
| 34 | table method | ||
| 35 | |||
| 36 | =cut | ||
| 37 | |||
| 38 | sub draw { | ||
| 39 | my $self = shift; | ||
| 40 | return unless $self->{columns}; | ||
| 41 | my $rows = @{ $self->{columns}->[0]->[1] } - 1; | ||
| 42 | my $columns = @{ $self->{columns} } - 1; | ||
| 43 | |||
| 44 | my $title = 0; | ||
| 45 | for my $column ( @{ $self->{columns} } ) { | ||
| 46 | $title = @{ $column->[2] } if $title < @{ $column->[2] }; | ||
| 47 | } | ||
| 48 | |||
| 49 | my @result = ( \ my @header ); | ||
| 50 | if ( $title ) { | ||
| 51 | for my $j ( 0 .. $columns ) { | ||
| 52 | push @header, join( | ||
| 53 | "\n", @{$self->{columns}->[$j]->[2]}[ 0 .. $title - 1 ] | ||
| 54 | ) || ''; | ||
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | # Rows | ||
| 59 | for my $i ( 0 .. $rows ) { | ||
| 60 | push @result, \ my @row; | ||
| 61 | for my $j ( 0 .. $columns ) { | ||
| 62 | my $column = $self->{columns}->[$j]; | ||
| 63 | my $text = ( defined $column->[1]->[$i] ) ? $column->[1]->[$i] : ''; | ||
| 64 | push @row, $text; | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | return \@result; | ||
| 69 | } | ||
| 70 | |||
| 71 | sub _wrap { | ||
| 72 | my ( $self, $text, $width ) = @_; | ||
| 73 | return [ $text ]; | ||
| 74 | } | ||
| 75 | |||
| 76 | 1; | ||
| 77 | |||
| 78 | __END__ |