Skip to content

Commit 6333552

Browse files
authored
Merge pull request microsoft#165633 from rwe/serialize-message-zsh
shellIntegration-rc.zsh: escape values in "E" (executed command) and "P" (property KV) codes
2 parents d0564ea + 1ab5754 commit 6333552

File tree

1 file changed

+45
-12
lines changed

1 file changed

+45
-12
lines changed

src/vs/workbench/contrib/terminal/browser/media/shellIntegration-rc.zsh

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,48 +34,81 @@ if [ -z "$VSCODE_SHELL_INTEGRATION" ]; then
3434
builtin return
3535
fi
3636

37+
# The property (P) and command (E) codes embed values which require escaping.
38+
# Backslashes are doubled. Non-alphanumeric characters are converted to escaped hex.
39+
__vsc_escape_value() {
40+
builtin emulate -L zsh
41+
42+
# Process text byte by byte, not by codepoint.
43+
builtin local LC_ALL=C str="$1" i byte token out=''
44+
45+
for (( i = 0; i < ${#str}; ++i )); do
46+
byte="${str:$i:1}"
47+
48+
# Backslashes must be doubled.
49+
if [ "$byte" = "\\" ]; then
50+
token="\\\\"
51+
# Conservatively pass alphanumerics through.
52+
elif [[ "$byte" == [0-9A-Za-z] ]]; then
53+
token="$byte"
54+
# Hex-encode anything else.
55+
# (Importantly including: semicolon, newline, and control chars).
56+
else
57+
token="\\x${(l:2::0:)$(( [##16] #byte ))}"
58+
# | | | |/|/ |_____|
59+
# | | | | | |
60+
# left-pad --+ | | | | +- the byte value of the character
61+
# two digits --+ | | +------- in hexadecimal
62+
# with '0' -------+ +--------- with no prefix
63+
fi
64+
65+
out+="$token"
66+
done
67+
68+
builtin print -r "$out"
69+
}
70+
3771
__vsc_in_command_execution="1"
3872
__vsc_current_command=""
3973

4074
__vsc_prompt_start() {
41-
builtin printf "\033]633;A\007"
75+
builtin printf '\e]633;A\a'
4276
}
4377

4478
__vsc_prompt_end() {
45-
builtin printf "\033]633;B\007"
79+
builtin printf '\e]633;B\a'
4680
}
4781

4882
__vsc_update_cwd() {
49-
builtin printf "\033]633;P;Cwd=%s\007" "$PWD"
83+
builtin printf '\e]633;P;Cwd=%s\a' "$(__vsc_escape_value "${PWD}")"
5084
}
5185

5286
__vsc_command_output_start() {
53-
builtin printf "\033]633;C\007"
54-
# Send command line, escaping printf format chars %
55-
builtin printf "\033]633;E;%s\007" "$__vsc_current_command"
87+
builtin printf '\e]633;C\a'
88+
builtin printf '\e]633;E;%s\a' "$(__vsc_escape_value "${__vsc_current_command}")"
5689
}
5790

5891
__vsc_continuation_start() {
59-
builtin printf "\033]633;F\007"
92+
builtin printf '\e]633;F\a'
6093
}
6194

6295
__vsc_continuation_end() {
63-
builtin printf "\033]633;G\007"
96+
builtin printf '\e]633;G\a'
6497
}
6598

6699
__vsc_right_prompt_start() {
67-
builtin printf "\033]633;H\007"
100+
builtin printf '\e]633;H\a'
68101
}
69102

70103
__vsc_right_prompt_end() {
71-
builtin printf "\033]633;I\007"
104+
builtin printf '\e]633;I\a'
72105
}
73106

74107
__vsc_command_complete() {
75108
if [[ "$__vsc_current_command" == "" ]]; then
76-
builtin printf "\033]633;D\007"
109+
builtin printf '\e]633;D\a'
77110
else
78-
builtin printf "\033]633;D;%s\007" "$__vsc_status"
111+
builtin printf '\e]633;D;%s\a' "$__vsc_status"
79112
fi
80113
__vsc_update_cwd
81114
}

0 commit comments

Comments
 (0)