| 6 | |
| 7 | export namespace Editor { |
| 8 | export async function open(opts: { value: string; renderer: CliRenderer }): Promise<string | undefined> { |
| 9 | const editor = process.env["VISUAL"] || process.env["EDITOR"] |
| 10 | if (!editor) return |
| 11 | |
| 12 | const filepath = join(tmpdir(), `${Date.now()}.md`) |
| 13 | await using _ = defer(async () => rm(filepath, { force: true })) |
| 14 | |
| 15 | await Bun.write(filepath, opts.value) |
| 16 | opts.renderer.suspend() |
| 17 | opts.renderer.currentRenderBuffer.clear() |
| 18 | const parts = editor.split(" ") |
| 19 | const proc = Bun.spawn({ |
| 20 | cmd: [...parts, filepath], |
| 21 | stdin: "inherit", |
| 22 | stdout: "inherit", |
| 23 | stderr: "inherit", |
| 24 | }) |
| 25 | await proc.exited |
| 26 | const content = await Bun.file(filepath).text() |
| 27 | opts.renderer.currentRenderBuffer.clear() |
| 28 | opts.renderer.resume() |
| 29 | opts.renderer.requestRender() |
| 30 | return content || undefined |
| 31 | } |
| 32 | } |