1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
|
# Tests for region_highlight, true-color support, near-color support
# Version 0.7 2018-12-06
%prep
export TERM=xterm-256color
if [[ ${+termcap} != 1 || ${termcap[Co]} != <-> || ${termcap[Co]} -lt 256 ]]; then
ZTST_unimplemented="no termcap module OR termcap doesn't support 256 or more colors"
elif zmodload zsh/zpty 2> /dev/null; then
zpty_start() {
export PS1= PS2=
zpty -d
zpty zsh "${(q)ZTST_testdir}/../Src/zsh -fiV +Z"
zpty -w zsh "module_path=( ${(j< >)${(@q-)module_path}} \$module_path )"
zpty -w zsh 'zle_highlight=( fg_start_code:"CDE|3" fg_end_code:"|" bg_start_code:"BCDE|4" bg_end_code:"|" )'
zpty -w zsh '.term.extensions=( -query truecolor -bracketed-paste -integration )'
}
zpty_input() {
zpty ${${(M)2:#nonl}:+-n} -w zsh "$1"
}
zpty_enable_zle() {
zpty -w zsh "tcfunc() { REPLY=""; }"
# This line will not be echoed back, behaving like ! -o zle
zpty -w zsh "setopt zle; zle -T tc tcfunc"
}
zpty_line() {
setopt localoptions extendedglob noshwordsplit
local REPLY cm=$'\r'
integer i
for (( i = 0; i < ${1:-1}; ++i )); do
zpty -r zsh REPLY
# Normalise me/sgr0 to \e[0m. We also need to strip \e(B in zpty_stop()
REPLY=${REPLY//${termcap[me]}/$'\x1b[0m'}
# P is for "preserve", and induces keeping some
# expected color codes to test region_highlight,
# etc. - the color codes are made a regular text.
[[ "$2" = "p" ]] && {
REPLY=${REPLY//(#b)$'\x1b'\[([0-9;]##m)/${match[1]}}
} || {
REPLY=${REPLY//$'\x1b'\[[0-9;]##m/} # remove all [0-9]...m codes
}
# Fix e^Mexit - match ((?)\r(?)), if \2 == \3, then replace with \2
# otherwise replace with \1 stripped out of leading/trailing [[:space:]]
REPLY=${REPLY//(#b)((?(#c0,1))$cm(?(#c0,1)))/${${${(M)match[2]:#${match[3]}}:+${match[2]}}:-${${match[1]##[[:space:]]##}%%[[:space:]]##}}}
[[ -n "$REPLY" ]] && print -r -- ${${REPLY%%${~cm}*}##[[:space:]]##}
done
}
zpty_stop() {
setopt localoptions extendedglob
local REPLY cm=$'\r'
# To early Ctrl-D might happen, it was happening when debug
# logs were writing to a file slowing down Zle a little
LANG=C sleep 0.333
# Zle is active, can use Ctrl-D to exit
zpty -n -w zsh $'\C-d'
# zpty gives no output when piped without these braces (?)
# The while loop with // substitution is to convert `e^Mexit'
# into `exit' (see zpty_line). The sed commands remove escapes
{ zpty -r zsh } |
sed -e $'/[^\t\r ]/!d' -e $'s/\r$//' -e $'s/\x1b\\[[0-9;]*m//g' -e $'s/\x1b([A-Z]//g' |
while read REPLY; do
REPLY=${REPLY//(#b)((?(#c0,1))$cm(?(#c0,1)))/${${${(M)match[2]:#${match[3]}}:+${match[2]}}:-${${match[1]##[[:space:]]##}%%[[:space:]]##}}}
print -rn -- "$REPLY"
done
zpty -d
:
}
else
ZTST_unimplemented='the zsh/zpty module is not available'
fi
%test
zpty_start
zpty_input 'rh_widget() { BUFFER="true word2 word3"; region_highlight+=( "0 4 fg=196" ); rh2; }'
zpty_input 'rh2() { region_highlight=( "2 3 standout" ); };' # note the =, not +=
zpty_input 'zle -N rh_widget'
zpty_input 'bindkey "\C-a" rh_widget'
zpty_enable_zle
zpty_input $'\C-a' # emits newline, which executes BUFFER="true" command
zpty_line 1 p # the line of interest, preserving escapes ("p")
zpty_stop
0:region highlight - standout overlapping on other region_highlight entry
>0mtr7mu0me word2 word3
zpty_start
zpty_input 'rh_widget() { BUFFER="true"; region_highlight+=( "0 4 fg=green" ); }'
zpty_input 'zle -N rh_widget'
zpty_input 'bindkey "\C-a" rh_widget'
zpty_enable_zle
zpty_input $'\C-a' # emits newline, which executes BUFFER="true" command
zpty_line 1 p # the line of interest, preserving escapes ("p")
zpty_stop
0:basic region_highlight with 8 colors
>0mCDE|32|true
zpty_start
zpty_input 'zle-line-pre-redraw() { BUFFER=": abcde" region_highlight=( "2 5 faint,bold" ) }'
zpty_input 'zle -N zle-line-pre-redraw'
zpty_enable_zle
zpty_input $'\C-a'
zpty_line 1 p
zpty_stop
0:later highlight attribute takes precedence over earlier one that contradicts
>0m: 1mabc0mde
zpty_start
zpty_input 'zle-line-pre-redraw() { BUFFER=": abcde" region_highlight=( "2 5 faint,unrecognised,bold" ) }'
zpty_input 'zle -N zle-line-pre-redraw'
zpty_enable_zle
zpty_input $'\C-a'
zpty_line 1 p
zpty_stop
0:unrecognised highlight attribute and any subsequent ones ignored
>0m: 2mabc0mde
zpty_start
zpty_input 'zle-line-pre-redraw() { BUFFER=": abcd" region_highlight=( "2 5 bold" "3 4 nobold" ) }'
zpty_input 'zle -N zle-line-pre-redraw'
zpty_enable_zle
zpty_input $'\C-a'
zpty_line 1 p
zpty_stop
0:disable highlight attribute with "no" prefix
>0m: 1ma0mb1mc0md
zpty_start
zpty_input 'zle-line-pre-redraw() { BUFFER=": abcdefg" region_highlight=( "2 5 faint,layer=14" "3 4 bold,layer=12" ) }'
zpty_input 'zle -N zle-line-pre-redraw'
zpty_enable_zle
zpty_input $'\C-a'
zpty_line 1 p
zpty_stop
0:use layers to override precedence of ordering
>0m: 2mabc0mdefg
zpty_start
zpty_input 'rh_widget() { region_highlight+=( "0 4 fg=green memo=someplugin" ); typeset -p region_highlight }'
zpty_input 'zle -N rh_widget'
zpty_input 'bindkey "\C-a" rh_widget'
zpty_enable_zle
zpty_input $'\C-a'
zpty_line
zpty_stop
0:region_highlight memo information round trips
>typeset -a region_highlight=( '0 4 fg=green memo=someplugin' )
zpty_start
zpty_input 'rh_widget() { region_highlight+=( "0 4 fg=green memo=someplugin,futureattribute=futurevalue" ); typeset -p region_highlight }'
zpty_input 'zle -N rh_widget'
zpty_input 'bindkey "\C-a" rh_widget'
zpty_enable_zle
zpty_input $'\C-a'
zpty_line
zpty_stop
0:region_highlight memo information forward compatibility, #1
>typeset -a region_highlight=( '0 4 fg=green memo=someplugin' )
zpty_start
zpty_input 'rh_widget() { region_highlight+=( "0 4 fg=green memo=someplugin futurefifthfield" ); typeset -p region_highlight }'
zpty_input 'zle -N rh_widget'
zpty_input 'bindkey "\C-a" rh_widget'
zpty_enable_zle
zpty_input $'\C-a'
zpty_line
zpty_stop
0:region_highlight memo information forward compatibility, #2
>typeset -a region_highlight=( '0 4 fg=green memo=someplugin' )
zpty_start
zpty_input 'rh_widget() { region_highlight+=( "0 4 fg=green memo=some'$'\0''plugin" ); typeset -p region_highlight }'
zpty_input 'zle -N rh_widget'
zpty_input 'bindkey "\C-a" rh_widget'
zpty_enable_zle
zpty_input $'\C-a'
zpty_line
zpty_stop
0:region_highlight memo information forward compatibility, #3: NULs
>typeset -a region_highlight=( '0 4 fg=green memo=some' )
zpty_start
zpty_input 'rh_widget() { BUFFER="true"; region_highlight+=( "0 4 fg=#040810" ); }'
zpty_input 'zle -N rh_widget'
zpty_input 'bindkey "\C-a" rh_widget'
zpty_enable_zle
zpty_input $'\C-a' # emits newline, which executes BUFFER="true" command
zpty_line 1 p # the line of interest, preserving escapes ("p")
zpty_stop
0:basic region_highlight with true-color (hex-triplets)
>0m38;2;4;8;16mtrue
zpty_start
zpty_input 'zmodload zsh/nearcolor'
zpty_input 'rh_widget() { BUFFER="true"; region_highlight+=( "0 4 fg=#040810" ); }'
zpty_input 'zle -N rh_widget'
zpty_input 'bindkey "\C-a" rh_widget'
zpty_enable_zle
zpty_input $'\C-a' # emits newline, which executes BUFFER="true" command
zpty_line 1 p # the line of interest, preserving escapes ("p")
zpty_stop
0:basic region_highlight with near-color (hex-triplets at input)
>0mCDE|3232|true
zpty_start
zpty_input 'zmodload zsh/nearcolor'
zpty_input 'rh_widget() { BUFFER="true"; region_highlight+=( "0 4 fg=#000000,bg=#ffffff" ); }'
zpty_input 'zle -N rh_widget'
zpty_input 'bindkey "\C-a" rh_widget'
zpty_enable_zle
zpty_input $'\C-a' # emits newline, which executes BUFFER="true" command
zpty_line 1 p # the line of interest, preserving escapes ("p")
zpty_stop
0:basic region_highlight with near-color using extremes - all black and all white
>0mCDE|316|BCDE|4231|true
zpty_start
zpty_input 'rh_widget() { BUFFER="true"; region_highlight+=( "0 4 fg=green" ); rh2; }'
zpty_input 'rh2() { region_highlight+=( "1 2 fg=red" ); }' # `r' in red; the above line would be too long
zpty_input 'zle -N rh_widget'
zpty_input 'bindkey "\C-a" rh_widget'
zpty_enable_zle
zpty_input $'\C-a' # emits newline, which executes BUFFER="true" command
zpty_line 1 p # the line of interest, preserving escapes ("p")
zpty_stop
0:overlapping region_highlight with 8 colors
>0mCDE|32|tCDE|31|rCDE|32|ue
zpty_start
zpty_input 'rh_widget() { BUFFER="true"; region_highlight+=( "0 4 fg=#00cc00" ); rh2; }'
zpty_input 'rh2() { region_highlight+=( "1 2 fg=#cc0000" ); }' # `r' in red; the above line would be too long
zpty_input 'zle -N rh_widget'
zpty_input 'bindkey "\C-a" rh_widget'
zpty_enable_zle
zpty_input $'\C-a' # emits newline, which executes BUFFER="true" command
zpty_line 1 p # the line of interest, preserving escapes ("p")
zpty_stop
0:overlapping region_highlight with true-color
>0m38;2;0;204;0mt38;2;204;0;0mr38;2;0;204;0mue
zpty_start
zpty_input 'zmodload zsh/nearcolor'
zpty_input 'rh_widget() { BUFFER="true"; region_highlight+=( "0 4 fg=#00cc00" ); rh2; }'
zpty_input 'rh2() { region_highlight+=( "1 2 fg=#cc0000" ); }' # `r' in red; the above line would be too long
zpty_input 'zle -N rh_widget'
zpty_input 'bindkey "\C-a" rh_widget'
zpty_enable_zle
zpty_input $'\C-a' # emits newline, which executes BUFFER="true" command
zpty_line 1 p # the line of interest, preserving escapes ("p")
zpty_stop
0:overlapping region_highlight with near-color (hex-triplets at input)
>0mCDE|340|tCDE|3160|rCDE|340|ue
zpty_start
zpty_input 'f () { zle clear-screen; zle g -f nolast; BUFFER=": ${(q)LASTWIDGET}" }; zle -N f'
zpty_input 'g () { }; zle -N g'
zpty_input 'bindkey "\C-a" f'
zpty_enable_zle
zpty_input $'\C-a'
zpty_line 1 p
zpty_stop
0:zle $widgetname -f nolast
>0m0m: clear-screen
zpty_start
zpty_input '.term.extensions[(r)truecolor]=-truecolor'
zpty_input 'zle-line-pre-redraw() { BUFFER=": abc" region_highlight=( "2 4 fg=230" "3 5 fg=132,opacity=50%" ) }'
zpty_input 'zle -N zle-line-pre-redraw'
zpty_enable_zle
zpty_input $'\C-a'
zpty_line 1 p
zpty_stop
0:overlapping backgrounds with opacity and nearcolor
>0m: CDE|3230|aCDE|3181|bCDE|3132|c
zpty_start
zpty_input '.term.extensions[(r)truecolor]=-truecolor'
zpty_input 'zle-line-pre-redraw() { BUFFER=": abcde" region_highlight=( "3 4 fg=149,opacity=80" "5 6 fg=182,opacity=49" ) }'
zpty_input 'zle -N zle-line-pre-redraw'
zpty_enable_zle
zpty_input $'\C-a'
zpty_line 1 p
zpty_stop
0:opacity with an unknown base colour
>0m: aCDE|3149|b0mcde
zpty_start
zpty_input 'zle-line-pre-redraw() { BUFFER=": abcde" region_highlight=( "2 7 bg=#d5e7c2,fg=#00005f,bold" "4 6 bg=#e0ff50,fg=#e00020,opacity=60/40" ) }'
zpty_input 'zle -N zle-line-pre-redraw'
zpty_enable_zle
zpty_input $'\C-a'
zpty_line 1 p
zpty_stop
0:foreground and background opacity
>0m: 1m38;2;0;0;95m48;2;213;231;194mab38;2;134;0;57m48;2;217;240;148mcd38;2;0;0;95m48;2;213;231;194me
%clean
zmodload -ui zsh/zpty
|