From 37916f6c49fa799b5f5ab1fd073368dc9f0e580b Mon Sep 17 00:00:00 2001 From: Mplan Date: Tue, 9 Jun 2026 17:24:07 +0800 Subject: [PATCH] feat(cli): use external editor for commit message editing --- index.ts | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/index.ts b/index.ts index 07ecbc6..ca8b14b 100644 --- a/index.ts +++ b/index.ts @@ -1,6 +1,9 @@ #!/usr/bin/env bun import * as readline from "node:readline"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { unlink } from "node:fs/promises"; import { loadConfig, saveConfig } from "./src/config"; import { isGitRepo, @@ -105,10 +108,38 @@ async function confirmCommit(message: string): Promise<"y" | "n" | "e"> { } async function editMessage(current: string): Promise { - console.log(` Current: ${DIM}${current}${RESET}`); - console.log(` Enter new message (empty to abort):`); - const newMsg = await ask(" > "); - return newMsg || null; + const tmpPath = join(tmpdir(), `gai-msg-${Date.now()}`); + + const header = `# Edit commit message below. Save and close to confirm.\n# Delete all lines to abort.\n`; + await Bun.write(tmpPath, header + current); + + const editor = + process.env.VISUAL || + process.env.EDITOR || + (process.platform === "darwin" ? "vi" : "nano"); + + const proc = Bun.spawn([editor, tmpPath], { + stdout: "inherit", + stderr: "inherit", + stdin: "inherit", + }); + + const exitCode = await proc.exited; + if (exitCode !== 0) { + await unlink(tmpPath).catch(() => {}); + return null; + } + + const content = await Bun.file(tmpPath).text(); + await unlink(tmpPath).catch(() => {}); + + const lines = content + .split("\n") + .filter((line) => !line.startsWith("#")) + .join("\n") + .trim(); + + return lines || null; } async function main() {