Implement edit and exit
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
import {Command, ParseContext} from "./command.js";
|
||||
import {Command, ParseContext, wrapString} from "./command.js";
|
||||
import {LexInput} from "../lexer.js";
|
||||
import {StrVM} from "../vm.js";
|
||||
import {Awaitable} from "../../util/types.js";
|
||||
import fs from "node:fs/promises";
|
||||
import childProcess from "node:child_process";
|
||||
import {tempFile} from "../../util/fs.js";
|
||||
|
||||
export class Edit extends Command<{}> {
|
||||
isParseCandidate(token: LexInput): boolean {
|
||||
@@ -19,8 +22,17 @@ export class Edit extends Command<{}> {
|
||||
execute(vm: StrVM, data: {}): Awaitable<StrVM> {
|
||||
return vm.replaceContextMatchingTerm({
|
||||
string: async sub => {
|
||||
await vm.control$.next({ cmd: 'close-prompt' })
|
||||
|
||||
return sub
|
||||
const tmp = tempFile()
|
||||
await fs.writeFile(tmp, sub, 'utf8')
|
||||
const proc = childProcess.spawn(process.env.EDITOR || 'vim', [tmp], { stdio: 'inherit' })
|
||||
await new Promise<void>(res => {
|
||||
proc.on('close', () => res())
|
||||
})
|
||||
|
||||
await vm.control$.next({ cmd: 'restore-prompt' })
|
||||
return (await fs.readFile(tmp, 'utf8')).toString()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user