]> src.twobees.de Git - dotfiles.git/blob - stow/oh-my-zsh/.oh-my-zsh/plugins/zsh-navigation-tools/doc/n-preview
2d8eea3fb59516d24d6d54c2a6b4a8e5d201c792
[dotfiles.git] / stow / oh-my-zsh / .oh-my-zsh / plugins / zsh-navigation-tools / doc / n-preview
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
9 emulate -L zsh
10
11 zmodload zsh/curses
12
13 setopt typesetsilent extendedglob
14 trap "return" TERM INT QUIT
15 trap "_vpreview_exit" EXIT
16
17 local IFS="
18 "
19
20 [ -f ~/.config/znt/n-list.conf ] && . ~/.config/znt/n-list.conf
21
22 [[ "$colorpair" = "" ]] && colorpair="white/black"
23 local background="${colorpair#*/}"
24
25 # Drawing and input
26 autoload 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
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#* }"
65 fi
66
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
71
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
76
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
81
82 integer _from_what_idx_list_is_shown_1=1
83 integer current_1=1
84
85 integer _from_what_idx_list_is_shown_2=1
86 integer current_2=1
87 integer hscroll_2=0
88
89 integer active_window=0
90
91 local ansi_mode="ansi"
92 [ -f ~/.config/znt/n-preview.conf ] && . ~/.config/znt/n-preview.conf
93 typeset -a hcmd
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 )
101 else
102     ansi_mode="noansi"
103 fi
104
105 zcurses init
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
112
113 #
114 # Listening for input
115 #
116
117 local key keypad
118
119 # Clear input buffer
120 zcurses timeout status 0
121 zcurses input status key keypad
122 zcurses timeout status -1
123 key=""
124 keypad=""
125
126 typeset -a filenames
127 integer last_element_1
128
129 typeset -a body
130 integer last_element_2
131
132 filenames=( *(N) )
133 filenames=( "${(@M)filenames:#(#i)*$1*}" )
134
135 local NLIST_GREP_STRING="$1"
136
137 integer last_element_1="$#filenames"
138 integer last_element_2=0
139
140 local selection action final_key
141
142 while (( 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
222 done
223
224 # vim: set filetype=zsh: