Skip to content

Commit 5eaaf6a

Browse files
committed
refactor: improve codebase
This refactor improves the indentation structure and readability. It enhances maintainability and makes it easier to comprehend the rendering process. Signed-off-by: Salvydas Lukosius <ss-o@users.noreply.github.com>
1 parent 9f0ffdf commit 5eaaf6a

File tree

4 files changed

+209
-199
lines changed

4 files changed

+209
-199
lines changed

functions/-zui-list-box-loop

Lines changed: 91 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
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+
# shellcheck shell=bash
4+
45
# Draws list-box drop-down list
56
local __page_height="$1" __page_width="$2" __ypos="$3" __xpos="$4" __id="$5" __width_var="$6" __idx_var="$7" __opts_var="$8" __data2="$9" __data2="$10" __data3="$11"
67

78
# Get maximum width
89
local -a options
910
options=( "${(@Ps:;:)__opts_var}" )
11+
1012
# Remove color marks
1113
# [all] [fg] [bg] TEXT
1214
options=( "${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 +17,26 @@ options=( "${options[@]//(#b)([$'\03'-$'\07'$'\013'-$'\014'$'\016'-$'\031'])([$'
1517
local txt
1618
integer width=7 height=10 size="${#options}"
1719
for txt in "${options[@]}"; do
18-
(( width = ${(m)#txt} > width ? ${(m)#txt} : width ))
20+
(( width = ${(m)#txt} > width ? ${(m)#txt} : width ))
1921
done
2022

2123
if (( __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 ))
24+
if (( size + 2 > __page_height + 1 )); then
25+
height=__page_height-2
26+
(( height <= 0 )) && height=__page_height # paranoid
27+
__ypos=3
28+
else
3229
height=size
30+
(( __ypos = __page_height - size - 1 ))
31+
fi
32+
else
33+
(( __ypos+=1 ))
34+
height=size
3335
fi
3436

3537
if (( __xpos + width + 4 > __page_width )); then
36-
# Basic protection, can be inaccurate
37-
(( __xpos=__page_width - width - 4 ))
38+
# Basic protection, can be inaccurate
39+
(( __xpos=__page_width - width - 4 ))
3840
fi
3941

4042
zcurses delwin lbox 2>/dev/null
@@ -45,96 +47,95 @@ zcurses bg lbox "${ZUI[colorpair]}"
4547
zcurses timeout lbox 0
4648
zcurses input lbox key keypad
4749
zcurses timeout lbox -1
50+
4851
key=""
4952
keypad=""
5053

5154
integer hidx __start __end return_val=0 initial_idx=${(P)__idx_var}
5255

5356
__start=initial_idx-height/2
5457
if (( __start <= 0 )); then
55-
__start=1
56-
__end=size
57-
if (( size > height )); then
58-
__end=height
59-
fi
58+
__start=1
59+
__end=size
60+
if (( size > height )); then
61+
__end=height
62+
fi
6063
elif (( __start + height - 1 > size )); then
61-
__start=size-height+1
62-
__end=size
64+
__start=size-height+1
65+
__end=size
6366
else
64-
__end=initial_idx+(height-height/2)-1
67+
__end=initial_idx+(height-height/2)-1
6568
fi
6669

6770
while (( 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
71+
# Draw list box
72+
zcurses clear lbox
73+
74+
integer i count=1
75+
hidx=${(P)__idx_var}
76+
for (( i = __start; i <= __end; ++ i )); do
77+
txt="${options[i]}"
78+
zcurses move lbox "$count" 2
79+
80+
if (( i == hidx )); then
81+
zcurses attr lbox +reverse
82+
zcurses string lbox "$txt"
83+
zcurses attr lbox -reverse
84+
else
85+
zcurses string lbox "$txt"
86+
fi
87+
88+
(( ++ count ))
89+
done
90+
91+
zcurses border lbox
92+
zcurses refresh lbox
93+
94+
# Wait for input
95+
local key keypad final_key
96+
zcurses input "lbox" key keypad
97+
98+
# Get the special (i.e. "keypad") key or regular key
99+
if [[ -n "$key" ]]; then
100+
final_key="$key"
101+
elif [[ -n "$keypad" ]]; then
102+
final_key="$keypad"
103+
fi
104+
105+
case "$final_key" in
106+
(UP|k|BTAB)
107+
hidx=${(P)__idx_var}
108+
(( hidx = hidx > 1 ? hidx-1 : hidx ))
109+
if (( hidx < __start )); then
110+
__start=hidx
111+
__end=__start+height-1
112+
fi
113+
: ${(P)__idx_var::=$hidx}
114+
;;
115+
(DOWN|j|$'\t')
116+
hidx=${(P)__idx_var}
117+
(( hidx = hidx < ${#options} ? hidx+1 : hidx ))
118+
if (( hidx > __end )); then
119+
__end=hidx
120+
__start=__end-height+1
121+
fi
122+
: ${(P)__idx_var::=$hidx}
123+
;;
124+
($'\E')
125+
: ${(P)__idx_var::=$initial_idx}
126+
return_val=1
127+
break
128+
;;
129+
($'\n')
130+
break
131+
;;
132+
(??*)
133+
;;
134+
(*)
135+
;;
136+
esac
134137
done
135138

136139
zcurses delwin lbox 2>/dev/null
137140

138141
return $return_val
139-
140-
# vim:ft=zsh

0 commit comments

Comments
 (0)