forked from vit-project/vit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.pl
159 lines (152 loc) · 3.95 KB
/
search.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# Copyright 2012 - 2013, Steve Rader
# Copyright 2013 - 2016, Scott Kostyshak
sub start_search {
my $ch = $_[0];
if ( $search_direction == 1 ) {
$search_pat = '/';
} else {
$search_pat = '?';
}
&draw_prompt($search_pat);
echo();
curs_set(1);
$cur_pos = 1;
GETCH: while (1) {
my $ch = prompt_u8getch();
if ( $ch eq "\ch" || $ch eq KEY_BACKSPACE ) {
if ( $cur_pos > 1 ) {
$cur_pos--;
substr($search_pat, $cur_pos, 1, "");
}
&draw_prompt_cur($search_pat);
next GETCH;
}
if ( $ch eq "\cu" ) {
my $search_ch;
if ( $search_direction == 1 ) {
$search_ch = '/';
} else {
$search_ch = '?';
}
$search_pat = $search_ch.substr($search_pat, $cur_pos);
$cur_pos = 1;
&draw_prompt_cur($search_pat);
next GETCH;
}
if ( $ch eq "\e" || $ch eq "\cg" ) {
&draw_prompt('');
noecho();
curs_set(0);
return;
}
if ( $ch eq "\n" ) {
last GETCH;
}
if ( $ch eq KEY_LEFT ) {
if ( $cur_pos > 1 ) {
$cur_pos--;
}
&draw_prompt_cur($search_pat);
next GETCH;
}
if ( $ch eq KEY_RIGHT ) {
if ( $cur_pos < length($search_pat) ) {
$cur_pos++;
}
&draw_prompt_cur($search_pat);
next GETCH;
}
if ( &is_printable($ch) ) {
substr($search_pat, $cur_pos, 0, $ch);
$cur_pos = $cur_pos + 1;
}
&draw_prompt_cur($search_pat);
}
noecho();
curs_set(0);
$search_pat = substr($search_pat, 1);
if ( $search_pat eq '' ) {
$search_pat = '';
&draw_prompt('');
beep();
return;
}
$refresh_needed = 1;
if ( ! &do_search('n') ) {
return;
}
$input_mode = 'search';
return;
}
#------------------------------------------------------------------
sub do_search {
my $ch = $_[0];
my $rtn = &do_inner_search($ch);
if ( $rtn == 1 ) {
if ( $task_selected_idx - $display_start_idx >= $REPORT_LINES ) {
$display_start_idx = $task_selected_idx - $REPORT_LINES + 1;
} elsif ( $task_selected_idx < $display_start_idx ) {
$display_start_idx = $task_selected_idx;
}
return 1;
} else {
$error_msg = "Pattern not found: $search_pat";
beep();
return 0;
}
return 0;
}
#------------------------------------------------------------------
sub do_inner_search {
my $ch = $_[0];
$during_try = 1;
my $search_pat_run;
try {
"dummytext" =~ /$search_pat/i;
$search_pat_run = $search_pat;
} catch {
# print search_pat but run search_pat_run so the user sees original input. e.g.
# search_pat = "abc\"
# search_pat_actual = "abc\\"
$search_pat_run = quotemeta($search_pat);
};
$during_try = 0;
if ( $search_direction == 1 && $ch eq 'n' || $search_direction == 0 && $ch eq 'N' ) {
for ( my $i = $task_selected_idx + 1; $i <= $#report_lines; $i++ ) {
if ( $report_lines[$i] =~ /$search_pat_run/i ) {
$task_selected_idx = $i;
return 1;
}
}
&draw_prompt('Search hit BOTTOM, continuing at TOP');
usleep($error_delay);
for ( my $i = 0; $i < $task_selected_idx; $i++ ) {
if ( $report_lines[$i] =~ /$search_pat_run/i ) {
$task_selected_idx = $i;
return 1;
}
}
if ( $report_lines[$task_selected_idx] =~ /$search_pat_run/i ) { return 1; }
return 0;
}
if ( $search_direction == 1 && $ch eq 'N' || $search_direction == 0 && $ch eq 'n' ) {
for ( my $i = $task_selected_idx - 1; $i >= 0; $i-- ) {
if ( $report_lines[$i] =~ /$search_pat_run/i ) {
$task_selected_idx = $i;
return 1;
}
}
&draw_prompt('Search hit TOP, continuing at BOTTOM');
usleep($error_delay);
for ( my $i = $#report_lines; $i > $task_selected_idx; $i-- ) {
if ( $report_lines[$i] =~ /$search_pat_run/i ) {
$task_selected_idx = $i;
return 1;
}
}
if ( $report_lines[$task_selected_idx] =~ /$search_pat_run/i ) { return 1; }
return 0;
}
return -1;
}
return 1;