-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsentaku
executable file
·2032 lines (1892 loc) · 44.2 KB
/
sentaku
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
#!/usr/bin/env bash
## Description {{{
#
# Utility to make sentaku (selection) window with shell command.
#
# Homepage: https://github.com/rcmdnk/sentaku
#
# Usage: sentaku [-HNladnh] [-f <file>] [-s <sep>] [input variables]
#
#
SENTAKU_VERSION=v0.8.0
SENTAKU_DATE="30/Sep/2019"
#
# }}}
## License {{{
#
#The MIT License (MIT)
#
#Copyright (c) 2014 rcmdnk
#
#Permission is hereby granted, free of charge, to any person obtaining a copy of
#this software and associated documentation files (the "Software"), to deal in
#the Software without restriction, including without limitation the rights to
#use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
#the Software, and to permit persons to whom the Software is furnished to do so,
#subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in all
#copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
#FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
#COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
#IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
#CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# }}}
# Default variables # {{{
_SENTAKU_INPUT_FILE=${_SENTAKU_INPUT_FILE:-}
_SENTAKU_SEPARATOR=${_SENTAKU_SEPARATOR:-$IFS}
_SENTAKU_MAX=${_SENTAKU_MAX:-20}
_SENTAKU_MIN_SHOW=${_SENTAKU_MIN_SHOW:-3}
_SENTAKU_NOHEADER=${_SENTAKU_NOHEADER:-0}
_SENTAKU_NONUMBER=${_SENTAKU_NONUMBER:-0}
_SENTAKU_SHOWLAST=${_SENTAKU_SHOWLAST:-0}
_SENTAKU_CONTENT_LINES=${_SENTAKU_CONTENT_LINES:-10}
_SENTAKU_CONTENT_SHOW_UNDER=${_SENTAKU_CONTENT_SHOW_UNDER:-0}
_SENTAKU_CHILD=${_SENTAKU_CHILD:-0}
# 0: AND (smart case), 1: AND (case sensitive), 2: starts with (smart case), 3: starts with (case sensitive)
_SENTAKU_SEARCH_OPT=${_SENTAKU_SEARCH_OPT:-0}
_SENTAKU_PAGE_CHANGE=${_SENTAKU_PAGE_CHANGE:-1}
_SENTAKU_KEYMODE=${_SENTAKU_KEYMODE:-0}
_SENTAKU_COLOR_NUM_ONLY=${_SENTAKU_COLOR_NUM_ONLY:-0}
_SENTAKU_LINE_HIGHLIGHT=${_SENTAKU_LINE_HIGHLIGHT:-1}
_SENTAKU_DEBUG=${_SENTAKU_DEBUG:-0}
# }}}
_sf_help() { # {{{
if [[ -t 1 ]]; then
echo "$_s_help" | ${PAGER:-less} >/dev/tty
else
echo "$_s_help"
fi
} # }}}
_sf_initialize() { # {{{
# Set variables
_s_file="${SENTAKU_INPUT_FILE:-$_SENTAKU_INPUT_FILE}"
_s_use_file=0
_s_s="${SENTAKU_SEPARATOR:-$_SENTAKU_SEPARATOR}"
# If a separator is normal IFS, use space for file write out.
# note: $' \t\n' should not be surrounded by '"'.
if [[ "$_s_s" = $' \t\n' ]]; then
_s_s_push=" "
else
_s_s_push="$_s_s"
fi
_s_inputs=()
_s_cmd_line_inputs=""
_s_n=0
_s_max=${SENTAKU_MAX:-$_SENTAKU_MAX}
_s_min_show=${SENTAKU_MIN_SHOW:-$_SENTAKU_MIN_SHOW}
_s_header=""
_s_noheader=${SENTAKU_NOHEADER:-$_SENTAKU_NOHEADER}
_s_nonumber=${SENTAKU_NONUMBER:-$_SENTAKU_NONUMBER}
_s_showlast=${SENTAKU_SHOWLAST:-$_SENTAKU_SHOWLAST}
_s_content_view=0
_s_s_content=0
_s_show_content=0
_s_content_lines=${SENTAKU_CONTENT_LINES:-$_SENTAKU_CONTENT_LINES}
_s_content_show_under=${SENTAKU_CONTENT_SHOW_UNDER:-$_SENTAKU_CONTENT_SHOW_UNDER}
_s_search_opt=${SENTAKU_SEARCH_OPT:-$_SENTAKU_SEARCH_OPT}
_s_page_change=${SENTAKU_PAGE_CHANGE:-$_SENTAKU_PAGE_CHANGE}
_s_keymode=${SENTAKU_KEYMODE:-$_SENTAKU_KEYMODE}
_s_col_n_only=${SENTAKU_COLOR_NUM_ONLY:-$_SENTAKU_COLOR_NUM_ONLY}
_s_line_highlight=${SENTAKU_LINE_HIGHLIGHT:-$_SENTAKU_LINE_HIGHLIGHT}
_s_debug=${SENTAKU_DEBUG:-$_SENTAKU_DEBUG}
_s_show=""
_s_lines=0
_s_cols=0
_s_max_show=0
_s_stdin=0
_s_align=0
_s_delete=0
_s_continue=0
_s_noshow=0
_s_trapped=0
_s_normal_echo=${_s_normal_echo:-1}
_s_is_print=1
_s_ext_row=0
_s_current_n=-1
_s_num=""
_s_n_offset=0
_s_g=0
_s_n_move=-1
_s_visual=-1
_s_v_selected=()
_s_break=0
_s_search=""
_s_search_words=""
_s_pre_inputs_n=0
local i
for i in $(seq 0 999); do
eval "_s_pre_inputs_$i=()"
done
_s_read=""
_s_zsh_ksharrays=0
_s_stty=""
_s_IFS=$IFS
_s_ret=0
# Help
_s_help=${_s_help:-"Usage: sentaku [-HNulapEVcCURLSnvh] [-f <file>] [-s <sep>] [-r <n>] [input variables]
Arguments:
-f <file> Set input file
-s <sep> Set separator (default: ${SENTAKU_SEPARATOR:-$_SENTAKU_SEPARATOR})
If <sep> is \"line\", \$'\\n' is set as a separator.
-H Force to show a header at sentaku window.
-N No numbers are shown.
-u Use underline to show selected line, instead of highlighting.
-l Show last words instead of starting words for longer lines.
-a Align input list (set selected one to the first).
-r <n> Return nth value directly.
-p Push words to the file.
-E Use Emacs mode
-V Use Vim mode
-c Load functions as a child process in other sentaku process.
-C Show the file content at the list view
-R Show the file content in the right (default: right)
-U Show the file content under the list (default: right)
-L Number of lines to show the file content under the list (default: ${SENTAKU_CONTENT_LINES:-$_SENTAKU_CONTENT_LINES})
-S Open the file under the cursor by \${VISUAL:-less} at s
instead of showing full line of the selected one
-n Don't run functions, to just source this file
-v Show version
-h Print this HELP and exit
Key operation at sentaku window
Common for all:
C-p/C-n Up/Down.
C-u/C-d Half page down/Half page down.
C-b/C-f Page up/Page down.
M-v/C-v Page up/Page down.
C-a/C-e Go to the beginning/end.
C-i/C-o Move the item up/down.
C-x Quit.
C-s Start/Stop Visual mode (multi-selection).
Space Select/unselect current line for multi-selection.
At Emacs mode or search mode in Vim mode,
it selects when space is pushed twice.
Esc At search mode, first Esc takes it back to normal mode
with selected words.
Second Esc clear search mode.
Visual mode is cleared by first Esc.
Enter Select and Quit.
For Vim mode:
n(any number) Set number. Multi-digit can be used (13, 320, etc...).
Used/reset by other key.
k/j Up/Down (if n is given, n-th up/n-th down).
gg/G Go to top/bottom. (If n is given, move to n-th candidate.)
d Delete current candidate. (in case you use input file.)
s Show detail of current candidate.
v Visual mode, same as C-s
/ Search.
q Quit.
Others Nothing happens.
For Emacs mode:
Others Normal keys start an incremental search.
"}
# Lines and columns at beginning
_s_lines=$(tput lines)
_s_cols=$(tput cols)
# Fix array for zsh
if [[ -n "$ZSH_VERSION" ]]; then
if ! setopt | grep -q ksharrays; then
_s_zsh_ksharrays=1
setopt ksharrays
fi
fi
# Check std input
[[ ! -t 0 ]] && _s_stdin=1
# User initialization
_sf_initialize_user
} # }}}
_sf_initialize_user() { # {{{
:
} # }}}
_sf_finalize() { # {{{
IFS=$_s_IFS
if ((_s_ret == 0 && _s_current_n >= 0)); then
# Align values
if [[ "$_s_search" != "" ]]; then
local val="${_s_inputs[$_s_current_n]}"
local -a selected
selected=()
IFS="$_s_s"
if [[ $_s_visual -ne -1 ]]; then
local is_first=1
local i
local j=0
for i in $(seq 0 $((_s_n - 1))); do
if ((_s_v_selected[i] == 1)); then
selected[$j]=${_s_inputs[$i]}
((j++))
fi
done
fi
_s_inputs=("${_s_pre_inputs_0[@]}")
IFS=$_s_IFS
_s_n=${#_s_inputs[@]}
if ((_s_visual != -1)); then
local i
for i in $(seq 0 $((_s_n - 1))); do
_s_v_selected[$i]=0
done
fi
_s_current_n=0
if ((_s_visual != -1)); then
local i
local j
for i in $(seq 0 $((_s_n - 1))); do
for j in $(seq 0 $((${#selected[@]} - 1))); do
if [[ "${selected[$j]}" = "${_s_inputs[$i]}" ]]; then
_s_current_n=$i
_s_v_selected[$i]=1
fi
done
done
else
local i
for i in $(seq 0 $((_s_n - 1))); do
if [[ "$val" = "${_s_inputs[$i]}" ]]; then
_s_current_n=$i
break
fi
done
fi
_s_search=""
fi
# Execution for selected value
_sf_execute
((_s_align == 1 && _s_stdin == 0)) && _sf_align_values "$_s_current_n" 0
fi
if ((_SENTAKU_CHILD == 0 && _s_noshow == 0)); then
# Show cursor
tput cnorm >/dev/tty 2>/dev/null || tput vs >/dev/tty 2>/dev/null
# Enable echo input
if ((_s_stdin == 0)); then
if [[ "$_s_stty" != "" ]]; then
stty $_s_stty
# followings are for fixing problems of previous commands...
stty echo
fi
fi
fi
if ((_s_trapped == 1)); then
# fixed at C-c
stty echo 2>/dev/null
fi
# Release variables
unset _s_file
unset _s_use_file
unset _s_s
unset _s_s_push
unset _s_inputs
unset _s_cmd_line_inputs
unset _s_n
unset _s_max
unset _s_min_show
unset _s_header
unset _s_noheader
unset _s_nonumber
unset _s_showlast
unset _s_content_view
unset _s_s_content
unset _s_show_content
unset _s_content_lines
unset _s_content_show_under
unset _s_search_opt
unset _s_page_change
unset _s_keymode
unset _s_col_n_only
unset _s_line_highlight
unset _s_debug
unset _s_show
unset _s_lines
unset _s_cols
unset _s_max_show
unset _s_stdin
unset _s_align
unset _s_delete
unset _s_continue
unset _s_noshow
unset _s_trapped
unset _s_normal_echo
unset _s_is_print
unset _s_ext_row
unset _s_current_n
unset _s_num
unset _s_n_offset
unset _s_g
unset _s_n_move
unset _s_visual
unset _s_v_selected
unset _s_break
unset _s_search
unset _s_search_words
unset _s_pre_inputs_n
local i
for i in $(seq 0 999); do
eval "unset _s_pre_inputs_$i"
done
unset _s_read
unset _s_help
[[ "x$_s_zsh_ksharrays" != "x" && $_s_zsh_ksharrays -eq 1 ]] && unsetopt ksharrays
unset _s_zsh_ksharrays
unset _s_stty
unset _s_IFS
_sf_finalize_user
local ret=$_s_ret
unset _s_ret
return $ret
} # }}}
_sf_finalize_user() { # {{{
:
} # }}}
_sf_execute() { # {{{
if ((_s_visual != -1)); then
local is_first=1
local i
for i in $(seq 0 $((_s_n - 1))); do
if ((_s_v_selected[i] == 1)); then
if ((is_first == 1)); then
printf "%s" "${_s_inputs[$i]}"
is_first=0
else
printf "$_s_s%s" "${_s_inputs[$i]}"
fi
fi
done
else
printf "%s" "${_s_inputs[$_s_current_n]}"
fi
} # }}}
_sf_hide() { # {{{
if ((_SENTAKU_CHILD == 0 && _s_noshow == 0)); then
if ((_s_stdin == 0)); then
# Save current stty
_s_stty=$(stty -g)
# Hide any input
stty -echo
fi
# Hide cursor
tput civis >/dev/tty 2>/dev/null || tput vi >/dev/tty 2>/dev/null
# Save current display
tput smcup >/dev/tty 2>/dev/null || tput ti >/dev/tty 2>/dev/null
_s_normal_echo=0
fi
} # }}}
_sf_clear() { # {{{
if ((_SENTAKU_CHILD == 0 && _s_noshow == 0)); then
# clear after selection
clear >/dev/tty
# Restore display
tput rmcup >/dev/tty 2>/dev/null || tput te >/dev/tty 2>/dev/null
_s_normal_echo=1
fi
} # }}}
_sf_nth() { # {{{
case $1 in
1) echo 1st ;;
2) echo 2nd ;;
3) echo 3rd ;;
*) echo "${1}th" ;;
esac
} # }}}
_sf_read() { # {{{
IFS=
if [[ -n "$ZSH_VERSION" ]]; then
read -srk 1 _s_read </dev/tty
else
read -srn 1 _s_read </dev/tty
fi
IFS=$_s_IFS
} # }}}
_sf_wait() { # {{{
_sf_read
} # }}}
_sf_yn() { # {{{
local message="$*"
while :; do
clear >/dev/tty
echo "$message [y/n]: " >/dev/tty
_sf_read
if [[ "$_s_read" = "y" ]]; then
return 0
elif [[ "$_s_read" = "n" ]]; then
_sf_quit
return 1
fi
done
} # }}}
_sf_check_args() { # {{{
# Get arguments
_s_continue=0
while [[ $# -gt 0 ]]; do
case $1 in
"-f") # Use file
_s_file=$2
_s_use_file=1
if [[ "$_s_file" = "" ]]; then
echo "ERROR: empty input was given with -i" >/dev/tty
return 1
fi
shift
;;
"-s") # Set separator
_s_s=$2
if [[ "$_s_s" = "line" ]]; then
_s_s=$'\n'
fi
if [[ "$_s_s" = $' \t\n' ]]; then
_s_s_push=" "
else
_s_s_push="$_s_s"
fi
shift
;;
"-H") _s_noheader=0 ;;
"-N") _s_nonumber=1 ;;
"-u") _s_line_highlight=0 ;;
"-l") _s_showlast=1 ;;
"-a") _s_align=1 ;;
"-m") ;;
"-r")
if [[ "$2" =~ ^[0-9]+$ ]]; then
_s_current_n=$2
shift
else
echo "-r option requires a number" >/dev/tty
return 1
fi
;;
"-p")
shift
_sf_push "$@"
return $?
;;
"-E") _s_keymode=1 ;;
"-V") _s_keymode=0 ;;
"-c") _SENTAKU_CHILD=1 ;;
"-C") _s_show_content=1 ;;
"-R")
_s_show_content=1
_s_content_show_under=0
;;
"-U")
_s_show_content=1
_s_content_show_under=1
;;
"-L")
_s_content_lines=$2
shift
;;
"-S") _s_s_content=1 ;;
"-n") ;;
"-v")
echo "$(basename -- "$0" 2>/dev/null) $SENTAKU_VERSION $SENTAKU_DATE" >/dev/tty
return 0
;;
"-h")
_sf_help
return 0
;;
-*)
echo "$(basename -- "$0" 2>/dev/null) $1: unknown argument
Check \"$(basename -- "$0" 2>/dev/null) -h\" for further information" >/dev/tty
return 1
;;
*)
break
;;
esac
shift
done
_s_cmd_line_inputs="$*"
_s_continue=1
return 0
} # }}}
_sf_get_values_wrapper() { # {{{ _sf_get_values_wrapper [<is_stdin> [<is_check>]]
_sf_get_values "$1" "$2"
if ((_s_n == 0)); then
return 1
else
_sf_setview
fi
} # }}}
_sf_get_values() { # {{{ _sf_get_values [<is_stdin> [<is_check>]]
local stdin=$_s_stdin
local check=1
if [[ "$1" != "" ]]; then
stdin=$1
fi
if [[ "$2" != "" ]]; then
check=$2
fi
# Get values
IFS="$_s_s"
if [[ "$stdin" -eq 0 || "$_s_use_file" -eq 1 ]]; then
if [[ "$_s_use_file" -eq 0 && -z "$_s_file" ]]; then
_s_inputs=($(echo "$_s_cmd_line_inputs"))
else
touch "$_s_file"
_s_inputs=($(cat "$_s_file"))
fi
else
_s_inputs=($(cat -))
fi
IFS=$_s_IFS
if [[ -n "$ZSH_VERSION" ]]; then
# Fix array for ZSH
# Zsh's array adds additional empty value to array if IFS is in the end of file.
if [[ ${#_s_inputs[@]} -gt 0 ]]; then
local last="${_s_inputs[$((${#_s_inputs[@]} - 1))]}"
if [[ "${#last}" -eq 0 ]]; then
_s_inputs=(${_s_inputs[0, $((${#_s_inputs[@]} - 2))]})
fi
fi
fi
_s_n=${#_s_inputs[@]}
if ((check == 1 && _s_n == 0)); then
if ((stdin == 0 || _s_use_file == 1)); then
_sf_echoln "No value in $_s_file\\n"
else
_sf_echoln "No value in stdin\\n"
fi
_s_ret=1
return
elif ((_s_n > 0)); then
_s_v_selected=()
local i
for i in $(seq 0 $((_s_n - 1))); do
_s_v_selected[$i]=0
done
fi
_s_ret=0
} # }}}
_sf_align_values() { # {{{ _sf_align_values [<n> [<is_get>]]
local n=${1:-$_s_current_n}
local is_get=${2:-1}
if [[ ! "$n" =~ ^[0-9]+$ || "$n" -ge "$_s_n" ]]; then
_sf_echoln "$n is not valid for _sf_align_values"
return 1
fi
local v="${_s_inputs[$n]}"
printf "%s$_s_s_push" "$v" >"$_s_file"
local i=0
for i in $(seq 0 $((_s_n - 1))); do
if [[ "$i" -ge "$_s_max" ]]; then break; fi
if [[ "${_s_inputs[$i]}" != "$v" ]]; then
printf "%s$_s_s_push" "${_s_inputs[$i]}" >>"$_s_file"
fi
done
if [[ "$is_get" -eq 1 ]]; then
_sf_get_values_wrapper
fi
} # }}}
_sf_delete() { # {{{
echo -n >"$_s_file"
local i
local old_current_n=$_s_current_n
for i in $(seq 0 $((_s_n - 1))); do
if (((_s_visual != -1 && _s_v_selected[i] == 1) || (\
_s_visual == -1 && i == old_current_n))); then
if ((i < old_current_n)); then
((_s_current_n--))
fi
continue
fi
printf "%s$_s_s_push" "${_s_inputs[$i]}" >>"$_s_file"
done
_sf_get_values_wrapper
} # }}}
_sf_remove() { # {{{
local inputs
inputs=()
local i
local old_current_n=$_s_current_n
for i in $(seq 0 $((_s_n - 1))); do
if (((_s_visual != -1 && _s_v_selected[i] == 1) || (\
_s_visual == -1 && i == old_current_n))); then
if ((i < old_current_n)); then
((_s_current_n--))
fi
continue
fi
inputs[${#inputs[@]}]="${_s_inputs[$i]}"
done
_s_inputs=("${inputs[@]}")
_s_n=${#_s_inputs[@]}
for i in $(seq 0 $((_s_n - 1))); do
_s_v_selected[$i]=0
done
} # }}}
_sf_rm_del() { # {{{
if ((_s_delete == 0 || _s_stdin == 1)); then
_sf_remove
else
_sf_delete
fi
if ((_s_n == 0)); then
_sf_echoln "There are no remained entries"
_s_ret=1
fi
if ((_s_ret != 0)); then
_sf_quit $_s_ret
return
fi
if ((_s_current_n >= _s_n)); then
_s_current_n=$((_s_n - 1))
fi
if ((_s_current_n < _s_n_offset)); then
_s_n_offset=$_s_current_n
fi
local n_move
if ((_s_n < (_s_lines - _s_ext_row + _s_n_offset))); then
local n_move=$((_s_lines - _s_ext_row + _s_n_offset - _s_n))
if ((n_move > _s_n_offset)); then
n_move=$_s_n_offset
fi
_s_n_offset=$((_s_n_offset - n_move))
fi
_s_visual=-1
local i
for i in $(seq 0 $((_s_n - 1))); do
_s_v_selected[$i]=0
done
_s_is_print=1
} # }}}
_sf_add_spaces() { # {{{ _sf_add_spaces <var> <n> [is_prepend]
local var=$1
local n=$2
local prepend=${3:-0}
while ((n > 0)); do
if ((prepend == 1)); then
eval "$var=\" \$$var\""
else
eval "$var=\"\$$var \""
fi
((n--))
done
} # }}}
_sf_cut_word() { # {{{ _sf_cut_word <var> <max_length> [<is_last_show>]
local var=$1
local max_length=$2
local is_last_show=${3:-0}
if eval "[[ \${#$var} -le $max_length ]]"; then
eval "_sf_add_spaces $var \$(($max_length-\${#$var}))"
return
fi
if ((is_last_show == 0)); then
eval "$var=\${$var: 0: $max_length}"
return
else
eval "$var=\${$var: \$((\${#$var}-max_length))}"
return
fi
} # }}}
_sf_get_content() { # {{{
if ! file "${_s_inputs[$_s_current_n]}" | grep -q text; then
echo "Not a text file"
else
cat "${_s_inputs[$_s_current_n]}"
fi
} # }}}
_sf_content() { # {{{
if ((_s_content_view == 0)); then
return
fi
local i
local start_line=$((_s_lines + 1))
local end_line=$(($(tput lines) - 1))
local start_col=0
local n_cols=$_s_cols
if ((_s_content_show_under == 0)); then
start_line=0
start_col=$_s_cols
n_cols=$_s_content_cols
fi
for i in $(seq $start_line $end_line); do
tput cup "$i" "$start_col" >/dev/tty
printf "%-${n_cols}s" "" >/dev/tty
done
if ((_s_content_show_under == 1)); then
tput cup "$_s_lines" 0 >/dev/tty
printf -- "---"
fi
i=$start_line
while read -r line; do
if ((_s_content_show_under == 0)); then
line="|$line"
fi
if ((${#line} >= n_cols)); then
if [ "x$ZSH_VERSION" != "x" ]; then
line="${line[0, $((n_cols - 1))]}"
else
line="${line:0:$n_cols}"
fi
fi
tput cup "$i" "$start_col" >/dev/tty
printf "%s" "$line"
((i++))
if [ $i -gt $end_line ]; then
break
fi
done < <(_sf_get_content)
if ((_s_content_show_under == 0)); then
while ((i < end_line)); do
tput cup "$i" "$start_col" >/dev/tty
printf "|"
((i++))
done
fi
} # }}}
_sf_show() { # _sf_show [<is_selected>] [<n_show>] {{{
local is_selected=${1:-0}
local n_show=${2:-$_s_cols}
_sf_cut_word _s_show "$n_show" "$_s_showlast"
# Color search words
if [[ "$_s_search_words" != "" ]]; then
local ignore_case=0
local search_words=$_s_search_words
if ((_s_search_opt == 0 || _s_search_opt == 2)); then
if [[ -n "$ZSH_VERSION" ]]; then
local words_lower=$_s_search_words:l
elif [[ -n ${BASH_VERSINFO[0]} ]] && [[ ${BASH_VERSINFO[0]} -ge 4 ]]; then
local words_lower=${_s_search_words,,}
else
local words_lower=$(echo ${_s_search_words} | tr "[:upper:]" "[:lower:]")
fi
if [[ $words_lower = "$_s_search_words" ]]; then
local ignore_case=1
search_words=$words_lower
fi
fi
local negative=""
if ((is_selected == 1)); then
negative=";7"
fi
if ((_s_search_opt <= 1)); then
local -a words
words=($search_words)
local w
for w in "${words[@]}"; do
if ((ignore_case == 1)); then
# Similar replace can be done by GNU sed, BSD sed doesn't support ignore case option
_s_show=$(echo "$_s_show" | perl -pe "s|($w)|\\e[31${negative}m\\1\\e[0${negative}m|gi")
else
_s_show="${_s_show//$w/\\e[31${negative}m$w\\e[${negative}m}"
fi
done
else
if ((ignore_case == 1)); then
_s_show=$(echo "$_s_show" | perl -pe "s|(^$search_words)|\\e[31${negative}m\\1\\e[0${negative}m|gi")
else
_s_show="\\e[31${negative}m$search_words\\e[${negative}m${_s_show#$search_words}"
fi
fi
fi
} # }}}
_sf_printline() { # usage: _sf_printline is_selected n_line n_input {{{
local is_selected=$1
local n_line=$2
local n_input=$3
local color=""
if ((_s_v_selected[n_input] == 1)); then
color="[36m"
fi
if ((is_selected == 1)); then
if ((_s_line_highlight == 1)); then
color="${color}[7m"
else
color="${color}[4m"
fi
fi
# Change line breaks to \n (to be shown), remove the last line break, replace tab to space
_s_show=${_s_inputs[$n_input]//$'\n'/\\\\n}
_s_show=${_s_show%\\\\n}
_s_show=${_s_show//$'\t'/ }
tput cup "$n_line" 0 >/dev/tty
local n_show=$_s_cols
_s_num=""
if ((_s_nonumber == 0)); then
local nmax=$((_s_n - 1))
local num_width=${#nmax}
n_show=$((_s_cols - num_width - 2))
_sf_add_spaces _s_num $((num_width - ${#n_input})) 1
_s_num="$_s_num$n_input: "
fi
_sf_show "$is_selected" "$n_show"
if ((_s_col_n_only == 1)); then
printf "${color}${_s_num}[m${_s_show}" >/dev/tty
else
printf "${color}${_s_num}${_s_show}[m" >/dev/tty
fi
tput cup "$n_line" 0 >/dev/tty
} # }}}
_sf_print_current_line() { # print current line {{{
_sf_printline 1 $((_s_current_n - _s_n_offset + _s_ext_row)) $_s_current_n
_sf_content
} # }}}
_sf_printall() { # usage: _sf_printall [not force] {{{
# if any argument is given, check if echoed or not.
local n=$#
if ((n >= 1 && _s_is_print == 0)); then
return
fi
local lines=$_s_lines
local cols=$_s_cols
_s_lines=$(tput lines)
_s_cols=$(tput cols)
_sf_setview
if ((lines != _s_lines || cols != _s_cols)); then
_s_current_n=0
_s_n_offset=0
fi
clear >/dev/tty
# Header
_sf_print "${_s_header}"
local i
for i in $(seq 0 $((_s_max_show - 1))); do
if (((i + _s_n_offset) >= _s_n)); then break; fi
if (((i + _s_n_offset) == _s_current_n)); then
_sf_print_current_line
else
_sf_printline 0 $((i + _s_ext_row)) $((i + _s_n_offset))
fi
done
_s_is_print=0
} # }}}
_sf_print() { # {{{
printf "%b" "$*" >/dev/tty
} # }}}
_sf_echo() { # {{{
if ((_s_noshow == 1)); then
:
elif ((_s_normal_echo == 1)); then
_sf_print "$*"
else
clear >/dev/tty
_sf_print "$*"
_s_is_print=1
_sf_wait
fi
} # }}}
_sf_echoln() { # {{{
_sf_echo "$*\\n"
} # }}}
_sf_echo_debug() { # {{{
if [[ "$_s_debug" = "" ]]; then
# Temporarily use default value for debug_echo before initialization.
_s_debug=$_SENTAKU_DEBUG
fi
if ((_s_debug > 0)); then
_sf_echo "$*"
fi
} # }}}
_sf_echoln_debug() { # {{{
_sf_echo_debug "$*\\n"
} # }}}
_sf_echo_printall() { # {{{
_sf_echo "$*"
_sf_printall
} # }}}
_sf_set_header() { # {{{
_s_header="\\e[43;30m$_s_n values in total\\e[0m "
if ((_s_noheader == 1)); then
return
fi
if ((_s_delete == 1)); then
local delete_key="d(delete), "
else
local delete_key=""
fi
if ((_s_keymode == 0)); then
if ((_s_cols >= 68)); then
_s_header="$_s_header
[n]j(n-down), [n]k(n-up), gg(top), G(bottom), [n]gg/G, (go to n)
^D(Half page down), ^U(Half page up), ^F(Page down), ^B(Page Up)
${delete_key}/(search), Enter/Space(select), q(quit)"
elif ((_s_cols >= 41)); then
_s_header="$_s_header
vimike updown, e.g)j:down, k:up, gg/G
${delete_key}Enter/Space(select), q(quit)"
fi
else
if ((_s_cols >= 51)); then
_s_header="$_s_header