1 # Copy this file into /usr/share/zsh/site-functions/
2 # and add 'autoload n-preview` to .zshrc
4 # This is partially a test if n-list-draw and n-list-input can be
5 # used multiple times to create multiple lists. It might become
6 # more usable if someone adds more features like previewing of
13 setopt typesetsilent extendedglob
14 trap "return" TERM INT QUIT
15 trap "_vpreview_exit" EXIT
20 [ -f ~/.config/znt/n-list.conf ] && . ~/.config/znt/n-list.conf
22 [[ "$colorpair" = "" ]] && colorpair="white/black"
23 local background="${colorpair#*/}"
26 autoload n-list-draw n-list-input
28 # Cleanup before any exit
30 zcurses 2>/dev/null delwin files
31 zcurses 2>/dev/null delwin body
32 zcurses 2>/dev/null delwin status
33 zcurses 2>/dev/null refresh
37 # Outputs a message in the bottom of the screen
38 _vpreview_status_msg() {
39 zcurses move status 1 2
40 zcurses clear status eol
41 zcurses string status "$1"
44 # Prefer tput, then module terminfo
45 _nlist_cursor_visibility() {
46 if type tput 2>/dev/null 1>&2; then
47 [ "$1" = "1" ] && tput cvvis
48 [ "$1" = "0" ] && tput civis
49 elif [ "$has_termcap" = "1" ]; then
50 [ "$1" = "1" ] && [ -n $terminfo[cvvis] ] && echo -n $terminfo[cvvis]
51 [ "$1" = "0" ] && [ -n $terminfo[civis] ] && echo -n $terminfo[civis]
59 integer term_height="$LINES"
60 integer term_width="$COLUMNS"
61 if [[ "$term_height" -lt 1 || "$term_width" -lt 1 ]]; then
62 local stty_out=$( stty size )
63 term_height="${stty_out% *}"
64 term_width="${stty_out#* }"
67 integer status_height=3
68 integer status_width=term_width
69 integer status_page_height=1
70 integer status_page_width=term_width-2
72 integer files_height=term_height-status_height
73 integer files_width=term_width/5
74 integer files_page_height=files_height-2
75 integer files_page_width=files_width-2
77 integer body_height=term_height-status_height
78 integer body_width=term_width-files_width
79 integer body_page_height=body_height-2
80 integer body_page_width=body_width
82 integer _from_what_idx_list_is_shown_1=1
85 integer _from_what_idx_list_is_shown_2=1
89 integer active_window=0
91 local ansi_mode="ansi"
92 [ -f ~/.config/znt/n-preview.conf ] && . ~/.config/znt/n-preview.conf
94 #if type pygmentize 2>/dev/null 1>&2; then
95 # hcmd=( pygmentize -g )
96 if type highlight 2>/dev/null 1>&2; then
97 hcmd=( highlight -q --force -O ansi )
98 elif type source-highlight 2>/dev/null 1>&2; then
99 # Warning: source-highlight can have problems
100 hcmd=( source-highlight --failsafe -fesc -o STDOUT -i )
106 zcurses addwin status "$status_height" "$status_width" $(( term_height - status_height )) 0
107 zcurses addwin files "$files_height" "$files_width" 0 0
108 zcurses addwin body "$body_height" "$body_width" 0 "$files_width"
109 zcurses bg status white/black
110 zcurses bg files white/black
111 zcurses bg body white/black
114 # Listening for input
120 zcurses timeout status 0
121 zcurses input status key keypad
122 zcurses timeout status -1
127 integer last_element_1
130 integer last_element_2
133 filenames=( "${(@M)filenames:#(#i)*$1*}" )
135 local NLIST_GREP_STRING="$1"
137 integer last_element_1="$#filenames"
138 integer last_element_2=0
140 local selection action final_key
144 integer end_idx=$(( _from_what_idx_list_is_shown_1 + files_page_height - 1 ))
145 [ "$end_idx" -gt "$last_element_1" ] && end_idx=last_element_1
147 n-list-draw "$(( (current_1 -1) % files_page_height + 1 ))" \
148 "$files_page_height" "$files_page_width" 1 2 0 files \
149 "${(@)filenames[_from_what_idx_list_is_shown_1, end_idx]}"
151 if [ "$#body" -ge 1 ]; then
152 end_idx=$(( _from_what_idx_list_is_shown_2 + body_page_height - 1 ))
153 [ "$end_idx" -gt "$last_element_2" ] && end_idx=last_element_2
155 n-list-draw "$(( (current_2 -1) % body_page_height + 1 ))" \
156 "$body_page_height" "$body_page_width" 1 0 "$hscroll_2" body \
157 "${(@)body[_from_what_idx_list_is_shown_2, end_idx]}"
160 [[ "$active_window" -eq 0 ]] && zcurses border files
161 zcurses border status
162 zcurses refresh files body status
165 zcurses input status key keypad
167 # Get the special (i.e. "keypad") key or regular key
168 if [ -n "$key" ]; then
170 elif [ -n "$keypad" ]; then
173 _vpreview_status_msg "Inproper input detected"
174 zcurses refresh status
177 if [ "$active_window" -eq 0 ]; then
179 n-list-input "$current_1" "$_from_what_idx_list_is_shown_1" "$files_page_height" \
180 "$files_page_width" "$last_element_1" 0 "$final_key"
182 selection="$reply[1]"
184 current_1="$reply[3]"
185 _from_what_idx_list_is_shown_1="$reply[4]"
187 if [ "$action" = "SELECT" ]; then
188 # Load new file and refresh the displaying window
189 local filename="$filenames[$selection]"
190 if [ "$ansi_mode" = "ansi" ]; then
191 body=( "${(@f)"$( "$hcmd[@]" "$filename" )"}" )
193 body=( "${(@f)"$(<$filename)"}" )
195 last_element_2="$#body"
197 _from_what_idx_list_is_shown_2=1
200 elif [ "$active_window" -eq 1 ]; then
202 n-list-input "$current_2" "$_from_what_idx_list_is_shown_2" "$body_page_height" \
203 "$body_page_width" "$last_element_2" "$hscroll_2" "$final_key"
205 selection="$reply[1]"
207 current_2="$reply[3]"
208 _from_what_idx_list_is_shown_2="$reply[4]"
209 hscroll_2="$reply[5]"
213 if [ "$action" = "LEAVE" ]; then
214 active_window=1-active_window
215 elif [ "$action" = "QUIT" ]; then
217 elif [ "$action" = "REDRAW" ]; then
218 zcurses clear files redraw
219 zcurses clear body redraw
220 zcurses clear status redraw
224 # vim: set filetype=zsh: