| 1 | package DBIx::dbMan::Extension::SQLOutputTable; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use base 'DBIx::dbMan::Extension'; | ||
| 5 | use utf8; | ||
| 6 | use Text::FormatTable; | ||
| 7 | use Term::ANSIColor; | ||
| 8 | |||
| 9 | our $VERSION = '0.05'; | ||
| 10 | |||
| 11 | 1; | ||
| 12 | |||
| 13 | sub IDENTIFICATION { return "000001-000026-000005"; } | ||
| 14 | |||
| 15 | sub preference { return -25; } | ||
| 16 | |||
| 17 | sub known_actions { return [ qw/SQL_OUTPUT/ ]; } | ||
| 18 | |||
| 19 | sub init { | ||
| 20 | my $obj = shift; | ||
| 21 | $obj->{-mempool}->register('output_format','table'); | ||
| 22 | $obj->{-mempool}->register('output_format','colortable'); | ||
| 23 | $obj->{-mempool}->set('output_format',$obj->{-config}->output_format || 'table') unless $obj->{-mempool}->get('output_format'); | ||
| 24 | } | ||
| 25 | |||
| 26 | sub done { | ||
| 27 | my $obj = shift; | ||
| 28 | $obj->{-mempool}->deregister('output_format','table'); | ||
| 29 | $obj->{-mempool}->deregister('output_format','colortable'); | ||
| 30 | |||
| 31 | if ( $obj->{-mempool}->get('output_format') =~ /^(color)?table$/ ) { | ||
| 32 | my @all_formats = $obj->{-mempool}->get_register('output_format'); | ||
| 33 | $obj->{-mempool}->set('output_format',scalar @all_formats ? $all_formats[0] : ''); | ||
| 34 | } | ||
| 35 | } | ||
| 36 | |||
| 37 | sub handle_action { | ||
| 38 | my ($obj,%action) = @_; | ||
| 39 | |||
| 40 | $action{processed} = 1; | ||
| 41 | if ($action{action} eq 'SQL_OUTPUT') { # table is standard fallback | ||
| 42 | my $colorized = $obj->{-mempool}->get('output_format') eq 'colortable'; | ||
| 43 | |||
| 44 | # $action{fieldtypes} - formatting ? | ||
| 45 | my $table = new Text::FormatTable ('|'.( 'l|' x scalar @{$action{fieldnames}} )); | ||
| 46 | $table->rule; | ||
| 47 | $table->head( map { $colorized ? color( $obj->{-config}->tablecolor_head || 'bright_yellow' ) . $_ . color( $obj->{-config}->tablecolor_lines || 'reset' ) : $_; } @{$action{fieldnames}} ); | ||
| 48 | $table->rule; | ||
| 49 | use Data::Dumper; | ||
| 50 | for (@{$action{result}}) { | ||
| 51 | $table->row( map { | ||
| 52 | my $r = $_; | ||
| 53 | $r =~ s/(\S+)/color( $obj->{-config}->tablecolor_content || 'bright_white' ) . $1 . color( $obj->{-config}->tablecolor_lines || 'reset' )/seg if $colorized; | ||
| 54 | $r; | ||
| 55 | } @$_ ); | ||
| 56 | } | ||
| 57 | $table->rule; | ||
| 58 | $action{action} = 'OUTPUT'; | ||
| 59 | $action{output} = ( $colorized ? color( $obj->{-config}->tablecolor_lines || 'reset' ) : '' ) | ||
| 60 | . $table->render( $obj->{-interface}->render_size ) . ( $colorized ? color( 'reset' ) : '' ); | ||
| 61 | delete $action{processed}; | ||
| 62 | } | ||
| 63 | |||
| 64 | return %action; | ||
| 65 | } |