@@ -34,6 +34,40 @@ if [ -z "$VSCODE_SHELL_INTEGRATION" ]; then
34
34
builtin return
35
35
fi
36
36
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
+
37
71
__vsc_in_command_execution=" 1"
38
72
__vsc_current_command=" "
39
73
@@ -46,13 +80,12 @@ __vsc_prompt_end() {
46
80
}
47
81
48
82
__vsc_update_cwd () {
49
- builtin printf ' \e]633;P;Cwd=%s\a' " $PWD "
83
+ builtin printf ' \e]633;P;Cwd=%s\a' " $( __vsc_escape_value " ${ PWD} " ) "
50
84
}
51
85
52
86
__vsc_command_output_start () {
53
87
builtin printf ' \e]633;C\a'
54
- # Send command line, escaping printf format chars %
55
- builtin printf ' \e]633;E;%s\a' " $__vsc_current_command "
88
+ builtin printf ' \e]633;E;%s\a' " $( __vsc_escape_value " ${__vsc_current_command} " ) "
56
89
}
57
90
58
91
__vsc_continuation_start () {
0 commit comments