Skip to content

Commit 421a4cd

Browse files
fix(desktop): switch the Windows console code page only for non-ASCII paths
A Ctrl-C that ends the batch skips restoring the code page, which left the user's console in UTF-8 after every interrupted `t3` run. All-ASCII installs, the common case, now skip the switch entirely. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 9752dc5 commit 421a4cd

2 files changed

Lines changed: 27 additions & 6 deletions

File tree

‎apps/desktop/src/app/DesktopCliShim.test.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const fakeAppImage = (canMount = true) => {
5858
appImage,
5959
[
6060
"#!/bin/sh",
61-
'if [ "$1" = --appimage-extract ]; then cp -R ' + `'${image}'` + " squashfs-root; exit 0; fi",
61+
`if [ "$1" = --appimage-extract ]; then cp -R '${image}' squashfs-root; exit 0; fi`,
6262
'[ "$1" = --appimage-mount ] || exit 9',
6363
'[ -n "$APPIMAGE_EXTRACT_AND_RUN" ] && exit 8',
6464
canMount ? `echo '${image}'` : 'echo "Cannot mount AppImage"; exit 1',
@@ -167,4 +167,15 @@ describe("renderCliShim", () => {
167167
expect(bare).toEqual([]);
168168
expect(lines).toContain('set "T3CODE_CLI_PATH=C:\\Users\\José\\.t3\\bin\\t3.cmd"');
169169
});
170+
171+
it("switches the Windows console to UTF-8 only when a path needs it", () => {
172+
const ascii = renderCliShim({
173+
target: { kind: "windows", executable: "C:\\T3\\T3 Code.exe", entry: "C:\\T3\\bin.mjs" },
174+
shimPath: "C:\\Users\\me\\.t3\\bin\\t3.cmd",
175+
t3Home: "C:\\Users\\me\\.t3",
176+
});
177+
// A Ctrl-C that ends the batch would leave the console switched.
178+
expect(ascii).not.toContain("chcp");
179+
expect(ascii.split("\r\n")).toContain("exit /b %t3_exit%");
180+
});
170181
});

‎apps/desktop/src/app/DesktopCliShim.ts‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,26 +45,36 @@ export const renderCliShim = (input: {
4545
}) => {
4646
const { target } = input;
4747
if (target.kind === "windows") {
48+
const utf8 = [target.executable, target.entry, input.shimPath, input.t3Home].some((value) =>
49+
[...value].some((character) => character.codePointAt(0)! > 0x7f),
50+
);
51+
const restore = utf8 ? ["chcp %t3_codepage% >nul"] : [];
4852
return [
4953
"@echo off",
5054
`rem ${MARKER}`,
5155
// Delayed expansion would rewrite `!name!` in paths and arguments.
5256
"setlocal EnableExtensions DisableDelayedExpansion",
53-
// The file is UTF-8; read the paths below in UTF-8, then restore the console's code page.
54-
`for /f "tokens=2 delims=:." %%c in ('chcp') do set "t3_codepage=%%c"`,
55-
"chcp 65001 >nul",
57+
// The file is UTF-8. Only a non-ASCII path needs the console switched to
58+
// UTF-8 to read it; a Ctrl-C that ends the batch skips the restore, so the
59+
// switch is left out for the common all-ASCII install.
60+
...(utf8
61+
? [
62+
`for /f "tokens=2 delims=:." %%c in ('chcp') do set "t3_codepage=%%c"`,
63+
"chcp 65001 >nul",
64+
]
65+
: []),
5666
`set "T3CODE_CLI_PATH=${cmdText(input.shimPath)}"`,
5767
`if not defined T3CODE_HOME set "T3CODE_HOME=${cmdText(input.t3Home)}"`,
5868
'set "ELECTRON_RUN_AS_NODE=1"',
5969
// A goto, not a parenthesized block: "Program Files (x86)" would close the block early.
6070
`if exist ${cmdWord(target.executable)} goto run`,
61-
"chcp %t3_codepage% >nul",
71+
...restore,
6272
`echo ${MOVED} 1>&2`,
6373
"exit /b 127",
6474
":run",
6575
`${cmdWord(target.executable)} ${cmdWord(target.entry)} %*`,
6676
'set "t3_exit=%ERRORLEVEL%"',
67-
"chcp %t3_codepage% >nul",
77+
...restore,
6878
"exit /b %t3_exit%",
6979
"",
7080
].join("\r\n");

0 commit comments

Comments
 (0)