-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathcore-complete.sh
10378 lines (9291 loc) · 349 KB
/
core-complete.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
ble/util/import "$_ble_base/lib/core-syntax.sh"
## @fn ble/complete/string#search-longest-suffix-in needle haystack
## @var[out] ret
function ble/complete/string#search-longest-suffix-in {
local needle=$1 haystack=$2
local l=0 u=${#needle}
while ((l<u)); do
local m=$(((l+u)/2))
if [[ $haystack == *"${needle:m}"* ]]; then
u=$m
else
l=$((m+1))
fi
done
ret=${needle:l}
}
## @fn ble/complete/string#common-suffix-prefix lhs rhs
## @var[out] ret
function ble/complete/string#common-suffix-prefix {
local lhs=$1 rhs=$2
if ((${#lhs}<${#rhs})); then
local i n=${#lhs}
for ((i=0;i<n;i++)); do
ret=${lhs:i}
[[ $rhs == "$ret"* ]] && return 0
done
ret=
else
local j m=${#rhs}
for ((j=m;j>0;j--)); do
ret=${rhs::j}
[[ $lhs == *"$ret" ]] && return 0
done
ret=
fi
}
## @fn ble/complete/string#match-patterns str patterns...
## 指定した文字列が patterns 集合の何れかのパターンに一致するか検査します。
## @param[in] str
## @param[in] patterns
## @exit
function ble/complete/string#match-patterns {
local s=$1 found= pattern; shift
for pattern; do
if [[ $s == $pattern ]]; then
return 0
fi
done
return 1
}
## @fn ble/complete/get-wordbreaks
## @var[out] wordbreaks
function ble/complete/get-wordbreaks {
wordbreaks=$_ble_term_IFS$COMP_WORDBREAKS
[[ $wordbreaks == *'('* ]] && wordbreaks=${wordbreaks//['()']}'()'
[[ $wordbreaks == *']'* ]] && wordbreaks=']'${wordbreaks//']'}
[[ $wordbreaks == *'-'* ]] && wordbreaks=${wordbreaks//'-'}'-'
}
#
#==============================================================================
# 選択インターフェイス (ble/complete/menu)
## @arr _ble_complete_menu_page_icons
##
## 各要素は以下の形式の文字列である。
##
## x0,y0,x1,y1,${#pack},${#esc1}[,bbox]:$pack$esc1
##
## * x0,y0 と x1,y1 は menu 項目の描画開始点と終了点。
## * esc1 は実際に出力する描画シーケンス。
## * bbox は "x y cols lines" の形式をしていて、描画シーケンスを生成する際に
## 使った bbox の情報を格納する。これは特に truncate が起こった時に、選択状
## 態の描画を同じ条件で実行する時に参照する。
_ble_complete_menu_items=()
_ble_complete_menu_class=
_ble_complete_menu_param=
_ble_complete_menu_page_style=
_ble_complete_menu_page_index=
_ble_complete_menu_page_offset=
_ble_complete_menu_page_icons=()
_ble_complete_menu_page_infodata=()
_ble_complete_menu_selected=-1
function ble/complete/menu#check-cancel {
((menu_iloop++%menu_interval==0)) &&
[[ :$menu_construct_opts: != *:sync:* ]] &&
ble/decode/has-input
}
## @fn ble/complete/menu-style:$menu_style/construct-page
## 候補一覧メニューの表示・配置を計算します。
##
## @var[in] menu_style
## @arr[in] menu_items
## @var[in] menu_class menu_param
## @var[in] cols lines
## @var[in,out] begin
## @var[out] end
## @var[out] x y esc
##
## @fn ble/complete/menu-style:$menu_style/guess
## scroll 番目の候補がどのページにいるかを予測します。
## 可能性のある最初のページ番号 ipage を返します。
##
## @var[in] scroll
## @var[out] ipage begin end
## @var[in] cols lines
##
_ble_complete_menu_style_hash=
_ble_complete_menu_style_measure=()
_ble_complete_menu_style_icons=()
_ble_complete_menu_style_pages=()
#
# ble/complete/menu-style:align
#
## @fn ble/complete/menu#render-item item opts
## @var[in] cols lines
## Note: "$menu_class"/render-item の中で用いる。
## @var[out] x y ret
function ble/complete/menu#render-item {
# use custom renderer
if ble/is-function "$menu_class"/render-item; then
"$menu_class"/render-item "$@"
return "$?"
fi
local item=$1 opts=$2
#g=0 lc=0 lg=0 LINES=$lines COLUMNS=$cols ble/canvas/trace "$item" truncate:ellipsis
local sgr0=$_ble_term_sgr0 sgr1=$_ble_term_rev
[[ :$opts: == *:selected:* ]] && local sgr0=$sgr1 sgr1=$sgr0
ble/canvas/trace-text "$item" nonewline:external-sgr
ret=$sgr0$ret$_ble_term_sgr0
}
## @fn ble/complete/menu#get-prefix-width format column_width
## @param[in] format
## @param[in] column_width
## @var[out] prefix_width
## @var[out] prefix_format
function ble/complete/menu#get-prefix-width {
prefix_width=0
prefix_format=${1:-$bleopt_menu_prefix}
if [[ $prefix_format ]]; then
local prefix1 column_width=$2
ble/util/sprintf prefix1 "$prefix_format" "${#menu_items[@]}"
local x1 y1 x2 y2 g=0
LINES=1 COLUMNS=$column_width x=0 y=0 ble/canvas/trace "$prefix1" truncate:measure-bbox
if ((x2<=column_width/2)); then
prefix_width=$x2
ble/string#reserve-prototype "$prefix_width"
fi
fi
}
## @fn ble/complete/menu#render-prefix index
## @param[in] index
## @param[in,opt] column_width
## @var[in] prefix_width
## @var[in] prefix_format
## @var[out] prefix_esc
function ble/complete/menu#render-prefix {
prefix_esc=
local index=$1
if ((prefix_width)); then
local prefix1; ble/util/sprintf prefix1 "$prefix_format" "$((index+1))"
local x=0 y=0 g=0
LINES=1 COLUMNS=$prefix_width ble/canvas/trace "$prefix1" truncate:relative
prefix_esc=$ret$_ble_term_sgr0
if ((x<prefix_width)); then
prefix_esc=${_ble_string_prototype::prefix_width-x}$prefix_esc
fi
fi
}
## @fn ble/complete/menu-style:align/construct/.measure-candidates-in-page
## その頁に入り切る範囲で候補の幅を計測する
## @var[in] begin
## その頁の一番最初に表示する候補を指定します。
## @var[out] end
## その頁に表示する候補の範囲の終端を返します。
## 実際には描画の際に全角文字などの文字送りによって
## ここまで表示できるとは限りません。
## @var[out] wcell
## その頁を描画する時のセル幅を返します。
## @arr[in,out] _ble_complete_menu_style_measure
## 計測結果をキャッシュしておく配列です。
##
## @var[in] lines cols menu_iloop
function ble/complete/menu-style:align/construct/.measure-candidates-in-page {
local max_wcell=$bleopt_menu_align_max; ((max_wcell>cols&&(max_wcell=cols)))
((wcell=bleopt_menu_align_min,wcell<2&&(wcell=2)))
local ncell=0 index=$begin
local item ret esc1 w
for item in "${menu_items[@]:begin}"; do
ble/complete/menu#check-cancel && return 148
local wcell_old=$wcell
# 候補の表示幅 w を計算
local w=${_ble_complete_menu_style_measure[index]%%:*}
if [[ ! $w ]]; then
local prefix_esc
ble/complete/menu#render-prefix "$index"
local x=$prefix_width y=0
ble/complete/menu#render-item "$item"; esc1=$ret
local w=$((y*cols+x))
_ble_complete_menu_style_measure[index]=$w:${#item},${#esc1}:$item$esc1$prefix_esc
fi
# wcell, ncell 更新
local wcell_request=$((w++,w<=max_wcell?w:max_wcell))
((wcell<wcell_request)) && wcell=$wcell_request
# 新しい ncell
local line_ncell=$((cols/wcell))
local cand_ncell=$(((w+wcell-1)/wcell))
if [[ $menu_style == align-nowrap ]]; then
# Note: nowrap が起こるのはすでに wcell == max_wcell の時なので、
# 改行処理が終わった後に wcell が変化するという事はない。
local x1=$((ncell%line_ncell*wcell))
local ncell_eol=$(((ncell/line_ncell+1)*line_ncell))
if ((x1>0&&x1+w>=cols)); then
# 行送り
((ncell=ncell_eol+cand_ncell))
elif ((x1+w<cols)); then
# 余白に収まる場合
((ncell+=cand_ncell))
((ncell>ncell_eol&&(ncell=ncell_eol)))
else
((ncell+=cand_ncell))
fi
else
((ncell+=cand_ncell))
fi
local max_ncell=$((line_ncell*lines))
((index&&ncell>max_ncell)) && { wcell=$wcell_old; break; }
((index++))
done
end=$index
}
## @fn ble/complete/menu-style:align/construct-page
## @var[in,out] begin end x y esc
## @arr[out] _ble_complete_menu_style_icons
##
## @var[in,out] cols lines menu_iloop
function ble/complete/menu-style:align/construct-page {
x=0 y=0 esc=
local prefix_width prefix_format
ble/complete/menu#get-prefix-width "$bleopt_menu_align_prefix" "$bleopt_menu_align_max"
local wcell=2
ble/complete/menu-style:align/construct/.measure-candidates-in-page
(($?==148)) && return 148
local ncell=$((cols/wcell))
local index=$begin entry
for entry in "${_ble_complete_menu_style_measure[@]:begin:end-begin}"; do
ble/complete/menu#check-cancel && return 148
local w=${entry%%:*}; entry=${entry#*:}
local s=${entry%%:*}; entry=${entry#*:}
local len; ble/string#split len , "$s"
local item=${entry::len[0]} esc1=${entry:len[0]:len[1]} prefix_esc=${entry:len[0]+len[1]}
local x0=$x y0=$y
if ((x==0||x+w<cols)); then
((x+=w%cols,y+=w/cols))
((y>=lines&&(x=x0,y=y0,1))) && break
else
if [[ $menu_style == align-nowrap ]]; then
((y+1>=lines)) && break
esc=$esc$'\n'
((x0=x=0,y0=++y))
((x=w%cols,y+=w/cols))
((y>=lines&&(x=x0,y=y0,1))) && break
else
((x+=prefix_width))
ble/complete/menu#render-item "$item" ||
((begin==index)) || # [Note: 少なくとも1個ははみ出ても表示する]
{ x=$x0 y=$y0; break; }; esc1=$ret
fi
fi
_ble_complete_menu_style_icons[index]=$((x0+prefix_width)),$y0,$x,$y,${#item},${#esc1}:$item$esc1
esc=$esc$prefix_esc$esc1
# 候補と候補の間の空白
if ((++index<end)); then
local icell=$((x==0?0:(x+wcell)/wcell))
if ((icell<ncell)); then
# 次の升目
local pad=$((icell*wcell-x))
ble/string#reserve-prototype "$pad"
esc=$esc${_ble_string_prototype::pad}
((x=icell*wcell))
else
# 次の行
((y+1>=lines)) && break
esc=$esc$'\n'
((x=0,++y))
fi
fi
done
end=$index
}
function ble/complete/menu-style:align-nowrap/construct-page {
ble/complete/menu-style:align/construct-page "$@"
}
#
# ble/complete/menu-style:dense
#
## @fn ble/complete/menu-style:dense/construct-page
## @var[in,out] begin end x y esc
## @var[in,out] cols lines menu_iloop
function ble/complete/menu-style:dense/construct-page {
local prefix_width prefix_format
ble/complete/menu#get-prefix-width "$bleopt_menu_dense_prefix" "$cols"
x=0 y=0 esc=
local item index=$begin N=${#menu_items[@]}
for item in "${menu_items[@]:begin}"; do
ble/complete/menu#check-cancel && return 148
local x0=$x y0=$y
local prefix_esc esc1
ble/complete/menu#render-prefix "$index"
((x+=prefix_width,x>cols&&(y+=x/cols,x%=cols)))
ble/complete/menu#render-item "$item" ||
((index==begin)) ||
{ x=$x0 y=$y0; break; }; esc1=$ret
if [[ $menu_style == dense-nowrap ]]; then
if ((y>y0&&x>0||y>y0+1)); then
((++y0>=lines)) && break
esc=$esc$'\n'
((y=y0,x0=0,x=prefix_width))
ble/complete/menu#render-item "$item" ||
((begin==index)) ||
{ x=$x0 y=$y0; break; }; esc1=$ret
fi
fi
local x1=$((x0+prefix_width)) y1=$y0
((x1>=cols)) && ((y1+=x1/cols,x1%=cols))
_ble_complete_menu_style_icons[index]=$x1,$y1,$x,$y,${#item},${#esc1}:$item$esc1
esc=$esc$prefix_esc$esc1
# 候補と候補の間の空白
if ((++index<N)); then
if [[ $menu_style == dense-nowrap ]] && ((x==0)); then
: skip
elif ((x+1<cols)); then
esc=$esc' '
((x++))
else
((y+1>=lines)) && break
esc=$esc$'\n'
((x=0,++y))
fi
fi
done
end=$index
}
## @fn ble/complete/menu-style:dense/construct opts
## complete_menu_style=align{,-nowrap} に対して候補を配置します。
function ble/complete/menu-style:dense-nowrap/construct-page {
ble/complete/menu-style:dense/construct-page "$@"
}
#
# ble/complete/menu-style:linewise
#
## @fn ble/complete/menu-style:linewise/construct-page opts
## @var[in,out] begin end x y esc
function ble/complete/menu-style:linewise/construct-page {
local opts=$1 ret
local max_icon_width=$((cols-1))
local prefix_width prefix_format
ble/complete/menu#get-prefix-width "$bleopt_menu_linewise_prefix" "$max_icon_width"
local item x0 y0 esc1 index=$begin
end=$begin x=0 y=0 esc=
for item in "${menu_items[@]:begin:lines}"; do
ble/complete/menu#check-cancel && return 148
local prefix_esc=
ble/complete/menu#render-prefix "$index" "$max_icon_width"
esc=$esc$prefix_esc
((x=prefix_width))
((x0=x,y0=y))
local lines1=1 cols1=$max_icon_width
lines=$lines1 cols=$cols1 y=0 ble/complete/menu#render-item "$item"; esc1=$ret
_ble_complete_menu_style_icons[index]=$x0,$y0,$x,$y,${#item},${#esc1},"$x0 0 $cols1 $lines1":$item$esc1
((index++))
esc=$esc$esc1
((y+1>=lines)) && break
((x=0,++y))
esc=$esc$'\n'
done
end=$index
}
function ble/complete/menu-style:linewise/guess {
((ipage=scroll/lines,
begin=ipage*lines,
end=begin))
}
#
# ble/complete/menu-style:desc
#
_ble_complete_menu_desc_pageheight=()
## @fn ble/complete/menu-style:desc/construct-page opts
## @var[in,out] begin end x y esc
## @var[in] ipage
function ble/complete/menu-style:desc/construct-page {
local opts=$1 ret
local opt_raw=; [[ $menu_style != desc-text ]] && opt_raw=1
# 失敗時・エラー時の既定値
end=$begin esc= x=0 y=0
local colsep=' | '
local desc_sgr0=$'\e[m'
ble/color/face2sgr-ansi menu_desc_quote; local desc_sgrq=$ret
ble/color/face2sgr-ansi menu_desc_type; local desc_sgrt=$ret
local ncolumn=1 nline=$lines
local nrest_item=$((${#menu_items[@]}-begin))
if [[ $bleopt_menu_desc_multicolumn_width ]]; then
ncolumn=$((cols/bleopt_menu_desc_multicolumn_width))
if ((ncolumn<1)); then
ncolumn=1
elif ((ncolumn>nrest_item)); then
ncolumn=$nrest_item
fi
fi
((nline=(${#menu_items[@]}-begin+ncolumn-1)/ncolumn,
nline>lines&&(nline=lines)))
local ncolumn_max=$(((nrest_item+nline-1)/nline))
((ncolumn>ncolumn_max&&(ncolumn=ncolumn_max)))
# Note #D1727: 相対移動の時は、右端に接すると端末による振る舞いの違
# いが問題になるので、右端に接しない様に col-1 にする。一部の端末
# については右端に接しても相対移動が壊れないと分かっているので、
# white list で右端に接する事を許可する。
local available_width=$cols
case $_ble_term_TERM in
(screen:*|tmux:*|kitty:*|contra:*) ;;
(*) ((available_width--)) ;;
esac
local wcolumn=$(((available_width-${#colsep}*(ncolumn-1))/ncolumn))
local prefix_width prefix_format
ble/complete/menu#get-prefix-width "$bleopt_menu_desc_prefix" "$wcolumn"
((wcolumn>=prefix_width+15)) || prefix_width=0
local wcand_limit=$(((wcolumn-prefix_width+1)*2/3))
((wcand_limit<10&&(wcand_limit=wcolumn-prefix_width)))
local -a DRAW_BUFF=()
local index=$begin icolumn ymax=0
for ((icolumn=0;icolumn<ncolumn;icolumn++)); do
# 各候補を描画して幅を計算する
local measure; measure=()
local pack w esc1 max_width=0
for pack in "${menu_items[@]:index:nline}"; do
ble/complete/menu#check-cancel && return 148
x=0 y=0
lines=1 cols=$wcand_limit ble/complete/menu#render-item "$pack"; esc1=$ret
((w=y*wcand_limit+x,w>max_width&&(max_width=w)))
ble/array#push measure "$w:${#pack}:$pack$esc1"
done
local cand_width=$max_width
local desc_x=$((prefix_width+cand_width+1)); ((desc_x>wcolumn&&(desc_x=wcolumn)))
local desc_prefix=; ((wcolumn-prefix_width-desc_x>30)) && desc_prefix=': '
local xcolumn=$((icolumn*(wcolumn+${#colsep})))
x=0 y=0
local entry w s pack esc1 x0 y0 pad
for entry in "${measure[@]}"; do
ble/complete/menu#check-cancel && return 148
w=${entry%%:*} entry=${entry#*:}
s=${entry%%:*} entry=${entry#*:}
pack=${entry::s} esc1=${entry:s}
local prefix_esc
ble/complete/menu#render-prefix "$index"
ble/canvas/put.draw "$prefix_esc"
((x+=prefix_width))
# 候補表示
((x0=x,y0=y,x+=w))
_ble_complete_menu_style_icons[index]=$((xcolumn+x0)),$y0,$((xcolumn+x)),$y,${#pack},${#esc1},"0 0 $wcand_limit 1":$pack$esc1
((index++))
ble/canvas/put.draw "$esc1"
# 余白
ble/canvas/put-spaces.draw "$((pad=desc_x-x))"
ble/canvas/put.draw "$desc_prefix"
((x+=pad+${#desc_prefix}))
# 説明表示
local desc=$desc_sgrt'(no description)'$desc_sgr0
ble/function#try "$menu_class"/get-desc "$pack"
if [[ $opt_raw ]]; then
y=0 g=0 lc=0 lg=0 LINES=1 COLUMNS=$wcolumn ble/canvas/trace.draw "$desc" truncate:relative:ellipsis:face0=menu_desc_default
else
ble/color/face2sgr menu_desc_default
ble/canvas/put.draw "$ret"
y=0 lines=1 cols=$wcolumn ble/canvas/trace-text "$desc" nonewline
ble/canvas/put.draw "$ret"
fi
ble/canvas/put.draw "$_ble_term_sgr0"
((y+1>=nline)) && break
ble/canvas/put-move.draw "$((-x))" 1
((x=0,++y))
done
((y>ymax)) && ymax=$y
if ((icolumn+1<ncolumn)); then
# カラム仕切りを出力 (最後に次のカラムの先頭に移動)
ble/canvas/put-move.draw "$((wcolumn-x))" "$((-y))"
for ((y=0;y<=ymax;y++)); do
ble/canvas/put.draw "$colsep"
if ((y<ymax)); then
ble/canvas/put-move.draw -${#colsep} 1
else
ble/canvas/put-move-y.draw "$((-y))"
fi
done
else
((y<ymax)) && ble/canvas/put-move-y.draw "$((ymax-y))"
((x+=xcolumn,y=ymax))
fi
done
_ble_complete_menu_desc_pageheight[ipage]=$nline
end=$index
ble/canvas/sflush.draw -v esc
}
function ble/complete/menu-style:desc/guess {
local ncolumn=1
if [[ $bleopt_menu_desc_multicolumn_width ]]; then
ncolumn=$((cols/bleopt_menu_desc_multicolumn_width))
((ncolumn<1)) && ncolumn=1
fi
local nitem_per_page=$((ncolumn*lines))
((ipage=scroll/nitem_per_page,
begin=ipage*nitem_per_page,
end=begin))
}
function ble/complete/menu-style:desc/locate {
local type=$1 osel=$2
local ipage=$_ble_complete_menu_page_index
local nline=${_ble_complete_menu_desc_pageheight[ipage]:-1}
case $type in
(right) ((ret=osel+nline)) ;;
(left) ((ret=osel-nline)) ;;
(down) ((ret=osel+1)) ;;
(up) ((ret=osel-1)) ;;
(*) return 1 ;;
esac
local beg=$_ble_complete_menu_page_offset
local end=$((beg+${#_ble_complete_menu_page_icons[@]}))
if ((ret<beg)); then
((ret=beg-1))
elif ((ret>end)); then
((ret=end))
fi
return 0
}
function ble/complete/menu-style:desc-text/construct-page { ble/complete/menu-style:desc/construct-page "$@"; }
function ble/complete/menu-style:desc-text/guess { ble/complete/menu-style:desc/guess; }
function ble/complete/menu-style:desc-text/locate { ble/complete/menu-style:desc/locate "$@"; }
# Obsolete menu_style (now synonym to "desc")
function ble/complete/menu-style:desc-raw/construct-page { ble/complete/menu-style:desc/construct-page "$@"; }
function ble/complete/menu-style:desc-raw/guess { ble/complete/menu-style:desc/guess; }
function ble/complete/menu-style:desc-raw/locate { ble/complete/menu-style:desc/locate "$@"; }
## @fn ble/complete/menu#construct/.initialize-size
## @var[out] cols lines
function ble/complete/menu#construct/.initialize-size {
ble/edit/info/.initialize-size
local maxlines=$((bleopt_complete_menu_maxlines))
((maxlines>0&&lines>maxlines)) && lines=$maxlines
}
## @fn ble/complete/menu#construct opts
## 実装分離の adapter 部分
##
## @param[in] opts
## A colon-separated list of options.
##
## @opt scroll=INT
## This option is used to select the page that contains the item
## specified by the index INT.
##
## @opt sync
## Do not cancel menu preparation on the user input. Note: This is
## implemented through ble/complete/menu#check-cancel.
##
## @var[in] menu_style
##
## @arr[in] menu_items
## 項目のリストを指定します。
##
## @var[in] menu_class menu_param
## 以下に掲げる様々な callback を呼び出す為の変数です。
##
## @fn[in,opt] $menu_class/render-item item opts
## 各項目に対応する描画内容を決定する renderer 関数を指定します。
## @param[in] item
## 描画される項目を指定します。
## @param[in] opts
## selected
## 選択されている項目の描画を行う事を示します。
## @var[in] lines cols
## 描画範囲の行数と列数を指定します。
## @var[in,out] x y
## 描画開始位置を指定します。終了位置を返します。
## @var[out] ret
## 描画に用いるシーケンスを返します。
##
## @fn[in,opt] $menu_class/onselect nsel osel
## 項目が選択された時に呼び出される callback を指定します。
## @param[in] nsel osel
##
## @fn[in,opt] $menu_class/get-desc item
## 項目の説明を取得します。
## @param[out] desc
##
## @fn[in,opt] $menu_class/onaccept nsel [item]
## @fn[in,opt] $menu_class/oncancel nsel
##
function ble/complete/menu#construct {
local menu_construct_opts=$1
local menu_iloop=0
local menu_interval=$bleopt_complete_polling_cycle
_ble_complete_menu_items=("${menu_items[@]}")
_ble_complete_menu_class=$menu_class
_ble_complete_menu_param=$menu_param
_ble_complete_menu_selected=-1
local nitem=${#menu_items[@]}
if [[ :$menu_construct_opts: == *:hidden:* ]]; then
ble/array#reserve-prototype "$nitem"
_ble_complete_menu_page_style=
_ble_complete_menu_page_index=
_ble_complete_menu_page_offset=
_ble_complete_menu_page_icons=("${_ble_array_prototype[@]::nitem}")
_ble_complete_menu_page_infodata=(store 0 0 '')
return 0
elif ((nitem==0)); then
# 項目がない時の特別表示
_ble_complete_menu_page_style=
_ble_complete_menu_page_index=0
_ble_complete_menu_page_offset=0
_ble_complete_menu_page_icons=()
_ble_complete_menu_page_infodata=(ansi $'\e[38;5;242m(no items)\e[m')
return 0
fi
local cols lines
ble/complete/menu#construct/.initialize-size
local hash=$nitem,$lines,$cols:$menu_style
# 表示したい項目の指定
local scroll=0 rex=':scroll=([0-9]+):' use_cache=
if [[ :$menu_construct_opts: =~ $rex ]]; then
scroll=${BASH_REMATCH[1]}
((nitem&&(scroll%=nitem)))
[[ $hash == "$_ble_complete_menu_style_hash" ]] && use_cache=1
fi
if [[ ! $use_cache ]]; then
_ble_complete_menu_style_measure=()
_ble_complete_menu_style_icons=()
_ble_complete_menu_style_pages=()
fi
_ble_complete_menu_style_hash=$hash
local begin=0 end=0 ipage=0 x y esc
ble/function#try ble/complete/menu-style:"$menu_style"/guess
while ((end<nitem)); do
((scroll<begin)) && return 1
local page_data=${_ble_complete_menu_style_pages[ipage]}
if [[ $page_data ]]; then
# キャッシュがある時はキャッシュから読み取り
local fields; ble/string#split fields , "${page_data%%:*}"
begin=${fields[0]} end=${fields[1]}
if ((begin<=scroll&&scroll<end)); then
x=${fields[2]} y=${fields[3]} esc=${page_data#*:}
break
fi
else
# キャッシュがない時は頁を構築
ble/complete/menu-style:"$menu_style"/construct-page "$menu_construct_opts" || return "$?"
_ble_complete_menu_style_pages[ipage]=$begin,$end,$x,$y:$esc
((begin<=scroll&&scroll<end)) && break
fi
begin=$end
((ipage++))
done
_ble_complete_menu_page_style=$menu_style
_ble_complete_menu_page_index=$ipage
_ble_complete_menu_page_offset=$begin
_ble_complete_menu_page_icons=("${_ble_complete_menu_style_icons[@]:begin:end-begin}")
_ble_complete_menu_page_infodata=(store "$x" "$y" "$esc")
return 0
}
function ble/complete/menu#show {
ble/edit/info/immediate-show "${_ble_complete_menu_page_infodata[@]}"
}
function ble/complete/menu#clear {
ble/edit/info/default
}
## @fn ble/complete/menu#select/.erase-item-selection.draw i
## @param[in] i
## Index of the item in the current page.
##
## @var[in] infoy
## @var[in] _ble_complete_menu_page_icons
## @var[in] _ble_canvas_panel_height
## @var[in] _ble_edit_info_panel
## @var[out] _ble_canvas_x _ble_canvas_y
function ble/complete/menu#select/.erase-item-selection.draw {
local i=$1
local entry=${_ble_complete_menu_page_icons[i]}
[[ $entry ]] || return 1
local fields text=${entry#*:}
ble/string#split fields , "${entry%%:*}"
((fields[3]<_ble_canvas_panel_height[_ble_edit_info_panel])) || return 1
# Note: 編集文字列の内容の変化により info panel が削れている事がある。
# 現在の項目がちゃんと info panel の中にある時にだけ描画する。(#D0880)
ble/canvas/panel#goto.draw "$_ble_edit_info_panel" "${fields[@]::2}"
ble/canvas/put.draw "${text:fields[4]}"
_ble_canvas_x=${fields[2]} _ble_canvas_y=$((infoy+fields[3]))
}
## @fn ble/complete/menu#select/.render-item-selection.draw i
## @param[in] i
## Index of the item in the current page.
##
## @var[in] infoy
## @var[in] _ble_complete_menu_page_icons
## @var[in] _ble_canvas_panel_height
## @var[in] _ble_edit_info_panel
## @var[out] _ble_canvas_x _ble_canvas_y
##
## @exit 1
## This means that the selected item is not rendered (not registered in
## _ble_complete_menu_page_icons) because the item is hidden.
## @exit 12
## Request re-arrangement of the menu items. This is caused when the item
## is not in the original page due to the size change of the info panel.
function ble/complete/menu#select/.render-item-selection.draw {
local i=$1
local entry=${_ble_complete_menu_page_icons[i]}
[[ $entry ]] || return 1
local fields text=${entry#*:}
ble/string#split fields , "${entry%%:*}"
local x=${fields[0]} y=${fields[1]}
local item=${text::fields[4]}
# construct reverted candidate
local ret
if [[ ${fields[6]} ]]; then
local box cols lines
ble/string#split-words box "${fields[6]}"
x=${box[0]} y=${box[1]} cols=${box[2]} lines=${box[3]}
ble/complete/menu#render-item "$item" selected
((x+=fields[0]-box[0]))
((y+=fields[1]-box[1]))
else
local cols lines
ble/complete/menu#construct/.initialize-size
ble/complete/menu#render-item "$item" selected
fi
# Note: 編集文字列の内容の変化により info panel が削れている事がある。
# 現在の項目がちゃんと info panel の中にある時にだけ描画する。(#D0880)
((y<_ble_canvas_panel_height[_ble_edit_info_panel])) || return 12
ble/canvas/panel#goto.draw "$_ble_edit_info_panel" "${fields[@]::2}"
ble/canvas/put.draw "$ret"
_ble_canvas_x=$x _ble_canvas_y=$((infoy+y))
}
## @fn ble/complete/menu#select index [opts]
## @param[in] opts
## goto-page-top
## 指定した項目を含む頁に移動した後に、
## その頁の一番上の項目に移動する事を指定します。
function ble/complete/menu#select {
local menu_class=$_ble_complete_menu_class
local menu_param=$_ble_complete_menu_param
local osel=$_ble_complete_menu_selected nsel=$1 opts=$2
local ncand=${#_ble_complete_menu_items[@]}
((0<=osel&&osel<ncand)) || osel=-1
((0<=nsel&&nsel<ncand)) || nsel=-1
((osel==nsel)) && return 0
local infox infoy
ble/canvas/panel#get-origin "$_ble_edit_info_panel" --prefix=info
# ページ更新
local visible_beg=0
local visible_end=$ncand
if [[ :$_ble_complete_menu_opts: != *:hidden:* ]]; then
visible_beg=$_ble_complete_menu_page_offset
visible_end=$((visible_beg+${#_ble_complete_menu_page_icons[@]}))
if ((nsel>=0&&!(visible_beg<=nsel&&nsel<visible_end))); then
ble/complete/menu/show scroll="$nsel"; local ext=$?
((ext)) && return "$ext"
if [[ $_ble_complete_menu_page_index ]]; then
local ipage=$_ble_complete_menu_page_index
ble/term/visible-bell "menu: Page $((ipage+1))" persistent
else
ble/term/visible-bell "menu: Offset $_ble_complete_menu_page_offset/$ncand" persistent
fi
visible_beg=$_ble_complete_menu_page_offset
visible_end=$((visible_beg+${#_ble_complete_menu_page_icons[@]}))
# スクロールに対応していない menu_style や、スクロールしすぎた時の為。
((visible_end<=nsel&&(nsel=visible_end-1)))
((nsel<=visible_beg&&(nsel=visible_beg)))
((visible_beg<=osel&&osel<visible_end)) || osel=-1
fi
fi
local -a DRAW_BUFF=()
local ret; ble/canvas/panel/save-position; local pos0=$ret
if ((osel>=0)); then
ble/complete/menu#select/.erase-item-selection.draw "$((osel-visible_beg))"
fi
local value=
if ((nsel>=0)); then
[[ :$opts: == *:goto-page-top:* ]] && nsel=$visible_beg
ble/complete/menu#select/.render-item-selection.draw "$((nsel-visible_beg))"
_ble_complete_menu_selected=$nsel
else
_ble_complete_menu_selected=-1
value=$_ble_complete_menu_original
fi
ble/canvas/panel/load-position.draw "$pos0"
ble/canvas/bflush.draw
ble/function#try "$menu_class"/onselect "$nsel" "$osel"
return 0
}
# widgets
function ble/widget/menu/forward {
local opts=$1
local nsel=$((_ble_complete_menu_selected+1))
local ncand=${#_ble_complete_menu_items[@]}
if ((nsel>=ncand)); then
if [[ :$opts: == *:cyclic:* ]] && ((ncand>=2)); then
nsel=0
else
ble/widget/.bell "menu: no more candidates"
return 1
fi
fi
ble/complete/menu#select "$nsel"
}
function ble/widget/menu/backward {
local opts=$1
local nsel=$((_ble_complete_menu_selected-1))
if ((nsel<0)); then
local ncand=${#_ble_complete_menu_items[@]}
if [[ :$opts: == *:cyclic:* ]] && ((ncand>=2)); then
((nsel=ncand-1))
else
ble/widget/.bell "menu: no more candidates"
return 1
fi
fi
ble/complete/menu#select "$nsel"
}
function ble/widget/menu/forward-column {
local osel=$((_ble_complete_menu_selected))
if local ret; ble/function#try ble/complete/menu-style:"$_ble_complete_menu_page_style"/locate right "$osel"; then
local nsel=$ret ncand=${#_ble_complete_menu_items[@]}
if ((0<=nsel&&nsel<ncand&&nsel!=osel)); then
ble/complete/menu#select "$nsel"
else
ble/widget/.bell "menu: no more candidates"
fi
else
ble/widget/menu/forward
fi
}
function ble/widget/menu/backward-column {
local osel=$((_ble_complete_menu_selected))
if local ret; ble/function#try ble/complete/menu-style:"$_ble_complete_menu_page_style"/locate left "$osel"; then
local nsel=$ret ncand=${#_ble_complete_menu_items[@]}
if ((0<=nsel&&nsel<ncand&&nsel!=osel)); then
ble/complete/menu#select "$nsel"
else
ble/widget/.bell "menu: no more candidates"
fi
else
ble/widget/menu/backward
fi
}
_ble_complete_menu_lastcolumn=
## @fn ble/widget/menu/.check-last-column
## @var[in,out] ox
function ble/widget/menu/.check-last-column {
if [[ $_ble_complete_menu_lastcolumn ]]; then
local lastwidget=${LASTWIDGET%%' '*}
if [[ $lastwidget == ble/widget/menu/forward-line ||
$lastwidget == ble/widget/menu/backward-line ]]
then
ox=$_ble_complete_menu_lastcolumn
return 0
fi
fi
_ble_complete_menu_lastcolumn=$ox
}
## @fn ble/widget/menu/.goto-column column
## 現在行の中で指定した列に対応する要素に移動する。
## @param[in] column
function ble/widget/menu/.goto-column {
local column=$1
local offset=$_ble_complete_menu_page_offset
local osel=$_ble_complete_menu_selected
((osel>=0)) || return 1
local entry=${_ble_complete_menu_page_icons[osel-offset]}
local fields; ble/string#split fields , "${entry%%:*}"
local ox=${fields[0]} oy=${fields[1]}
local nsel=-1
if ((ox<column)); then
# forward search within the line
nsel=$osel
for entry in "${_ble_complete_menu_page_icons[@]:osel+1-offset}"; do
ble/string#split fields , "${entry%%:*}"
local x=${fields[0]} y=${fields[1]}
((y==oy&&x<=column)) || break
((nsel++))
done
elif ((ox>column)); then
# backward search within the line
local i=$osel
while ((--i>=offset)); do
entry=${_ble_complete_menu_page_icons[i-offset]}
ble/string#split fields , "${entry%%:*}"
local x=${fields[0]} y=${fields[1]}
((y<oy||x<=column&&(nsel=i,1))) && break