11# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
22# vim: ft=zsh sw=2 ts=2 et
3- #
3+
44# Draws list-box drop-down list
55local __page_height=" $1 " __page_width=" $2 " __ypos=" $3 " __xpos=" $4 " __id=" $5 " __width_var=" $6 " __idx_var=" $7 " __opts_var=" $8 " __data2=" $9 " __data2=" $1 0" __data3=" $1 1"
66
77# Get maximum width
88local -a options
99options=( " ${(@ Ps: ;: )__opts_var} " )
10+
1011# Remove color marks
1112# [all] [fg] [bg] TEXT
1213options=( " ${options[@]// (# b)([$'\03'-$'\07'$'\013'-$'\014'$'\016'-$'\031'])([$'\03'-$'\07'$'\013'-$'\014'$'\016'-$'\017']|)([$'\020'-$'\030']|)([^${ZUI[FMT_END]} ]#)${ZUI[FMT_END]} /$match [4]}" )
@@ -15,26 +16,26 @@ options=( "${options[@]//(#b)([$'\03'-$'\07'$'\013'-$'\014'$'\016'-$'\031'])([$'
1516local txt
1617integer width=7 height=10 size=" ${# options} "
1718for txt in " ${options[@]} " ; do
18- (( width = ${(m)# txt} > width ? ${(m)# txt} : width ))
19+ (( width = ${(m)# txt} > width ? ${(m)# txt} : width ))
1920done
2021
2122if (( __ypos + size + 2 > __page_height + 1 )) ; then
22- if (( size + 2 > __page_height + 1 )) ; then
23- height=__page_height-2
24- (( height <= 0 )) && height=__page_height # paranoid
25- __ypos=3
26- else
27- height=size
28- (( __ypos = __page_height - size - 1 ))
29- fi
30- else
31- (( __ypos+= 1 ))
23+ if (( size + 2 > __page_height + 1 )) ; then
24+ height=__page_height-2
25+ (( height <= 0 )) && height=__page_height # paranoid
26+ __ypos=3
27+ else
3228 height=size
29+ (( __ypos = __page_height - size - 1 ))
30+ fi
31+ else
32+ (( __ypos+= 1 ))
33+ height=size
3334fi
3435
3536if (( __xpos + width + 4 > __page_width )) ; then
36- # Basic protection, can be inaccurate
37- (( __xpos= __page_width - width - 4 ))
37+ # Basic protection, can be inaccurate
38+ (( __xpos= __page_width - width - 4 ))
3839fi
3940
4041zcurses delwin lbox 2> /dev/null
@@ -45,96 +46,95 @@ zcurses bg lbox "${ZUI[colorpair]}"
4546zcurses timeout lbox 0
4647zcurses input lbox key keypad
4748zcurses timeout lbox -1
49+
4850key=" "
4951keypad=" "
5052
5153integer hidx __start __end return_val=0 initial_idx=${(P)__idx_var}
5254
5355__start=initial_idx-height/2
5456if (( __start <= 0 )) ; then
55- __start=1
56- __end=size
57- if (( size > height )) ; then
58- __end=height
59- fi
57+ __start=1
58+ __end=size
59+ if (( size > height )) ; then
60+ __end=height
61+ fi
6062elif (( __start + height - 1 > size )) ; then
61- __start=size-height+1
62- __end=size
63+ __start=size-height+1
64+ __end=size
6365else
64- __end=initial_idx+ (height-height/2)-1
66+ __end=initial_idx+ (height-height/2)-1
6567fi
6668
6769while (( 1 )) ; do
68- # Draw list box
69- zcurses clear lbox
70-
71- integer i count=1
72- hidx=${(P)__idx_var}
73- for (( i = __start; i <= __end; ++ i )) ; do
74- txt=" ${options[i]} "
75- zcurses move lbox " $count " 2
76-
77- if (( i == hidx )) ; then
78- zcurses attr lbox +reverse
79- zcurses string lbox " $txt "
80- zcurses attr lbox -reverse
81- else
82- zcurses string lbox " $txt "
83- fi
84-
85- (( ++ count ))
86- done
87-
88- zcurses border lbox
89- zcurses refresh lbox
90-
91- # Wait for input
92- local key keypad final_key
93- zcurses input " lbox" key keypad
94-
95- # Get the special (i.e. "keypad") key or regular key
96- if [[ -n " $key " ]]; then
97- final_key=" $key "
98- elif [[ -n " $keypad " ]]; then
99- final_key=" $keypad "
100- fi
101-
102- case " $final_key " in
103- (UP|k|BTAB)
104- hidx=${(P)__idx_var}
105- (( hidx = hidx > 1 ? hidx- 1 : hidx ))
106- if (( hidx < __start )) ; then
107- __start=hidx
108- __end=__start+height-1
109- fi
110- : ${(P)__idx_var::= $hidx }
111- ;;
112- (DOWN|j|$' \t ' )
113- hidx=${(P)__idx_var}
114- (( hidx = hidx < ${# options} ? hidx+ 1 : hidx ))
115- if (( hidx > __end )) ; then
116- __end=hidx
117- __start=__end-height+1
118- fi
119- : ${(P)__idx_var::= $hidx }
120- ;;
121- ($' \E' )
122- : ${(P)__idx_var::= $initial_idx }
123- return_val=1
124- break
125- ;;
126- ($' \n ' )
127- break
128- ;;
129- (??* )
130- ;;
131- (* )
132- ;;
133- esac
70+ # Draw list box
71+ zcurses clear lbox
72+
73+ integer i count=1
74+ hidx=${(P)__idx_var}
75+ for (( i = __start; i <= __end; ++ i )) ; do
76+ txt=" ${options[i]} "
77+ zcurses move lbox " $count " 2
78+
79+ if (( i == hidx )) ; then
80+ zcurses attr lbox +reverse
81+ zcurses string lbox " $txt "
82+ zcurses attr lbox -reverse
83+ else
84+ zcurses string lbox " $txt "
85+ fi
86+
87+ (( ++ count ))
88+ done
89+
90+ zcurses border lbox
91+ zcurses refresh lbox
92+
93+ # Wait for input
94+ local key keypad final_key
95+ zcurses input " lbox" key keypad
96+
97+ # Get the special (i.e. "keypad") key or regular key
98+ if [[ -n " $key " ]]; then
99+ final_key=" $key "
100+ elif [[ -n " $keypad " ]]; then
101+ final_key=" $keypad "
102+ fi
103+
104+ case " $final_key " in
105+ (UP|k|BTAB)
106+ hidx=${(P)__idx_var}
107+ (( hidx = hidx > 1 ? hidx- 1 : hidx ))
108+ if (( hidx < __start )) ; then
109+ __start=hidx
110+ __end=__start+height-1
111+ fi
112+ : ${(P)__idx_var::= $hidx }
113+ ;;
114+ (DOWN|j|$' \t ' )
115+ hidx=${(P)__idx_var}
116+ (( hidx = hidx < ${# options} ? hidx+ 1 : hidx ))
117+ if (( hidx > __end )) ; then
118+ __end=hidx
119+ __start=__end-height+1
120+ fi
121+ : ${(P)__idx_var::= $hidx }
122+ ;;
123+ ($' \E' )
124+ : ${(P)__idx_var::= $initial_idx }
125+ return_val=1
126+ break
127+ ;;
128+ ($' \n ' )
129+ break
130+ ;;
131+ (??* )
132+ ;;
133+ (* )
134+ ;;
135+ esac
134136done
135137
136138zcurses delwin lbox 2> /dev/null
137139
138140return $return_val
139-
140- # vim:ft=zsh
0 commit comments