-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.sh
executable file
·1249 lines (1045 loc) · 35.3 KB
/
config.sh
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
set -euo pipefail
Println()
{
printf '%b' "\n$1\n"
}
inquirer()
{
inquirer:print() {
tput el
printf '%b' "$1"
}
inquirer:join() {
local var=("$1"[@])
if [[ -z ${!var:-} ]]
then
return 0
fi
local join_list=("${!var}") first=true item
for item in "${join_list[@]}"
do
if [ "$first" = true ]
then
printf "%s" "$item"
first=false
else
printf "${2-, }%s" "$item"
fi
done
}
inquirer:on_default() {
return 0
}
inquirer:on_keypress() {
local OLD_IFS=$IFS
local key
local on_up=${1:-inquirer:on_default}
local on_down=${2:-inquirer:on_default}
local on_space=${3:-inquirer:on_default}
local on_enter=${4:-inquirer:on_default}
local on_left=${5:-inquirer:on_default}
local on_right=${6:-inquirer:on_default}
local on_ascii=${7:-inquirer:on_default}
local on_backspace=${8:-inquirer:on_default}
local on_not_ascii=${9:-inquirer:on_default}
break_keypress=false
while IFS="" read -rsn1 key
do
case "$key" in
$'\x1b')
read -rsn1 key
if [ "$key" == "[" ]
then
read -rsn1 key
case "$key" in
'A') $on_up;;
'B') $on_down;;
'D') $on_left;;
'C') $on_right;;
esac
fi
;;
$'\x20') $on_space;;
$'\x7f') $on_backspace "$key";;
'') $on_enter "$key";;
*[$'\x80'-$'\xFF']*) $on_not_ascii "$key";;
# [^ -~]
*) $on_ascii "$key";;
esac
if [ "$break_keypress" = true ]
then
break
fi
done
IFS=$OLD_IFS
}
inquirer:cleanup() {
# Reset character attributes, make cursor visible, and restore
# previous screen contents (if possible).
tput sgr0
tput cnorm
stty echo
}
inquirer:control_c() {
inquirer:cleanup
exit $?
}
inquirer:remove_instructions() {
if [ "$first_keystroke" = true ]
then
tput cuu $((current_index+1))
tput cub "$(tput cols)"
tput cuf $((prompt_width+3))
tput el
tput cud $((current_index+1))
first_keystroke=false
fi
}
inquirer:on_checkbox_input_up() {
inquirer:remove_instructions
tput cub "$(tput cols)"
if [ "${checkbox_selected[current_index]}" = true ]
then
inquirer:print " ${green}${checked}${normal} ${checkbox_list[current_index]}"
else
inquirer:print " ${unchecked} ${checkbox_list[current_index]}"
fi
if [ $current_index = 0 ]
then
current_index=$((${#checkbox_list[@]}-1))
tput cud $((${#checkbox_list[@]}-1))
else
current_index=$((current_index-1))
tput cuu1
fi
tput cub "$(tput cols)"
if [ "${checkbox_selected[current_index]}" = true ]
then
inquirer:print "${cyan}${arrow}${green}${checked}${normal} ${checkbox_list[current_index]}"
else
inquirer:print "${cyan}${arrow}${normal}${unchecked} ${checkbox_list[current_index]}"
fi
}
inquirer:on_checkbox_input_down() {
inquirer:remove_instructions
tput cub "$(tput cols)"
if [ "${checkbox_selected[current_index]}" = true ]
then
inquirer:print " ${green}${checked}${normal} ${checkbox_list[current_index]}"
else
inquirer:print " ${unchecked} ${checkbox_list[current_index]}"
fi
if [ $current_index = $((${#checkbox_list[@]}-1)) ]
then
current_index=0
tput cuu $((${#checkbox_list[@]}-1))
else
current_index=$((current_index+1))
tput cud1
fi
tput cub "$(tput cols)"
if [ "${checkbox_selected[current_index]}" = true ]
then
inquirer:print "${cyan}${arrow}${green}${checked}${normal} ${checkbox_list[current_index]}"
else
inquirer:print "${cyan}${arrow}${normal}${unchecked} ${checkbox_list[current_index]}"
fi
}
inquirer:on_checkbox_input_enter() {
local i
for i in "${!checkbox_list[@]}"
do
if [ "${checkbox_selected[i]}" = true ] && [ "$i" -ne "$checkbox_list_count" ]
then
checkbox_selected_indices+=("$i")
checkbox_selected_options+=("${checkbox_list[i]}")
fi
done
tput cub "$(tput cols)"
if [ -z "${checkbox_selected_indices:-}" ]
then
tput sc
failed_count=$((failed_count+1))
tput cuu $((current_index+1))
tput cuf $((prompt_width+3))
inquirer:print "${red}${checkbox_input_failed_msg}${normal}"
tput rc
else
tput cud $((${#checkbox_list[@]}-current_index))
for i in $(seq $((${#checkbox_list[@]}+1)))
do
tput el
tput cuu1
done
tput cuf $((prompt_width+3))
inquirer:print "${cyan}$(inquirer:join checkbox_selected_options)${normal}\n"
break_keypress=true
fi
}
inquirer:on_checkbox_input_space() {
local i
inquirer:remove_instructions
tput cub "$(tput cols)"
tput el
if [ "$current_index" -eq "$checkbox_list_count" ]
then
if [ "${checkbox_selected[current_index]}" = true ]
then
tput cuu $current_index
for i in "${!checkbox_list[@]}"
do
if [ "$i" -eq "$current_index" ]
then
inquirer:print "${cyan}${arrow}${normal}${unchecked} ${checkbox_list[i]}"
else
inquirer:print " ${unchecked} ${checkbox_list[i]}\n"
fi
done
for i in "${!checkbox_list[@]}"
do
checkbox_selected[i]=false
done
else
tput cuu $current_index
for i in "${!checkbox_list[@]}"
do
if [ "$i" -eq "$current_index" ]
then
inquirer:print "${cyan}${arrow}${green}${checked}${normal} ${checkbox_list[i]}"
else
inquirer:print " ${green}${checked}${normal} ${checkbox_list[i]}\n"
fi
done
for i in "${!checkbox_list[@]}"
do
checkbox_selected[i]=true
done
fi
else
if [ "${checkbox_selected[current_index]}" = true ]
then
checkbox_selected[current_index]=false
inquirer:print "${cyan}${arrow}${normal}${unchecked} ${checkbox_list[current_index]}"
else
checkbox_selected[current_index]=true
inquirer:print "${cyan}${arrow}${green}${checked}${normal} ${checkbox_list[current_index]}"
fi
fi
}
inquirer:on_checkbox_input_ascii() {
local key=$1
case "$key" in
"w" ) inquirer:on_checkbox_input_up;;
"s" ) inquirer:on_checkbox_input_down;;
esac
}
inquirer:_checkbox_input() {
local i var=("$2"[@])
checkbox_list=("${!var}")
checkbox_list_count=${#checkbox_list[@]}
if [ "$checkbox_list_count" -eq 1 ]
then
checkbox_selected_options=("${checkbox_list[@]}")
checkbox_selected_indices=(0)
return 0
fi
checkbox_selected=()
checkbox_selected_indices=()
checkbox_selected_options=()
checkbox_list+=("$(gettext 全选)")
checkbox_input_failed_msg=${4:-$(gettext "选择不能为空")}
current_index=0
failed_count=0
first_keystroke=true
trap inquirer:control_c SIGINT EXIT
stty -echo
tput civis
inquirer:print "${green}?${normal} ${bold}${prompt}${normal} ${dim}`gettext \"(按 <space> 选择, <enter> 确认)\"`${normal}\n"
for i in "${!checkbox_list[@]}"
do
checkbox_selected[i]=false
done
if [ -n "${3:-}" ]
then
var=("$3"[@])
if [[ -n ${!var:-} ]]
then
checkbox_selected_indices=("${!var}")
for i in "${checkbox_selected_indices[@]}"
do
checkbox_selected[i]=true
done
checkbox_selected_indices=()
fi
fi
for i in "${!checkbox_list[@]}"
do
if [ $i = 0 ]
then
if [ "${checkbox_selected[i]}" = true ]
then
inquirer:print "${cyan}${arrow}${green}${checked}${normal} ${checkbox_list[i]}\n"
else
inquirer:print "${cyan}${arrow}${normal}${unchecked} ${checkbox_list[i]}\n"
fi
else
if [ "${checkbox_selected[i]}" = true ]
then
inquirer:print " ${green}${checked}${normal} ${checkbox_list[i]}\n"
else
inquirer:print " ${unchecked} ${checkbox_list[i]}\n"
fi
fi
done
tput cuu ${#checkbox_list[@]}
inquirer:on_keypress inquirer:on_checkbox_input_up inquirer:on_checkbox_input_down inquirer:on_checkbox_input_space inquirer:on_checkbox_input_enter inquirer:on_default inquirer:on_default inquirer:on_checkbox_input_ascii
}
inquirer:checkbox_input() {
var_name=$3
inquirer:_checkbox_input "$1" "$2"
read -r -a ${var_name?} <<< "${checkbox_selected_options[@]}"
inquirer:cleanup
}
inquirer:checkbox_input_indices() {
var_name=$3
inquirer:_checkbox_input "$1" "$2" "$var_name"
read -r -a ${var_name?} <<< "${checkbox_selected_indices[@]}"
inquirer:cleanup
}
inquirer:on_sort_up() {
if [ "${#sort_options[@]}" -eq 1 ]
then
return 0
fi
tput cub "$(tput cols)"
inquirer:print " ${sort_options[current_index]}"
if [ $current_index = 0 ]
then
current_index=$((${#sort_options[@]}-1))
tput cud $((${#sort_options[@]}-1))
else
current_index=$((current_index-1))
tput cuu1
fi
tput cub "$(tput cols)"
inquirer:print "${cyan}${arrow} ${sort_options[current_index]} ${normal}"
}
inquirer:on_sort_down() {
if [ "${#sort_options[@]}" -eq 1 ]
then
return 0
fi
tput cub "$(tput cols)"
inquirer:print " ${sort_options[current_index]}"
if [ $current_index = $((${#sort_options[@]}-1)) ]
then
current_index=0
tput cuu $((${#sort_options[@]}-1))
else
current_index=$((current_index+1))
tput cud1
fi
tput cub "$(tput cols)"
inquirer:print "${cyan}${arrow} ${sort_options[current_index]} ${normal}"
}
inquirer:on_sort_move_up() {
if [ "${#sort_options[@]}" -eq 1 ]
then
return 0
fi
local i
tput cub "$(tput cols)"
if [ $current_index = 0 ]
then
for((i=1;i<${#sort_options[@]};i++));
do
inquirer:print " ${sort_options[i]}\n"
done
inquirer:print "${cyan}${arrow} ${sort_options[current_index]} ${normal}"
current_index=$((${#sort_options[@]}-1))
sort_options=( "${sort_options[@]:1}" "${sort_options[@]:0:1}" )
sort_indices=( "${sort_indices[@]:1}" "${sort_indices[@]:0:1}" )
else
inquirer:print " ${sort_options[current_index-1]}"
tput cuu1
tput cub "$(tput cols)"
inquirer:print "${cyan}${arrow} ${sort_options[current_index]} ${normal}"
local tmp="${sort_options[current_index]}"
sort_options[current_index]="${sort_options[current_index-1]}"
sort_options[current_index-1]="$tmp"
tmp="${sort_indices[current_index]}"
sort_indices[current_index]="${sort_indices[current_index-1]}"
sort_indices[current_index-1]="$tmp"
current_index=$((current_index-1))
fi
}
inquirer:on_sort_move_down() {
if [ "${#sort_options[@]}" -eq 1 ]
then
return 0
fi
local i
tput cub "$(tput cols)"
if [ $current_index = $((${#sort_options[@]}-1)) ]
then
tput cuu $((${#sort_options[@]}-1))
inquirer:print "${cyan}${arrow} ${sort_options[current_index]} ${normal}\n"
for((i=0;i<current_index;i++));
do
inquirer:print " ${sort_options[i]}\n"
done
tput cuu ${#sort_options[@]}
sort_options=( "${sort_options[@]:current_index}" "${sort_options[@]:0:current_index}" )
sort_indices=( "${sort_indices[@]:current_index}" "${sort_indices[@]:0:current_index}" )
current_index=0
else
inquirer:print " ${sort_options[current_index+1]}"
tput cud1
tput cub "$(tput cols)"
inquirer:print "${cyan}${arrow} ${sort_options[current_index]} ${normal}"
local tmp="${sort_options[current_index]}"
sort_options[current_index]="${sort_options[current_index+1]}"
sort_options[current_index+1]="$tmp"
tmp="${sort_indices[current_index]}"
sort_indices[current_index]="${sort_indices[current_index+1]}"
sort_indices[current_index+1]="$tmp"
current_index=$((current_index+1))
fi
}
inquirer:on_sort_ascii() {
case "$1" in
"w" ) inquirer:on_sort_move_up;;
"s" ) inquirer:on_sort_move_down;;
esac
}
inquirer:on_sort_enter_space()
{
local i
tput cud $((${#sort_options[@]}-current_index))
tput cub "$(tput cols)"
for i in $(seq $((${#sort_options[@]}+1)))
do
tput el
tput cuu1
done
tput cuf $((prompt_width+3))
inquirer:print "${cyan}$(inquirer:join sort_options)${normal}\n"
break_keypress=true
}
inquirer:_sort_input() {
local i var=("$2"[@])
sort_options=("${!var}")
sort_indices=("${!sort_options[@]}")
current_index=0
trap inquirer:control_c SIGINT EXIT
stty -echo
tput civis
inquirer:print "${green}?${normal} ${bold}${prompt}${normal} ${dim}`gettext \"(上下箭头选择, 按 <w> <s> 上下移动)\"`${normal}\n"
for i in "${!sort_options[@]}"
do
if [ $i = 0 ]
then
inquirer:print "${cyan}${arrow} ${sort_options[i]} ${normal}\n"
else
inquirer:print " ${sort_options[i]}\n"
fi
done
tput cuu ${#sort_options[@]}
inquirer:on_keypress inquirer:on_sort_up inquirer:on_sort_down inquirer:on_sort_enter_space inquirer:on_sort_enter_space inquirer:on_default inquirer:on_default inquirer:on_sort_ascii
}
inquirer:sort_input() {
var_name=$3
inquirer:_sort_input "$1" "$2"
read -r -a ${var_name?} <<< "${sort_options[@]}"
inquirer:cleanup
}
inquirer:sort_input_indices() {
var_name=$3
inquirer:_sort_input "$1" "$2"
read -r -a ${var_name?} <<< "${sort_indices[@]}"
inquirer:cleanup
}
inquirer:on_list_input_up() {
inquirer:remove_instructions
if [ "${#list_options[@]}" -eq 1 ]
then
return 0
fi
tput cub "$(tput cols)"
inquirer:print " ${list_options[current_index]}"
if [ $current_index = 0 ]
then
current_index=$((${#list_options[@]}-1))
tput cud $((${#list_options[@]}-1))
else
current_index=$((current_index-1))
tput cuu1
fi
tput cub "$(tput cols)"
inquirer:print "${cyan}${arrow} ${list_options[current_index]}${normal}"
}
inquirer:on_list_input_down() {
inquirer:remove_instructions
if [ "${#list_options[@]}" -eq 1 ]
then
return 0
fi
tput cub "$(tput cols)"
inquirer:print " ${list_options[current_index]}"
if [ $current_index = $((${#list_options[@]}-1)) ]
then
current_index=0
tput cuu $((${#list_options[@]}-1))
else
current_index=$((current_index+1))
tput cud1
fi
tput cub "$(tput cols)"
inquirer:print "${cyan}${arrow} ${list_options[current_index]} ${normal}"
}
inquirer:on_list_input_enter_space() {
local i
tput cud $((${#list_options[@]}-current_index))
tput cub "$(tput cols)"
for i in $(seq $((${#list_options[@]}+1)))
do
tput el
tput cuu1
done
tput cuf $((prompt_width+3))
inquirer:print "${cyan}${list_options[current_index]}${normal}\n"
break_keypress=true
}
inquirer:on_list_input_ascii() {
case "$1" in
"w" ) inquirer:on_list_input_up;;
"s" ) inquirer:on_list_input_down;;
esac
}
inquirer:_list_input() {
local i var=("$2"[@])
list_options=("${!var}")
current_index=0
if [ "${#list_options[@]}" -eq 1 ]
then
return 0
fi
first_keystroke=true
trap inquirer:control_c SIGINT EXIT
stty -echo
tput civis
inquirer:print "${green}?${normal} ${bold}${prompt}${normal} ${dim}`gettext \"(使用上下箭头选择)\"`${normal}\n"
for i in "${!list_options[@]}"
do
if [ $i = 0 ]
then
inquirer:print "${cyan}${arrow} ${list_options[i]} ${normal}\n"
else
inquirer:print " ${list_options[i]}\n"
fi
done
tput cuu ${#list_options[@]}
inquirer:on_keypress inquirer:on_list_input_up inquirer:on_list_input_down inquirer:on_list_input_enter_space inquirer:on_list_input_enter_space inquirer:on_default inquirer:on_default inquirer:on_list_input_ascii
}
inquirer:list_input() {
var_name=$3
inquirer:_list_input "$1" "$2"
read -r ${var_name?} <<< "${list_options[current_index]}"
inquirer:cleanup
}
inquirer:list_input_index() {
var_name=$3
inquirer:_list_input "$1" "$2"
read -r ${var_name?} <<< "$current_index"
inquirer:cleanup
}
inquirer:on_text_input_left() {
if [[ $current_pos -gt 0 ]]
then
local current=${text_input:$current_pos:1} current_width
current_width=$(inquirer:display_length "$current")
tput cub $current_width
current_pos=$((current_pos-1))
fi
}
inquirer:on_text_input_right() {
if [[ $((current_pos+1)) -eq ${#text_input} ]]
then
tput cuf1
current_pos=$((current_pos+1))
elif [[ $current_pos -lt ${#text_input} ]]
then
local next=${text_input:$((current_pos+1)):1} next_width
next_width=$(inquirer:display_length "$next")
tput cuf $next_width
current_pos=$((current_pos+1))
fi
}
inquirer:on_text_input_enter() {
text_input=${text_input:-$text_default_value}
tput civis
tput cub "$(tput cols)"
tput el
if $text_input_validator "$text_input"
then
tput sc
tput cuu $((1+failed_count*3))
tput cuf $((prompt_width+3))
inquirer:print "${cyan}${text_input}${normal}"
tput rc
break_keypress=true
else
failed_count=$((failed_count+1))
tput cud1
inquirer:print "${red}${text_input_regex_failed_msg}${normal}"
tput cud1
if [ "$text_input" == "$text_default_value" ]
then
text_input=""
current_pos=0
else
inquirer:print "${text_input}"
current_pos=${#text_input}
fi
fi
tput cnorm
}
inquirer:on_text_input_ascii() {
local c=${1:- }
local rest=${text_input:$current_pos} rest_width
local current=${text_input:$current_pos:1} current_width
rest_width=$(inquirer:display_length "$rest")
current_width=$(inquirer:display_length "$current")
text_input="${text_input:0:$current_pos}$c$rest"
current_pos=$((current_pos+1))
tput civis
[[ $current_width -gt 1 ]] && tput cub $((current_width-1))
inquirer:print "$c$rest"
if [[ $rest_width -gt 0 ]]
then
tput cub $((rest_width-current_width+1))
fi
tput cnorm
}
inquirer:on_text_input_not_ascii() {
local c=$1
local rest="${text_input:$current_pos}" rest_width
local current=${text_input:$current_pos:1} current_width
rest_width=$(inquirer:display_length "$rest")
current_width=$(inquirer:display_length "$current")
text_input="${text_input:0:$current_pos}$c$rest"
current_pos=$((current_pos+1))
tput civis
[[ $current_width -gt 1 ]] && tput cub $((current_width-1))
inquirer:print "$c$rest"
if [[ $rest_width -gt 0 ]]
then
tput cub $((rest_width-current_width+1))
fi
tput cnorm
}
inquirer:on_text_input_backspace() {
if [ $current_pos -gt 0 ] || { [ $current_pos -eq 0 ] && [ "${#text_input}" -gt 0 ]; }
then
local start rest rest_width del del_width next next_width offset
local current=${text_input:$current_pos:1} current_width
current_width=$(inquirer:display_length "$current")
tput civis
if [ $current_pos -eq 0 ]
then
rest=${text_input:$((current_pos+1))}
next=${text_input:$((current_pos+1)):1}
rest_width=$(inquirer:display_length "$rest")
next_width=$(inquirer:display_length "$next")
offset=$((current_width-1))
[[ $offset -gt 0 ]] && tput cub $offset
inquirer:print "$rest"
offset=$((rest_width-next_width+1))
[[ $offset -gt 0 ]] && tput cub $offset
text_input="$rest"
else
rest=${text_input:$current_pos}
start=${text_input:0:$((current_pos-1))}
del=${text_input:$((current_pos-1)):1}
rest_width=$(inquirer:display_length "$rest")
del_width=$(inquirer:display_length "$del")
current_pos=$((current_pos-1))
if [[ $current_width -gt 1 ]]
then
tput cub $((del_width+current_width-1))
inquirer:print "$rest"
tput cub $((rest_width-current_width+1))
else
tput cub $del_width
inquirer:print "$rest"
[[ $rest_width -gt 0 ]] && tput cub $((rest_width-current_width+1))
fi
text_input="$start$rest"
fi
tput cnorm
fi
}
inquirer:text_input_default_validator() {
return 0
}
inquirer:text_input() {
var_name=$2
text_default_value=${3:-}
text_input=""
current_pos=0
failed_count=0
local text_default_tip
if [ -n "$text_default_value" ]
then
text_default_tip=" ${bold}${dim}($text_default_value)${normal}"
else
text_default_tip=""
fi
text_input_regex_failed_msg=${4:-$(gettext "输入验证错误")}
text_input_validator=${5:-inquirer:text_input_default_validator}
inquirer:print "${green}?${normal} ${prompt}${text_default_tip}\n"
trap inquirer:control_c SIGINT EXIT
stty -echo
tput cnorm
inquirer:on_keypress inquirer:on_default inquirer:on_default inquirer:on_text_input_ascii inquirer:on_text_input_enter inquirer:on_text_input_left inquirer:on_text_input_right inquirer:on_text_input_ascii inquirer:on_text_input_backspace inquirer:on_text_input_not_ascii
read -r ${var_name?} <<< "$text_input"
inquirer:cleanup
}
inquirer:date_pick_default_validator() {
if ! date +%s -d "$1" > /dev/null 2>&1
then
return 1
fi
return 0
}
inquirer:remove_date_instructions() {
if [ "$first_keystroke" = true ]
then
tput sc
tput civis
tput cuu 1
tput cub "$(tput cols)"
tput cuf $((prompt_width+3))
tput el
tput rc
tput cnorm
first_keystroke=false
fi
}
inquirer:on_date_pick_ascii() {
case "$1" in
"w" ) inquirer:on_date_pick_up;;
"s" ) inquirer:on_date_pick_down;;
"a" ) inquirer:on_date_pick_left;;
"d" ) inquirer:on_date_pick_right;;
esac
}
inquirer:on_date_pick_up() {
inquirer:remove_date_instructions
case $current_pos in
3) date_pick="$((${date_pick:0:4}+1))${date_pick:4}"
;;
6)
local month=$((10#${date_pick:5:2}+1))
[ "$month" -eq 13 ] && month=1
date_pick="${date_pick:0:5}$(printf %02d "$month")${date_pick:7}"
;;
9)
local day=$((10#${date_pick:8:2}+1))
[ "$day" -eq 32 ] && day=1
date_pick="${date_pick:0:8}$(printf %02d "$day")${date_pick:10}"
;;
12)
local hour=$(((10#${date_pick:11:2}+1)%24))
date_pick="${date_pick:0:11}$(printf %02d "$hour")${date_pick:13}"
;;
15)
local min=$(((10#${date_pick:14:2}+1)%60))
date_pick="${date_pick:0:14}$(printf %02d "$min")${date_pick:16}"
;;
18)
local sec=$(((10#${date_pick:17:2}+1)%60))
date_pick="${date_pick:0:17}$(printf %02d "$sec")${date_pick:19}"
;;
esac
tput sc
tput civis
tput cub $current_pos
inquirer:print "$date_pick"
tput rc
tput cnorm
}
inquirer:on_date_pick_down() {
inquirer:remove_date_instructions
case $current_pos in
3)
local year=$((${date_pick:0:4}-1))
[ "$year" -eq 2020 ] && return 0
date_pick="$year${date_pick:4}"
;;
6)
local month=$((10#${date_pick:5:2}-1))
[ "$month" -eq 0 ] && month=12
date_pick="${date_pick:0:5}$(printf %02d "$month")${date_pick:7}"
;;
9)
local day=$((10#${date_pick:8:2}-1))
[ "$day" -eq 0 ] && day=31
date_pick="${date_pick:0:8}$(printf %02d "$day")${date_pick:10}"
;;
12)
local hour=$(((10#${date_pick:11:2}+23)%24))
date_pick="${date_pick:0:11}$(printf %02d "$hour")${date_pick:13}"
;;
15)
local min=$(((10#${date_pick:14:2}+59)%60))
date_pick="${date_pick:0:14}$(printf %02d "$min")${date_pick:16}"
;;
18)
local sec=$(((10#${date_pick:17:2}+59)%60))
date_pick="${date_pick:0:17}$(printf %02d "$sec")${date_pick:19}"
;;
esac
tput sc
tput civis
tput cub $current_pos
inquirer:print "$date_pick"
tput rc
tput cnorm
}
inquirer:on_date_pick_left() {
inquirer:remove_date_instructions
if [[ $current_pos -gt 3 ]]
then
tput cub 3
current_pos=$((current_pos-3))
fi
}
inquirer:on_date_pick_right() {
inquirer:remove_date_instructions
if [[ $current_pos -lt 18 ]]
then
tput cuf 3
current_pos=$((current_pos+3))
fi
}
inquirer:on_date_pick_enter_space() {
tput civis