n-list 14 KB

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