| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Teng::Plugin::TextTable; | 
| 2 | 1 |  |  | 1 |  | 25087 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 31 |  | 
| 3 | 1 |  |  | 1 |  | 4 | use warnings; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 27 |  | 
| 4 | 1 |  |  | 1 |  | 25 | use 5.010001; | 
|  | 1 |  |  |  |  | 7 |  | 
|  | 1 |  |  |  |  | 57 |  | 
| 5 |  |  |  |  |  |  | our $VERSION = '0.03'; | 
| 6 | 1 |  |  | 1 |  | 948 | use Text::SimpleTable; | 
|  | 1 |  |  |  |  | 1976 |  | 
|  | 1 |  |  |  |  | 49 |  | 
| 7 | 1 |  |  | 1 |  | 6 | use Carp (); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 20 |  | 
| 8 | 1 |  |  | 1 |  | 7 | use List::Util (); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 288 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | our @EXPORT = qw/draw_text_table/; | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | sub draw_text_table { | 
| 13 | 0 |  |  | 0 | 0 |  | my ($self, $table_name, $where, $opts, $cols) = @_; | 
| 14 | 0 | 0 |  |  |  |  | my $table = $self->schema->get_table($table_name) | 
| 15 |  |  |  |  |  |  | or Carp::croak("Unknown table: $table_name"); | 
| 16 | 0 | 0 |  |  |  |  | unless ($cols) { | 
| 17 | 0 |  |  |  |  |  | $cols = $table->columns(); | 
| 18 |  |  |  |  |  |  | } | 
| 19 |  |  |  |  |  |  |  | 
| 20 | 0 |  |  |  |  |  | my $iter = $self->search($table_name, $where, $opts); | 
| 21 | 0 |  |  |  |  |  | $iter->suppress_object_creation(1); | 
| 22 | 0 |  |  |  |  |  | my @rows = $iter->all; | 
| 23 |  |  |  |  |  |  |  | 
| 24 | 0 |  | 0 |  |  |  | my @headers = map { [length($_) || 1, $_] } @$cols; | 
|  | 0 |  |  |  |  |  |  | 
| 25 | 0 |  |  |  |  |  | for my $i (0..@$cols-1) { | 
| 26 | 0 |  |  |  |  |  | for my $row (@rows) { | 
| 27 | 0 |  | 0 |  |  |  | $headers[$i]->[0] = List::Util::max($headers[$i]->[0], length($row->{$cols->[$i]}) || 1); | 
| 28 |  |  |  |  |  |  | } | 
| 29 |  |  |  |  |  |  | } | 
| 30 | 0 |  |  |  |  |  | my $tt = Text::SimpleTable->new(@headers); | 
| 31 | 0 |  |  |  |  |  | for my $row (@rows) { | 
| 32 | 0 | 0 |  |  |  |  | $tt->row(map { $row->{$_} || '' } @$cols); | 
|  | 0 |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | } | 
| 34 | 0 |  |  |  |  |  | return $tt->draw; | 
| 35 |  |  |  |  |  |  | } | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | 1; | 
| 39 |  |  |  |  |  |  | __END__ |