Windows: fix cmd quoting, CRLF saves and Neovim's remote output

cmd strips outer quotes when a line starts with one, which broke .cmd editors with spaces in both paths. Neovim on Windows prints remote results to the console, so they ran over the screen and never reached us; give it a hidden console and read results from a file.
This commit is contained in:
Callum Alpass committed 2026-11-04 08:10:32 +11:00
1 parent df2c8e0907
commit eb5cccece1
3 files changed
+30 -7

No files matched your search

+1 -1
View File
@@ -149,7 +149,7 @@ export function rowTime(thread: OrchestrationV2ThreadShell): number {
export function projectLabel(project: OrchestrationProjectShell | undefined): string {
if (!project) return "";
return project.title || project.workspaceRoot.split("/").filter(Boolean).pop() || project.workspaceRoot;
return project.title || project.workspaceRoot.split(/[\\/]/).filter(Boolean).pop() || project.workspaceRoot;
}
/** Matches a thread by id, id prefix, or a case-insensitive title substring. */
+19 -4
View File
@@ -29,7 +29,8 @@ export function windowsWords(command: string): string[] {
export function windowsCommand(command: string, which: (program: string) => string | null = Bun.which): string[] {
const [program = "", ...args] = windowsWords(command);
const found = which(program) ?? program;
return [...(/\.(?:cmd|bat)$/i.test(found) ? ["cmd.exe", "/c"] : []), found, ...args];
// `call` keeps the line from starting with a quote, which cmd would strip along with the last one.
return [...(/\.(?:cmd|bat)$/i.test(found) ? ["cmd.exe", "/c", "call"] : []), found, ...args];
}
/** Whether this program, by name or path, is Neovim. */
@@ -52,7 +53,8 @@ export function nvimSocket(): { readonly path: string; readonly remove: () => vo
*/
export async function nvimRemote(socket: string, args: readonly string[]): Promise<string | null> {
const command = WINDOWS ? [...windowsCommand(`"${editorProgram()}"`), "--server", socket, ...args] : [shell(), "-c", `${editorProgram()} --server "$@"`, "sh", socket, ...args];
const child = Bun.spawn(command, { stdin: "ignore", stdout: "pipe", stderr: "ignore" });
// windowsHide gives it a console of its own, as Neovim on Windows prints results to the console, not stdout.
const child = Bun.spawn(command, { stdin: "ignore", stdout: "pipe", stderr: "ignore", windowsHide: true });
const [code, output] = await Promise.all([child.exited, new Response(child.stdout).text()]);
return code === 0 ? output : null;
}
@@ -63,10 +65,22 @@ export const nvimOpen = (socket: string, file: string) => nvimRemote(socket, ["-
export const nvimQuit = (socket: string) => nvimRemote(socket, ["--remote-send", "<C-\\><C-N>:confirm qa<CR>"]);
/** Whether the Neovim on `socket` has unsaved changes; null if it couldn't be reached. */
export const nvimModified = async (socket: string) => {
const count = await nvimRemote(socket, ["--remote-expr", "len(getbufinfo({'bufmodified': 1}))"]);
const count = await nvimEval(socket, "len(getbufinfo({'bufmodified': 1}))");
return count === null ? null : Number.parseInt(count, 10) > 0;
};
/** What `expr` comes to in the Neovim on `socket`. Windows can't read its output (see nvimRemote), so there it's written to a file. */
async function nvimEval(socket: string, expr: string): Promise<string | null> {
if (!WINDOWS) return nvimRemote(socket, ["--remote-expr", expr]);
const file = scratchFile("");
try {
const sent = await nvimRemote(socket, ["--remote-expr", `writefile([string(${expr})], ${vimString(file.path)})`]);
return sent === null ? null : file.read();
} finally {
file.remove();
}
}
const vimString = (text: string) => `'${text.replaceAll("'", "''")}'`;
/** A scratch copy of `text` to edit: `read` returns what was saved, `remove` cleans up. */
@@ -74,7 +88,8 @@ export function scratchFile(text: string): { readonly path: string; readonly rea
const dir = mkdtempSync(join(tmpdir(), "t3tui-"));
const path = join(dir, "prompt.md");
writeFileSync(path, text);
return { path, read: () => readFileSync(path, "utf8").replace(/\n$/, ""), remove: () => rmSync(dir, { recursive: true, force: true }) };
// Windows editors may save CRLF.
return { path, read: () => readFileSync(path, "utf8").replaceAll("\r\n", "\n").replace(/\n$/, ""), remove: () => rmSync(dir, { recursive: true, force: true }) };
}
/**
+10 -2
View File
@@ -1,6 +1,7 @@
import { expect, test } from "bun:test";
import { writeFileSync } from "node:fs";
import { isNvim, windowsCommand, windowsWords } from "../src/ui/editor.ts";
import { isNvim, scratchFile, windowsCommand, windowsWords } from "../src/ui/editor.ts";
test("splits a Windows command line on spaces outside quotes", () => {
expect(windowsWords('"C:\\Program Files\\Git\\usr\\bin\\vim.exe" -p')).toEqual(["C:\\Program Files\\Git\\usr\\bin\\vim.exe", "-p"]);
@@ -9,7 +10,7 @@ test("splits a Windows command line on spaces outside quotes", () => {
test("runs a .cmd through cmd, and anything else as it is", () => {
const which = (program: string) => ({ code: "C:\\VS Code\\bin\\code.cmd", nvim: "C:\\nvim\\nvim.exe" })[program] ?? null;
expect(windowsCommand("code --wait", which)).toEqual(["cmd.exe", "/c", "C:\\VS Code\\bin\\code.cmd", "--wait"]);
expect(windowsCommand("code --wait", which)).toEqual(["cmd.exe", "/c", "call", "C:\\VS Code\\bin\\code.cmd", "--wait"]);
expect(windowsCommand("nvim", which)).toEqual(["C:\\nvim\\nvim.exe"]);
expect(windowsCommand("missing", which)).toEqual(["missing"]);
});
@@ -20,3 +21,10 @@ test("knows Neovim by name or path", () => {
expect(isNvim("C:\\Program Files\\Neovim\\bin\\nvim.exe")).toBe(true);
expect(isNvim("vim")).toBe(false);
});
test("reads back a scratch file saved with CRLF", () => {
const file = scratchFile("");
writeFileSync(file.path, "one\r\ntwo\r\n");
expect(file.read()).toBe("one\ntwo");
file.remove();
});