n-list 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. # $1, $2, ... - elements of the list
  2. # $NLIST_NONSELECTABLE_ELEMENTS - array of indexes (1-based) that cannot be selected
  3. # $REPLY is the output variable - contains index (1-based) or -1 when no selection
  4. # $reply (array) is the second part of the output - use the index (REPLY) to get selected element
  5. #
  6. # Copy this file into /usr/share/zsh/site-functions/
  7. # and add 'autoload n-list` to .zshrc
  8. #
  9. # This function outputs a list of elements that can be
  10. # navigated with keyboard. Uses curses library
  11. emulate -LR zsh
  12. setopt typesetsilent extendedglob noshortloops
  13. _nlist_has_terminfo=0
  14. zmodload zsh/curses
  15. zmodload zsh/terminfo 2>/dev/null && _nlist_has_terminfo=1
  16. trap "REPLY=-2; reply=(); return" TERM INT QUIT
  17. trap "_nlist_exit" EXIT
  18. # Drawing and input
  19. autoload n-list-draw n-list-input
  20. # Cleanup before any exit
  21. _nlist_exit() {
  22. setopt localoptions
  23. setopt extendedglob
  24. [[ "$REPLY" = -(#c0,1)[0-9]## ]] || REPLY="-1"
  25. zcurses 2>/dev/null delwin inner
  26. zcurses 2>/dev/null delwin main
  27. zcurses 2>/dev/null refresh
  28. zcurses end
  29. _nlist_alternate_screen 0
  30. _nlist_cursor_visibility 1
  31. unset _nlist_has_terminfo
  32. }
  33. # Outputs a message in the bottom of the screen
  34. _nlist_status_msg() {
  35. # -1 for border, -1 for 0-based indexing
  36. zcurses move main $(( term_height - 1 - 1 )) 2
  37. zcurses clear main eol
  38. zcurses string main "$1"
  39. #status_msg_strlen is localized in caller
  40. status_msg_strlen=$#1
  41. }
  42. # Prefer tput, then module terminfo
  43. _nlist_cursor_visibility() {
  44. if type tput 2>/dev/null 1>&2; then
  45. [ "$1" = "1" ] && { tput cvvis; tput cnorm }
  46. [ "$1" = "0" ] && tput civis
  47. elif [ "$_nlist_has_terminfo" = "1" ]; then
  48. [ "$1" = "1" ] && { [ -n $terminfo[cvvis] ] && echo -n $terminfo[cvvis];
  49. [ -n $terminfo[cnorm] ] && echo -n $terminfo[cnorm] }
  50. [ "$1" = "0" ] && [ -n $terminfo[civis] ] && echo -n $terminfo[civis]
  51. fi
  52. }
  53. # Reason for this function is that on some systems
  54. # smcup and rmcup are not knowing why left empty
  55. _nlist_alternate_screen() {
  56. [ "$_nlist_has_terminfo" -ne "1" ] && return
  57. [[ "$1" = "1" && -n "$terminfo[smcup]" ]] && return
  58. [[ "$1" = "0" && -n "$terminfo[rmcup]" ]] && return
  59. case "$TERM" in
  60. *rxvt*)
  61. [ "$1" = "1" ] && echo -n $'\x1b7\x1b[?47h'
  62. [ "$1" = "0" ] && echo -n $'\x1b[2J\x1b[?47l\x1b8'
  63. ;;
  64. *)
  65. [ "$1" = "1" ] && echo -n $'\x1b[?1049h'
  66. [ "$1" = "0" ] && echo -n $'\x1b[?1049l'
  67. # just to remember two other that work: $'\x1b7\x1b[r\x1b[?47h', $'\x1b[?47l\x1b8'
  68. ;;
  69. esac
  70. }
  71. _nlist_compute_user_vars_difference() {
  72. if [[ "${(t)NLIST_NONSELECTABLE_ELEMENTS}" != "array" &&
  73. "${(t)NLIST_NONSELECTABLE_ELEMENTS}" != "array-local" ]]
  74. then
  75. last_element_difference=0
  76. current_difference=0
  77. else
  78. last_element_difference=$#NLIST_NONSELECTABLE_ELEMENTS
  79. current_difference=0
  80. local idx
  81. for idx in "${(n)NLIST_NONSELECTABLE_ELEMENTS[@]}"; do
  82. [ "$idx" -le "$NLIST_CURRENT_IDX" ] && current_difference+=1 || break
  83. done
  84. fi
  85. }
  86. # List was processed, check if variables aren't off range
  87. _nlist_verify_vars() {
  88. [ "$NLIST_CURRENT_IDX" -gt "$last_element" ] && NLIST_CURRENT_IDX="$last_element"
  89. [[ "$NLIST_CURRENT_IDX" -eq 0 && "$last_element" -ne 0 ]] && NLIST_CURRENT_IDX=1
  90. (( NLIST_FROM_WHAT_IDX_LIST_IS_SHOWN=0+((NLIST_CURRENT_IDX-1)/page_height)*page_height+1 ))
  91. }
  92. # Compute the variables which are shown to the user
  93. _nlist_setup_user_vars() {
  94. if [ "$1" = "1" ]; then
  95. # Basic values when there are no non-selectables
  96. NLIST_USER_CURRENT_IDX="$NLIST_CURRENT_IDX"
  97. NLIST_USER_LAST_ELEMENT="$last_element"
  98. else
  99. _nlist_compute_user_vars_difference
  100. NLIST_USER_CURRENT_IDX=$(( NLIST_CURRENT_IDX - current_difference ))
  101. NLIST_USER_LAST_ELEMENT=$(( last_element - last_element_difference ))
  102. fi
  103. }
  104. _nlist_colorify_disp_list() {
  105. local col=$'\x1b[00;34m' reset=$'\x1b[0m'
  106. [ -n "$NLIST_COLORING_COLOR" ] && col="$NLIST_COLORING_COLOR"
  107. [ -n "$NLIST_COLORING_END_COLOR" ] && reset="$NLIST_COLORING_END_COLOR"
  108. if [ "$NLIST_COLORING_MATCH_MULTIPLE" -eq 1 ]; then
  109. disp_list=( "${(@)disp_list//(#mi)$~NLIST_COLORING_PATTERN/$col${MATCH}$reset}" )
  110. else
  111. disp_list=( "${(@)disp_list/(#mi)$~NLIST_COLORING_PATTERN/$col${MATCH}$reset}" )
  112. fi
  113. }
  114. #
  115. # Main code
  116. #
  117. # Check if there is proper input
  118. if [ "$#" -lt 1 ]; then
  119. echo "Usage: n-list element_1 ..."
  120. return 1
  121. fi
  122. REPLY="-1"
  123. typeset -ga reply
  124. reply=()
  125. integer term_height="$LINES"
  126. integer term_width="$COLUMNS"
  127. if [[ "$term_height" -lt 1 || "$term_width" -lt 1 ]]; then
  128. local stty_out=$( stty size )
  129. term_height="${stty_out% *}"
  130. term_width="${stty_out#* }"
  131. fi
  132. integer inner_height=term_height-3
  133. integer inner_width=term_width-3
  134. integer page_height=inner_height
  135. integer page_width=inner_width
  136. typeset -a list disp_list
  137. integer last_element=$#
  138. local action
  139. local final_key
  140. integer selection
  141. integer last_element_difference=0
  142. integer current_difference=0
  143. local prev_search_buffer=""
  144. integer prev_uniq_mode=0
  145. integer prev_start_idx=-1
  146. local MBEGIN MEND MATCH mbegin mend match
  147. # Ability to remember the list between calls
  148. if [[ -z "$NLIST_REMEMBER_STATE" || "$NLIST_REMEMBER_STATE" -eq 0 || "$NLIST_REMEMBER_STATE" -eq 2 ]]; then
  149. NLIST_FROM_WHAT_IDX_LIST_IS_SHOWN=1
  150. NLIST_CURRENT_IDX=1
  151. NLIST_IS_SEARCH_MODE=0
  152. NLIST_SEARCH_BUFFER=""
  153. NLIST_TEXT_OFFSET=0
  154. NLIST_IS_UNIQ_MODE=0
  155. # Zero - because it isn't known, unless we
  156. # confirm that first element is selectable
  157. NLIST_USER_CURRENT_IDX=0
  158. [[ ${NLIST_NONSELECTABLE_ELEMENTS[(r)1]} != 1 ]] && NLIST_USER_CURRENT_IDX=1
  159. NLIST_USER_LAST_ELEMENT=$(( last_element - $#NLIST_NONSELECTABLE_ELEMENTS ))
  160. # 2 is init once, then remember
  161. [ "$NLIST_REMEMBER_STATE" -eq 2 ] && NLIST_REMEMBER_STATE=1
  162. fi
  163. if [ "$NLIST_START_IN_SEARCH_MODE" -eq 1 ]; then
  164. NLIST_START_IN_SEARCH_MODE=0
  165. NLIST_IS_SEARCH_MODE=1
  166. fi
  167. if [ -n "$NLIST_SET_SEARCH_TO" ]; then
  168. NLIST_SEARCH_BUFFER="$NLIST_SET_SEARCH_TO"
  169. NLIST_SET_SEARCH_TO=""
  170. fi
  171. if [ "$NLIST_START_IN_UNIQ_MODE" -eq 1 ]; then
  172. NLIST_START_IN_UNIQ_MODE=0
  173. NLIST_IS_UNIQ_MODE=1
  174. fi
  175. _nlist_alternate_screen 1
  176. zcurses init
  177. zcurses delwin main 2>/dev/null
  178. zcurses delwin inner 2>/dev/null
  179. zcurses addwin main "$term_height" "$term_width" 0 0
  180. zcurses addwin inner "$inner_height" "$inner_width" 1 2
  181. zcurses bg main white/black
  182. zcurses bg inner white/black
  183. if [ "$NLIST_IS_SEARCH_MODE" -ne 1 ]; then
  184. _nlist_cursor_visibility 0
  185. fi
  186. #
  187. # Listening for input
  188. #
  189. local key keypad
  190. # Clear input buffer
  191. zcurses timeout main 0
  192. zcurses input main key keypad
  193. zcurses timeout main -1
  194. key=""
  195. keypad=""
  196. # This loop makes script faster on some Zsh's (e.g. 5.0.8)
  197. repeat 1; do
  198. list=( "$@" )
  199. done
  200. last_element="$#list"
  201. while (( 1 )); do
  202. # Do searching (filtering with string)
  203. if [ -n "$NLIST_SEARCH_BUFFER" ]; then
  204. # Compute new list?
  205. if [[ "$NLIST_SEARCH_BUFFER" != "$prev_search_buffer" || "$NLIST_IS_UNIQ_MODE" -ne "$prev_uniq_mode" ]]; then
  206. prev_search_buffer="$NLIST_SEARCH_BUFFER"
  207. prev_uniq_mode="$NLIST_IS_UNIQ_MODE"
  208. # regenerating list -> regenerating disp_list
  209. prev_start_idx=-1
  210. # Take all elements, including duplicates and non-selectables
  211. typeset +U list
  212. repeat 1; do
  213. list=( "$@" )
  214. done
  215. # Remove non-selectable elements
  216. [ "$#NLIST_NONSELECTABLE_ELEMENTS" -gt 0 ] && for i in "${(nO)NLIST_NONSELECTABLE_ELEMENTS[@]}"; do
  217. list[$i]=()
  218. done
  219. # Remove duplicates
  220. [ "$NLIST_IS_UNIQ_MODE" -eq 1 ] && typeset -U list
  221. last_element="$#list"
  222. # Next do the filtering
  223. local search_buffer="${NLIST_SEARCH_BUFFER%% ##}"
  224. search_buffer="${search_buffer## ##}"
  225. search_buffer="${search_buffer//(#m)[][*?|#~^()><\\]/\\$MATCH}"
  226. local search_pattern=""
  227. local colsearch_pattern=""
  228. if [ -n "$search_buffer" ]; then
  229. # Patterns will be *foo*~^*bar* and (foo|bar)
  230. search_pattern="${search_buffer// ##/*~^*}"
  231. colsearch_pattern="${search_buffer// ##/|}"
  232. # The repeat will make the matching work on a fresh heap
  233. repeat 1; do
  234. list=( "${(@M)list:#(#i)*$~search_pattern*}" )
  235. done
  236. last_element="$#list"
  237. fi
  238. # Called after processing list
  239. _nlist_verify_vars
  240. fi
  241. _nlist_setup_user_vars 1
  242. integer end_idx=$(( NLIST_FROM_WHAT_IDX_LIST_IS_SHOWN + page_height - 1 ))
  243. [ "$end_idx" -gt "$last_element" ] && end_idx=last_element
  244. if [ "$prev_start_idx" -ne "$NLIST_FROM_WHAT_IDX_LIST_IS_SHOWN" ]; then
  245. prev_start_idx="$NLIST_FROM_WHAT_IDX_LIST_IS_SHOWN"
  246. disp_list=( "${(@)list[NLIST_FROM_WHAT_IDX_LIST_IS_SHOWN, end_idx]}" )
  247. if [ -n "$colsearch_pattern" ]; then
  248. local red=$'\x1b[00;31m' reset=$'\x1b[00;00m'
  249. # The repeat will make the matching work on a fresh heap
  250. repeat 1; do
  251. disp_list=( "${(@)disp_list//(#mi)($~colsearch_pattern)/$red${MATCH}$reset}" )
  252. done
  253. fi
  254. # We have display list, lets replace newlines with "\n" when needed (1/2)
  255. [ "$NLIST_REPLACE_NEWLINES" -eq 1 ] && disp_list=( "${(@)disp_list//$'\n'/\\n}" )
  256. fi
  257. # Output colored list
  258. n-list-draw "$(( (NLIST_CURRENT_IDX-1) % page_height + 1 ))" \
  259. "$page_height" "$page_width" 0 0 "$NLIST_TEXT_OFFSET" inner \
  260. "$disp_list[@]"
  261. else
  262. # There is no search, but there was in previous loop
  263. # OR
  264. # Uniq mode was entered or left out
  265. # -> compute new list
  266. if [[ -n "$prev_search_buffer" || "$NLIST_IS_UNIQ_MODE" -ne "$prev_uniq_mode" ]]; then
  267. prev_search_buffer=""
  268. prev_uniq_mode="$NLIST_IS_UNIQ_MODE"
  269. # regenerating list -> regenerating disp_list
  270. prev_start_idx=-1
  271. # Take all elements, including duplicates and non-selectables
  272. typeset +U list
  273. repeat 1; do
  274. list=( "$@" )
  275. done
  276. # Remove non-selectable elements only when in uniq mode
  277. [ "$NLIST_IS_UNIQ_MODE" -eq 1 ] && [ "$#NLIST_NONSELECTABLE_ELEMENTS" -gt 0 ] &&
  278. for i in "${(nO)NLIST_NONSELECTABLE_ELEMENTS[@]}"; do
  279. list[$i]=()
  280. done
  281. # Remove duplicates when in uniq mode
  282. [ "$NLIST_IS_UNIQ_MODE" -eq 1 ] && typeset -U list
  283. last_element="$#list"
  284. # Called after processing list
  285. _nlist_verify_vars
  286. fi
  287. # "1" - shouldn't bother with non-selectables
  288. _nlist_setup_user_vars "$NLIST_IS_UNIQ_MODE"
  289. integer end_idx=$(( NLIST_FROM_WHAT_IDX_LIST_IS_SHOWN + page_height - 1 ))
  290. [ "$end_idx" -gt "$last_element" ] && end_idx=last_element
  291. if [ "$prev_start_idx" -ne "$NLIST_FROM_WHAT_IDX_LIST_IS_SHOWN" ]; then
  292. prev_start_idx="$NLIST_FROM_WHAT_IDX_LIST_IS_SHOWN"
  293. disp_list=( "${(@)list[NLIST_FROM_WHAT_IDX_LIST_IS_SHOWN, end_idx]}" )
  294. [ -n "$NLIST_COLORING_PATTERN" ] && _nlist_colorify_disp_list
  295. # We have display list, lets replace newlines with "\n" when needed (2/2)
  296. [ "$NLIST_REPLACE_NEWLINES" -eq 1 ] && disp_list=( "${(@)disp_list//$'\n'/\\n}" )
  297. fi
  298. # Output the list
  299. n-list-draw "$(( (NLIST_CURRENT_IDX-1) % page_height + 1 ))" \
  300. "$page_height" "$page_width" 0 0 "$NLIST_TEXT_OFFSET" inner \
  301. "$disp_list[@]"
  302. fi
  303. local status_msg_strlen
  304. if [ "$NLIST_IS_SEARCH_MODE" = "1" ]; then
  305. local _txt2=""
  306. [ "$NLIST_IS_UNIQ_MODE" -eq 1 ] && _txt2="[-UNIQ-] "
  307. _nlist_status_msg "${_txt2}Filtering with: ${NLIST_SEARCH_BUFFER// /+}"
  308. elif [[ ${NLIST_NONSELECTABLE_ELEMENTS[(r)$NLIST_CURRENT_IDX]} != $NLIST_CURRENT_IDX ||
  309. -n "$NLIST_SEARCH_BUFFER" || "$NLIST_IS_UNIQ_MODE" -eq 1 ]]; then
  310. local _txt="" _txt2=""
  311. [ -n "$NLIST_GREP_STRING" ] && _txt=" [$NLIST_GREP_STRING]"
  312. [ "$NLIST_IS_UNIQ_MODE" -eq 1 ] && _txt2="[-UNIQ-] "
  313. _nlist_status_msg "${_txt2}Current #$NLIST_USER_CURRENT_IDX (of #$NLIST_USER_LAST_ELEMENT entries)$_txt"
  314. else
  315. _nlist_status_msg ""
  316. fi
  317. zcurses border main
  318. local top_msg="${(C)ZSH_NAME} $ZSH_VERSION, shell level $SHLVL, $USER"
  319. zcurses move main 0 $(( term_width / 2 - $#top_msg / 2 ))
  320. zcurses string main $top_msg
  321. zcurses refresh main inner
  322. zcurses move main $(( term_height - 1 - 1 )) $(( status_msg_strlen + 2 ))
  323. # Wait for input
  324. zcurses input main key keypad
  325. # Get the special (i.e. "keypad") key or regular key
  326. if [ -n "$key" ]; then
  327. final_key="$key"
  328. elif [ -n "$keypad" ]; then
  329. final_key="$keypad"
  330. else
  331. _nlist_status_msg "Inproper input detected"
  332. zcurses refresh main inner
  333. fi
  334. n-list-input "$NLIST_CURRENT_IDX" "$NLIST_FROM_WHAT_IDX_LIST_IS_SHOWN" \
  335. "$page_height" "$page_width" "$last_element" "$NLIST_TEXT_OFFSET" \
  336. "$final_key" "$NLIST_IS_SEARCH_MODE" "$NLIST_SEARCH_BUFFER" \
  337. "$NLIST_IS_UNIQ_MODE"
  338. selection="$reply[1]"
  339. action="$reply[2]"
  340. NLIST_CURRENT_IDX="$reply[3]"
  341. NLIST_FROM_WHAT_IDX_LIST_IS_SHOWN="$reply[4]"
  342. NLIST_TEXT_OFFSET="$reply[5]"
  343. NLIST_IS_SEARCH_MODE="$reply[6]"
  344. NLIST_SEARCH_BUFFER="$reply[7]"
  345. NLIST_IS_UNIQ_MODE="$reply[8]"
  346. if [ "$action" = "SELECT" ]; then
  347. REPLY="$selection"
  348. reply=( "$list[@]" )
  349. break
  350. elif [ "$action" = "QUIT" ]; then
  351. REPLY=-1
  352. reply=( "$list[@]" )
  353. break
  354. elif [ "$action" = "REDRAW" ]; then
  355. zcurses clear main redraw
  356. zcurses clear inner redraw
  357. fi
  358. done
  359. # vim: set filetype=zsh: