Choose/Opt/Search.pm untested

filter: Statementstmt 0.0% 0 / 123 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 176 CC 20 SCAR 40.8
CC
20
Cov
0%
__search_begin
110.0
__user_input
56.0
__search_end
30.0
CRAP 59.1
1 package Term::Choose::Opt::Search;
2  
3 use warnings;
4 use strict;
5 use 5.10.1;
6  
7 our $VERSION = '1.783';
8  
9 use Term::Choose::Constants qw( ROW COL );
10 use Term::Choose::Screen qw( up clear_to_end_of_screen show_cursor hide_cursor );
11  
12  
13 sub __user_input {
14 my ( $self, $prompt, $error, $default ) = @_;
15 $self->{plugin}->__reset_mode( { mouse => $self->{mouse}, hide_cursor => $self->{hide_cursor} } );
16 if ( $self->{margin_left} ) {
17 $prompt = ( ' ' x $self->{margin_left} ) . $prompt;
18 }
19 my $string;
20 if ( ! eval {
21 require Term::Form::ReadLine;
22 Term::Form::ReadLine->VERSION(0.544);
23 my $term = Term::Form::ReadLine->new();
24 $string = $term->readline(
25 $prompt,
26 { info => $error, default => $default, hide_cursor => 2, clear_screen => length $error ? 1 : 2,
27 color => $self->{color} }
28 );
29 1 }
30 ) {
31 print "\r", clear_to_end_of_screen();
32 if ( length $error ) {
33 print $error, "\n\r";
34 }
35 print show_cursor() if ! $self->{hide_cursor};
36 print $prompt;
37 $string = <STDIN>;
38 print hide_cursor() if ! $self->{hide_cursor};
39 chomp $string;
40 }
41 $self->__init_term();
42 return $string;
43 }
44  
45  
46 sub __search_begin {
47 my ( $self ) = @_;
48 my ($search_regex, $error, $default );
49  
50 USER_INPUT: while ( 1 ) {
51 my $search_str = $self->Term::Choose::Opt::Search::__user_input( '> search-pattern: ', $error, $default );
52 $error = '';
53 if ( ! length $search_str ) {
54 $self->Term::Choose::Opt::Search::__search_end();
55 return;
56 }
57 if ( ! eval {
58 if ( $self->{search} == 1 ) {
59 $search_regex = qr/$search_str/i;
60 $self->{search_info} = 'm/' . $search_str . '/i';
61 }
62 else {
63 $search_regex = qr/$search_str/;
64 $self->{search_info} = 'm/' . $search_str . '/';
65 }
66 'Teststring' =~ $search_regex;
67 1 }
68 ) {
69 $error = $@;
70 $default = $default eq $search_str ? '' : $search_str;
71 next USER_INPUT;
72 }
73 last USER_INPUT;
74 }
75 $self->{map_search_list_index} = [];
76 my $filtered_list = [];
77 for my $i ( 0 .. $#{$self->{list}} ) {
78 if ( $self->{list}[$i] =~ $search_regex ) {
79 push @{$self->{map_search_list_index}}, $i;
80 push @$filtered_list, $self->{list}[$i];
81 }
82 }
83 if ( ! @$filtered_list ) {
84 $filtered_list = [ 'No matches found.' ];
85 $self->{map_search_list_index} = [ 0 ];
86 }
87 $self->{mark} = $self->__marked_rc2idx();
88 $self->{backup_list} = [ @{$self->{list}} ];
89 $self->{list} = $filtered_list;
90 $self->{backup_width_elements} = [ @{$self->{width_elements}} ];
91 $self->{backup_col_width} = $self->{col_width};
92 $self->__length_list_elements();
93 $self->{default} = 0;
94 for my $opt ( qw(meta_items no_spacebar mark) ) {
95 if ( defined $self->{$opt} ) {
96 $self->{'backup_' . $opt} = [ @{$self->{$opt}} ];
97 my $tmp = [];
98 for my $orig_idx ( @{$self->{$opt}} ) {
99 for my $i ( 0 .. $#{$self->{map_search_list_index}} ) {
100 if ( $self->{map_search_list_index}[$i] == $orig_idx ) {
101 push @$tmp, $i;
102 }
103 }
104 }
105 $self->{$opt} = $tmp;
106 }
107 }
108 my $up = $self->{i_row} + $self->{count_pre_rows} + 1; # + 1 => readline
109 print up( $up ) if $up;
110 $self->__wr_first_screen();
111 }
112  
113  
114 sub __search_end {
115 my ( $self ) = @_;
116 $self->{search_info} = '';
117 if ( @{$self->{map_search_list_index}||[]} ) {
118 my $curr_idx = $self->{rc2idx}[ $self->{pos}[ROW] ][ $self->{pos}[COL] ];
119 $self->{default} = $self->{map_search_list_index}[$curr_idx];
120 $self->{mark} = $self->__marked_rc2idx();
121 my $tmp_mark = [];
122 for my $i ( @{$self->{mark}} ) {
123 push @$tmp_mark, $self->{map_search_list_index}[$i];
124 }
125 my %seen;
126 $self->{mark} = [ grep !$seen{$_}++, @$tmp_mark, defined $self->{backup_mark} ? @{$self->{backup_mark}} : () ];
127 }
128 delete $self->{map_search_list_index};
129 delete $self->{backup_mark};
130 for my $key ( qw(list width_elements col_width meta_items no_spacebar) ) {
131 my $key_backup = 'backup_' . $key;
132 $self->{$key} = $self->{$key_backup} if defined $self->{$key_backup};
133 delete $self->{$key_backup};
134 }
135 my $up = $self->{i_row} + $self->{count_pre_rows};
136 print up( $up ) if $up;
137 print "\r" . clear_to_end_of_screen();
138 $self->__wr_first_screen();
139 }
140  
141  
142  
143  
144  
145  
146  
147 1;
148  
149 __END__