App-DBBrowser
view release on metacpan or search on metacpan
lib/App/DBBrowser/GetContent/Filter.pm view on Meta::CPAN
package # hide from PAUSE
App::DBBrowser::GetContent::Filter;
use warnings;
use strict;
use 5.016;
use List::MoreUtils qw( any uniq );
use Term::Choose qw();
use Term::Choose::Constants qw( EXTRA_W );
use Term::Choose::LineFold qw( line_fold print_columns );
use Term::Choose::Util qw( insert_sep get_term_width get_term_height unicode_sprintf );
use Term::Choose::Screen qw( clear_screen );
use Term::Form qw();
use Term::Form::ReadLine qw();
use App::DBBrowser::Auxil;
sub new {
my ( $class, $info, $options, $d ) = @_;
my $sf = {
i => $info,
o => $options,
d => $d
};
bless $sf, $class;
}
sub input_filter {
my ( $sf, $sql ) = @_;
my $tc = Term::Choose->new( $sf->{i}{tc_default} );
my $confirm = ' OK';
my $back = ' ' . $sf->{i}{s_back};
my $reset = ' ' . $sf->{i}{reset};
my $reparse = ' Reparse';
my $merge_rows = 'Merge_Rows';
my $range_rows = 'Range_Rows';
my $row_groups = 'Row_Groups';
my $empty_rows = 'Empty_Rows';
my $choose_rows = 'Choose_Rows';
my $remove_cell = 'Remove_Cell';
my $insert_cell = 'Insert_Cell';
my $split_table = 'Split_Table';
my $split_column = 'Split_Column';
my $join_columns = 'Join_Columns';
my $fill_up_rows = 'Fill_up_Rows';
my $empty_to_null = ' Empty_2_null';
my $empty_cols = 'Empty_Columns';
my $choose_cols = 'Choose_Columns';
my $append_col = 'Append_Columns';
my $cols_to_rows = 'Columns_to_Rows';
my $convert_date = 'Convert_DateTime';
my $s_and_replace = 'Search_&_Replace';
my $field_count = @{$sql->{insert_args}} * @{$sql->{insert_args}[0]};
my $bu_insert_args = [ map { [ @$_ ] } @{$sql->{insert_args}} ]; # copy the entire data
$sf->{empty_to_null} = $sf->{o}{insert}{empty_to_null_file};
my $working = $field_count > 500_000 ? 'Working ... ' : undef;
my $old_idx = 0;
FILTER: while ( 1 ) {
my $skip = ' ';
my $menu = [
undef, $choose_rows, $range_rows, $row_groups,
$confirm, $choose_cols, $empty_rows, $empty_cols,
$reset, $s_and_replace, $convert_date, $skip,
$reparse, $remove_cell, $insert_cell, $skip,
$empty_to_null, $join_columns, $split_column, $append_col,
$cols_to_rows, $split_table, $merge_rows, $fill_up_rows,
];
my $max_cols = 4;
lib/App/DBBrowser/GetContent/Filter.pm view on Meta::CPAN
$digits,
{ info => $info, cs_label => 'Number columns new table: ', small_first => 1,
confirm => $sf->{i}{confirm}, back => $sf->{i}{back} }
);
$sf->__print_busy_string( $working );
if ( ! $col_count ) {
return;
}
if ( @{$aoa->[0]} < $col_count ) {
my $prompt = sprintf 'Chosen number(%d) bigger than the available columns(%d)!', $col_count, scalar( @{$aoa->[0]} );
my $info = $sf->__get_filter_info( $sql, $filter_str );
$tc->choose(
[ 'Continue with ENTER' ],
{ info => $info, prompt => $prompt }
);
$sf->__print_busy_string( $working );
next CHOOSE_A_NUMBER;
}
if ( @{$aoa->[0]} % $col_count ) {
my $prompt = sprintf 'The number of available columns(%d) cannot be divided by the selected number(%d) without remainder!', scalar( @{$aoa->[0]} ), $col_count;
my $info = $sf->__get_filter_info( $sql, $filter_str );
$tc->choose(
[ 'Continue with ENTER' ],
{ info => $info, prompt => $prompt }
);
$sf->__print_busy_string( $working );
next CHOOSE_A_NUMBER;
}
last CHOOSE_A_NUMBER;
}
my $begin = 0;
my $end = $col_count - 1;
my $tmp = [];
while ( 1 ) {
for my $row ( @$aoa ) {
push @$tmp, [ @{$row}[ $begin .. $end ] ];
}
$begin = $end + 1;
if ( $begin > $#{$aoa->[0]} ) {
last;
}
$end = $end + $col_count;
}
$sql->{insert_args} = $tmp;
}
sub __merge_rows {
my ( $sf, $sql, $filter_str, $working ) = @_;
my $tu = Term::Choose::Util->new( $sf->{i}{tcu_default} );
my $tf = Term::Form->new( $sf->{i}{tf_default} );
my $aoa = $sql->{insert_args};
my $term_w = get_term_width() + EXTRA_W;
my $stringified_rows;
{
no warnings 'uninitialized';
@$stringified_rows = map {
my $str_row = join( ',', @$_ );
if ( print_columns( $str_row ) > $term_w ) {
$str_row = unicode_sprintf( $str_row, $term_w, { suffix_on_truncate => $sf->{i}{dots} } );
}
else {
$str_row;
}
} @$aoa;
}
my $prompt = 'Choose rows:';
my $info = $filter_str;
# Choose
my $chosen_idxs = $tu->choose_a_subset(
$stringified_rows,
{ cs_separator => "\n", cs_end => "\n", layout => 2, order => 0, all_by_default => 0, prompt => $prompt,
index => 1, info => $info, busy_string => $working }
);
$sf->__print_busy_string( $working );
if ( ! defined $chosen_idxs || ! @$chosen_idxs ) {
return;
}
my $merged = [];
for my $col ( 0 .. $#{$aoa->[$chosen_idxs->[0]]} ) {
my @tmp;
for my $row ( @$chosen_idxs ) {
next if ! defined $aoa->[$row][$col];
next if $aoa->[$row][$col] =~ /^\s*\z/;
$aoa->[$row][$col] =~ s/^\s+|\s+\z//g;
push @tmp, $aoa->[$row][$col];
}
$merged->[$col] = join ' ', @tmp;
}
$prompt = @$chosen_idxs == 1 ? 'Edit row cells:' : 'Edit cells of merged rows:';
my $col_number = 0;
my $fields = [ map { [ ++$col_number, defined $_ ? "$_" : '' ] } @$merged ];
$info = $sf->__get_filter_info( $sql, $filter_str );
# Fill_form
my $form = $tf->fill_form(
$fields,
{ info => $info, prompt => $prompt, confirm => $sf->{i}{confirm},
back => $sf->{i}{back} . ' ' }
);
$sf->__print_busy_string( $working );
if ( ! $form ) {
return;
}
$merged = [ map { $_->[1] } @$form ];
my $first_idx = shift @$chosen_idxs;
$aoa->[$first_idx] = $merged; # modifies $aoa
for my $idx ( sort { $b <=> $a } @$chosen_idxs ) {
splice @$aoa, $idx, 1;
}
$sql->{insert_args} = $aoa;
return;
}
sub __join_columns {
my ( $sf, $sql, $filter_str, $working ) = @_;
my $tu = Term::Choose::Util->new( $sf->{i}{tcu_default} );
my $tf = Term::Form->new( $sf->{i}{tf_default} );
my $tr = Term::Form::ReadLine->new( $sf->{i}{tr_default} );
my $aoa = $sql->{insert_args};
( run in 0.946 second using v1.01-cache-2.11-cpan-39bf76dae61 )