diff --git a/src/model/threads.ts b/src/model/threads.ts index 3fc72d4..67e930b 100644 --- a/src/model/threads.ts +++ b/src/model/threads.ts @@ -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. */ diff --git a/src/ui/editor.ts b/src/ui/editor.ts index 8c09817..16db877 100644 --- a/src/ui/editor.ts +++ b/src/ui/editor.ts @@ -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 { 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", ":confirm qa"]); /** 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 { + 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 }) }; } /** diff --git a/test/editor.test.ts b/test/editor.test.ts index 82514e9..0cea3a1 100644 --- a/test/editor.test.ts +++ b/test/editor.test.ts @@ -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(); +});