53 lines
1.2 KiB
TypeScript
53 lines
1.2 KiB
TypeScript
import * as readline from 'node:readline'
|
|
import {BehaviorSubject} from "../util/subject.js";
|
|
import {Lifecycle, LifecycleAware} from "../util/lifecycle.js";
|
|
import {StreamLogger} from "../util/log.js";
|
|
import {log} from "../log.js";
|
|
|
|
export class Input extends BehaviorSubject<string> implements LifecycleAware {
|
|
private rl?: readline.Interface
|
|
private log: StreamLogger = log.getStreamLogger('input')
|
|
|
|
public readonly errors$: BehaviorSubject<Error> = new BehaviorSubject()
|
|
|
|
public hasPrompt(): boolean {
|
|
return !!this.rl
|
|
}
|
|
|
|
public setupPrompt(): void {
|
|
this.log.verbose({
|
|
setupPrompt: { hasExistingPrompt: !!this.rl },
|
|
})
|
|
|
|
if ( this.rl ) {
|
|
this.closePrompt()
|
|
}
|
|
|
|
this.rl = readline.createInterface({
|
|
input: process.stdin,
|
|
output: process.stdout,
|
|
prompt: 'str %> ',
|
|
})
|
|
|
|
this.rl.prompt()
|
|
|
|
this.rl.on('line', async (line) => {
|
|
await this.next(line + '\n')
|
|
this.rl?.prompt(true)
|
|
})
|
|
}
|
|
|
|
public closePrompt(): void {
|
|
this.log.verbose({
|
|
closePrompt: { hasExistingPrompt: !!this.rl },
|
|
})
|
|
|
|
this.rl?.close()
|
|
this.rl = undefined
|
|
}
|
|
|
|
adoptLifecycle(lifecycle: Lifecycle): void {
|
|
lifecycle.onClose(() => this.closePrompt())
|
|
}
|
|
}
|