Skip to content

Commit a5056a5

Browse files
Update shell.rs
1 parent e45a9f7 commit a5056a5

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

codex-rs/core/src/shell.rs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,42 @@ impl Shell {
9090
if let Some(command_index) = command.iter().position(|arg| arg == "-Command") {
9191
let mut modified_command = command.clone();
9292
if let Some(cmd_arg) = modified_command.get_mut(command_index + 1) {
93-
// Special case: preserve stdin sentinel "-"
93+
// Special case: preserve stdin sentinel "-"
9494
if cmd_arg == "-" {
9595
// For stdin input, we can't prepend to the argument itself.
9696
// Instead, we need to handle UTF-8 encoding differently or skip it.
9797
// For now, preserve the original behavior for stdin.
9898
return Some(command);
9999
}
100-
// Prepend UTF-8 encoding setup to the command
101-
*cmd_arg = format!("[Console]::OutputEncoding = [System.Text.Encoding]::UTF8; {}", cmd_arg);
100+
101+
// Special case: if stop-parsing token detected, use -EncodedCommand
102+
if cmd_arg.trim_start().starts_with("--%") {
103+
// Encode the command with UTF-8 setup prepended
104+
let full_command = format!(
105+
"[Console]::OutputEncoding = [System.Text.Encoding]::UTF8; {}",
106+
cmd_arg
107+
);
108+
109+
// Convert to UTF-16LE and then to base64
110+
let utf16_bytes: Vec<u8> = full_command
111+
.encode_utf16()
112+
.flat_map(|c| c.to_le_bytes())
113+
.collect();
114+
115+
let encoded = base64::engine::general_purpose::STANDARD.encode(&utf16_bytes);
116+
117+
// Replace -Command with -EncodedCommand and set the encoded string
118+
modified_command[command_index] = "-EncodedCommand".to_string();
119+
*cmd_arg = encoded;
120+
121+
return Some(modified_command);
122+
}
123+
124+
// Normal case: prepend UTF-8 encoding setup to the command
125+
*cmd_arg = format!(
126+
"[Console]::OutputEncoding = [System.Text.Encoding]::UTF8; {}",
127+
cmd_arg
128+
);
102129
}
103130
Some(modified_command)
104131
} else {

0 commit comments

Comments
 (0)