1 # Copy this file into /usr/share/zsh/site-functions/
2 # and add 'autoload n-list-draw` to .zshrc
4 # This is an internal function not for direct use
10 setopt typesetsilent extendedglob
12 _nlist_print_with_ansi() {
13 local win="$1" text="$2" out col chunk Xout
14 integer text_offset="$3" max_text_len="$4" text_len=0 no_match=0 nochunk_text_len to_skip_from_chunk to_chop_off_from_chunk before_len
16 # 1 - non-escaped text, 2 - first number in the escaped text, with ;
17 # 3 - second number, 4 - text after whole escape text
20 c=( black red green yellow blue magenta cyan white )
22 while [[ -n "$text" && "$no_match" -eq 0 ]]; do
23 if [[ "$text" = (#b)([^$'\x1b']#)$'\x1b'\[([0-9](#c0,2))(#B)(\;|)(#b)([0-9](#c0,2))m(*) ]]; then
24 # Text for further processing
26 # Text chunk to output now
30 (( match[3] >= 30 && match[3] <= 37 )) && col="$match[3]"
36 if [ -n "$out" ]; then
37 ################ Expand tabs ################
39 before_len="$text_len"
42 while [ -n "$chunk" ]; do
43 [[ "$chunk" = (#b)([^$'\t']#)$'\t'(*) ]] && {
44 (( all_text_len=((before_len+${#match[1]})/8+1)*8 ))
46 Xout+="${(r:all_text_len-before_len:: :)match[1]}"
48 before_len+=all_text_len-before_len
55 #############################################
57 # Input text length without the current chunk
58 nochunk_text_len=text_len
59 # Input text length up to current chunk
62 # Should start displaying with this chunk?
63 # I.e. stop skipping left part of the input text?
64 if (( text_len > text_offset )); then
65 to_skip_from_chunk=text_offset-nochunk_text_len
67 # LEFT - is chunk off the left skip boundary? +1 for 1-based index in string
68 (( to_skip_from_chunk > 0 )) && Xout="${Xout[to_skip_from_chunk+1,-1]}"
70 # RIGHT - is text off the screen?
71 if (( text_len-text_offset > max_text_len )); then
72 to_chop_off_from_chunk=0+(text_len-text_offset)-max_text_len
73 Xout="${Xout[1,-to_chop_off_from_chunk-1]}"
76 [ -n "$Xout" ] && zcurses string "$win" "$Xout"
80 if (( no_match == 0 )); then
81 if (( col >= 30 && col <= 37 )); then
82 zcurses attr "$win" $c[col-29]/"$background"
83 elif [[ "$col" -eq 0 ]]; then
84 zcurses attr "$win" "$colorpair"
90 integer highlight="$1"
91 integer page_height="$2"
92 integer page_width="$3"
95 local text_offset="$6"
98 integer max_text_len=page_width-x_offset
100 [[ "$bold" = "0" || "$bold" = "-bold" ]] && bold="-bold" || bold="+bold"
101 [[ "$active_text" = "underline" || "$active_text" = "reverse" ]] || local active_text="reverse"
102 # Linux has ncv 18, screen* has ncv 3 - underline won't work properly
103 (( ${terminfo[ncv]:-0} & 2 )) && active_text="reverse"
104 # FreeBSD uses TERM=xterm for newcons but doesn't actually support underline
105 [[ "$TERM" = "xterm" && -z "$DISPLAY" ]] && active_text="reverse"
107 integer max_idx=page_height
108 integer end_idx=max_idx
109 [ "$end_idx" -gt "$#" ] && end_idx="$#"
112 zcurses attr "$win" "$bold" "$colorpair"
116 for (( i=1; i<=end_idx; i++ )); do
117 zcurses move "$win" $y "$x_offset"
119 [ "$i" = "$highlight" ] && zcurses attr "$win" +"$active_text"
120 _nlist_print_with_ansi "$win" "$@[i]" "$text_offset" "$max_text_len"
121 zcurses clear "$win" eol
122 [ "$i" = "$highlight" ] && zcurses attr "$win" -"$active_text"
127 if [ "$end_idx" -lt "$max_idx" ]; then
128 zcurses move "$win" $y "$x_offset"
129 zcurses clear "$win" eol
132 zcurses attr "$win" white/black
133 # vim: set filetype=zsh: