forked from vit-project/vit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprompt.pl
335 lines (315 loc) · 9.56 KB
/
prompt.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# Copyright 2012 - 2013, Steve Rader
# Copyright 2013 - 2016, Scott Kostyshak
sub prompt_y {
my $ch = &prompt_chr(@_);
my $ans = 0;
if ( $ch eq "y" || $ch eq "Y" ) { $ans = 1; }
return $ans;
}
#------------------------------------------------------------------
# The following function is, with minor modifications, from
# https://cpan.rt.develooper.com/Public/Bug/Display.html?id=27335
# TODO:
# Is there a normal Perl way of doing the following more simple?
# - [BaZo] I think modern Curses can handle multibyte input
# with the getchar() function
# See http://search.cpan.org/~giraffed/Curses-1.36/Curses.pm#getchar
sub prompt_u8getch() {
my $chr = $prompt_win->getch();
my $cpt = ord($chr);
if ($chr =~ /\A\d+\z/ || $cpt <= 127) {
return $chr;
}
my $len=1;
if (($cpt & 0xe0) == 0xc0) { $len = 2; }
elsif (($cpt & 0xf0) == 0xe0) { $len = 3; }
elsif (($cpt & 0xf8) == 0xf0) { $len = 4; }
elsif (($cpt & 0xfc) == 0xf8) { $len = 5; }
elsif (($cpt & 0xfe) == 0xfc) { $len = 6; }
else {
return $chr;
}
for my $i (2..$len) {
my $chri = $prompt_win->getch();
return $chri if ((ord($chri) & 0xc0) != 0x80);
$chr .= $chri;
}
return decode_utf8($chr);
}
#------------------------------------------------------------------
sub prompt_chr {
my ($prompt) = @_;
my $ch;
echo();
curs_set(1);
&draw_prompt($prompt);
$ch = $prompt_win->getch();
if ( $ch eq KEY_RESIZE ) {
# FIXME resize
# This code chunk is also in getch.pl, except the call to draw_prompt_cur.
if ( $LINES > 1 ) {
&audit("Received KEY_RESIZE. Going to refresh.");
&init_curses('refresh');
&draw_screen();
} else {
&audit("Received KEY_RESIZE, but terminal height ($LINES) too small to
refresh.");
}
$ch = &prompt_chr($prompt);
}
noecho();
curs_set(0);
return $ch;
}
#------------------------------------------------------------------
sub prompt_str {
my ($prompt) = @_;
$cur_pos = length($prompt);
my $str = ''; # current user input
my $history_idx = 0;
my $addedPromptStr = 0;
my $mode; # type of completion to use (tags, command, projects)
my @match_types; # array containing strings to match against
my $tab_cnt = 0; # number of subsequent tab presses (for iterating through completions);
# shift-tabs can make this negative
my $tab_started = undef; # whether the user has pressed tab before
my $tab_match_str = ''; # value of $str upon first press of tab
# split prompt into prompt text and user input
if ( $prompt =~ /^(:)(.*)/ || $prompt =~ /^(.*?: )(.*)/ || $prompt =~ /^(.*?:)(.*)/ ) {
$prompt = $1;
$str = $2;
}
my $prompt_len = length($prompt);
# determine mode from prompt text
if ( $prompt eq ':' ) {
$mode = 'cmd';
@match_types = @report_types;
} else {
$mode = lc($prompt);
$mode =~ s/:.*$//;
if ( $mode eq 'project' ) {
@match_types = @project_types;
} elsif ( $mode eq 'tag' ) {
@match_types = @tag_types;
}
}
curs_set(1);
&draw_prompt("$prompt$str");
while (1) {
my $ch = prompt_u8getch();
if ( $tab_started and $ch ne "\t" and $ch ne KEY_BTAB
and $ch ne KEY_STAB and $ch ne KEY_RESIZE ) {
# When a key other than tab is pressed, then stop the tabbing cycle.
# That is, only uninterrupted tab keys cycle through completions.
# The only exceptions, which allow the continuation of tab cycling, are
# resize events, backtabs, shift-tabs and (obviously) regular tabs
$tab_started = undef;
#NOTE: no "next" here on purpose
}
if ( $ch eq "\cu" ) {
$str = substr($str, $cur_pos - $prompt_len);
$cur_pos = $prompt_len;
&draw_prompt_cur("$prompt$str");
next;
}
if ( $ch eq "\e" || $ch eq "\cg" ) {
noecho();
curs_set(0);
return '';
}
if ( $ch eq "\n" ) {
last;
}
if ( $ch eq "\t" or $ch eq KEY_STAB or $ch eq KEY_BTAB ) {
# handle tab completion
if ( $mode ne 'cmd' and $mode ne 'project' and $mode ne 'tag' ) {
# tab completion only makes sense for enumerable fields
beep();
next;
}
if ( not $tab_started ) {
# this is the first time the user presses tab (possibly after editing
# so reset the tab completion
$tab_match_str = $str;
$tab_started = 1;
$tab_cnt = 0;
}
# move forward or backwards in the list based on the key press (tab or backtab)
if ( $ch eq "\t") {
$tab_cnt++;
} else {
$tab_cnt--;
}
# check string to match
if ( $tab_match_str eq '' ) {
# empty string, so cycle through all possible options
my $idx = ($tab_cnt-1) % @match_types;
$str = $match_types[$idx];
} else {
# match based on the user input
# first we need to strip off a possible + or - prefix, which are used for tags
my $fc = ''; # (optional) prefix is saved here
if ($mode eq 'tag') {
$fc = substr($tab_match_str,0,1);
if ($fc eq '-' or $fc eq '+') {
$tab_match_str = substr($tab_match_str,1);
} else {
$fc = '';
}
}
# do the actual match. note the \Q\E to stop possible expansion of user input
my @matches = (grep(/^\Q$tab_match_str\E/,@match_types));
if ( $#matches == -1 ) {
# no match, so reset
$tab_started = undef;
beep();
} else {
my $idx = ($tab_cnt-1) % @matches;
# put back a potential + or - prefix
$str = $fc . $matches[$idx];
$tab_match_str = $fc . $tab_match_str;
}
}
&draw_prompt("$prompt$str");
next;
}
# This code was causing problems and was undocumented.
# if ( $ch eq "\cw" ) {
# if ( $str eq '' ) {
# chop $str;
# beep();
# next;
# }
# if ( $str =~ s/^(.*\s+)\S+\s+$/$1/ ) {
# &draw_prompt("$prompt$str");
# next;
# }
# if ( $str =~ s/^.*\s+$// ) {
# &draw_prompt("$prompt$str");
# next;
# }
# if ( $str =~ s/^(.*\s+).*/$1/ ) {
# &draw_prompt("$prompt$str");
# next;
# }
# $str = "";
# &draw_prompt("$prompt$str");
# next;
# }
if ( $ch eq KEY_BACKSPACE || $ch eq "\b" || $ch eq "\c?" ) {
if ( $cur_pos > $prompt_len ) {
$cur_pos--;
substr($str, $cur_pos - $prompt_len, 1, "");
&draw_prompt_cur("$prompt$str");
next;
}
}
if ( $ch eq KEY_LEFT ) {
if ( $cur_pos > $prompt_len ) {
$cur_pos -= 1;
}
&draw_prompt_cur("$prompt$str");
next;
}
if ( $ch eq KEY_RIGHT ) {
if ( $cur_pos < length("$prompt$str") ) {
$cur_pos += 1;
}
&draw_prompt_cur("$prompt$str");
next;
}
if ( $ch eq KEY_UP ) {
# We treat $history_idx specially because we save the prompt string the
# first time
# $#{ $histories{$prompt} } returns -1 if $prompt is not an existing key
if ( $history_idx > 0 && $history_idx >= $#{ $histories{$prompt} } ) {
next;
}
if ( $history_idx == 0 && $history_idx >= $#{ $histories{$prompt} } + 1 ) {
next;
}
if ( $history_idx == 0 ) {
if ( $addedPromptStr == 0 ) {
# don't add sequential duplicates
if ( $str ne $histories{$prompt}[0] ) {
$addedPromptStr = 1;
unshift @{ $histories{$prompt} }, $str;
}
}
else {
# if the user edits working prompt again, no need to add as separate
$histories{$prompt}[0] = $str;
}
}
$history_idx++;
$str = $histories{$prompt}[$history_idx];
&draw_prompt("$prompt$str");
}
if ( $ch eq KEY_DOWN ) {
if ( $history_idx == 0 ) {
next;
}
$history_idx--;
$str = $histories{$prompt}[$history_idx];
&draw_prompt("$prompt$str");
}
if ( $ch eq KEY_HOME ) {
$cur_pos = $prompt_len;
&draw_prompt_cur("$prompt$str");
}
if ( $ch eq KEY_END ) {
$cur_pos = length("$prompt$str");
&draw_prompt_cur("$prompt$str");
}
# Put hardcoded keys down here since they are the most fragile
# and could be platform-dependent. If they are defined differently,
# hopefully they will be matched above first.
if ( $ch eq 330 ) { # KEY_DELETE is not defined
if ( $cur_pos >= $prompt_len ) {
substr($str, $cur_pos - $prompt_len, 1, "");
&draw_prompt_cur("$prompt$str");
next;
}
}
if ( $ch eq KEY_RESIZE ) {
# FIXME resize
# This code chunk is also in getch.pl, except the call to draw_prompt_cur.
&audit("Received KEY_RESIZE. Going to refresh.");
&init_curses('refresh');
&draw_screen();
&draw_prompt_cur("$prompt$str");
curs_set(1);
next;
}
if ( ! &is_printable($ch) ) {
next;
}
if ( &is_printable($ch) ) {
substr($str, $cur_pos - $prompt_len, 0, $ch);
$cur_pos++;
}
&draw_prompt_cur("$prompt$str");
}
noecho();
curs_set(0);
if ( $mode ne 'project' && $str eq '' ) { beep(); }
if ( ! $str =~ /^:!/ ) {
$str =~ s/"/\\"/g;
$str =~ s/^\s+//;
$str =~ s/\s+$//;
}
if ( $addedPromptStr == 0 ) {
# we add if no elements or not a sequential duplicate
if ( $#{ $histories{$prompt} } < 0 || $str ne $histories{$prompt}[0] ) {
# do not add an empty string
if ( $str ne "" ) {
unshift @{ $histories{$prompt} }, $str;
}
}
}
else {
$histories{$prompt}[0] = $str;
}
return $str;
}
return 1;