1# Copy this file into /usr/share/zsh/site-functions/ 2# and add 'autoload n-preview` to .zshrc 3# 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 7# archive contents. 8 9emulate -L zsh 10 11zmodload zsh/curses 12 13setopt typesetsilent extendedglob 14trap "return" TERM INT QUIT 15trap "_vpreview_exit" EXIT 16 17local IFS=" 18" 19 20[ -f ~/.config/znt/n-list.conf ] && . ~/.config/znt/n-list.conf 21 22[[ "$colorpair" = "" ]] && colorpair="white/black" 23local background="${colorpair#*/}" 24 25# Drawing and input 26autoload n-list-draw n-list-input 27 28# Cleanup before any exit 29_vpreview_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 34 zcurses end 35} 36 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" 42} 43 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] 52 fi 53} 54 55# 56# Main code 57# 58 59integer term_height="$LINES" 60integer term_width="$COLUMNS" 61if [[ "$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#* }" 65fi 66 67integer status_height=3 68integer status_width=term_width 69integer status_page_height=1 70integer status_page_width=term_width-2 71 72integer files_height=term_height-status_height 73integer files_width=term_width/5 74integer files_page_height=files_height-2 75integer files_page_width=files_width-2 76 77integer body_height=term_height-status_height 78integer body_width=term_width-files_width 79integer body_page_height=body_height-2 80integer body_page_width=body_width 81 82integer _from_what_idx_list_is_shown_1=1 83integer current_1=1 84 85integer _from_what_idx_list_is_shown_2=1 86integer current_2=1 87integer hscroll_2=0 88 89integer active_window=0 90 91local ansi_mode="ansi" 92[ -f ~/.config/znt/n-preview.conf ] && . ~/.config/znt/n-preview.conf 93typeset -a hcmd 94#if type pygmentize 2>/dev/null 1>&2; then 95# hcmd=( pygmentize -g ) 96if type highlight 2>/dev/null 1>&2; then 97 hcmd=( highlight -q --force -O ansi ) 98elif 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 ) 101else 102 ansi_mode="noansi" 103fi 104 105zcurses init 106zcurses addwin status "$status_height" "$status_width" $(( term_height - status_height )) 0 107zcurses addwin files "$files_height" "$files_width" 0 0 108zcurses addwin body "$body_height" "$body_width" 0 "$files_width" 109zcurses bg status white/black 110zcurses bg files white/black 111zcurses bg body white/black 112 113# 114# Listening for input 115# 116 117local key keypad 118 119# Clear input buffer 120zcurses timeout status 0 121zcurses input status key keypad 122zcurses timeout status -1 123key="" 124keypad="" 125 126typeset -a filenames 127integer last_element_1 128 129typeset -a body 130integer last_element_2 131 132filenames=( *(N) ) 133filenames=( "${(@M)filenames:#(#i)*$1*}" ) 134 135local NLIST_GREP_STRING="$1" 136 137integer last_element_1="$#filenames" 138integer last_element_2=0 139 140local selection action final_key 141 142while (( 1 )); do 143 # Output the lists 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 146 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]}" 150 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 154 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]}" 158 fi 159 160 [[ "$active_window" -eq 0 ]] && zcurses border files 161 zcurses border status 162 zcurses refresh files body status 163 164 # Wait for input 165 zcurses input status key keypad 166 167 # Get the special (i.e. "keypad") key or regular key 168 if [ -n "$key" ]; then 169 final_key="$key" 170 elif [ -n "$keypad" ]; then 171 final_key="$keypad" 172 else 173 _vpreview_status_msg "Inproper input detected" 174 zcurses refresh status 175 fi 176 177 if [ "$active_window" -eq 0 ]; then 178 zcurses clear files 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" 181 182 selection="$reply[1]" 183 action="$reply[2]" 184 current_1="$reply[3]" 185 _from_what_idx_list_is_shown_1="$reply[4]" 186 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" )"}" ) 192 else 193 body=( "${(@f)"$(<$filename)"}" ) 194 fi 195 last_element_2="$#body" 196 current_2=1 197 _from_what_idx_list_is_shown_2=1 198 zcurses clear body 199 fi 200 elif [ "$active_window" -eq 1 ]; then 201 zcurses clear body 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" 204 205 selection="$reply[1]" 206 action="$reply[2]" 207 current_2="$reply[3]" 208 _from_what_idx_list_is_shown_2="$reply[4]" 209 hscroll_2="$reply[5]" 210 211 fi 212 213 if [ "$action" = "LEAVE" ]; then 214 active_window=1-active_window 215 elif [ "$action" = "QUIT" ]; then 216 break 217 elif [ "$action" = "REDRAW" ]; then 218 zcurses clear files redraw 219 zcurses clear body redraw 220 zcurses clear status redraw 221 fi 222done 223 224# vim: set filetype=zsh: 225