forked from noxious/server
34 lines
693 B
TypeScript
34 lines
693 B
TypeScript
import * as readline from 'readline'
|
|
|
|
export class ConsolePrompt {
|
|
private readonly rl: readline.Interface
|
|
private isClosed: boolean = false
|
|
|
|
constructor(private readonly commandHandler: (command: string) => void) {
|
|
this.rl = readline.createInterface({
|
|
input: process.stdin,
|
|
output: process.stdout
|
|
})
|
|
|
|
this.rl.on('close', () => {
|
|
this.isClosed = true
|
|
})
|
|
}
|
|
|
|
public start(): void {
|
|
if (this.isClosed) return
|
|
this.promptCommand()
|
|
}
|
|
|
|
public close(): void {
|
|
this.rl.close()
|
|
}
|
|
|
|
private promptCommand(): void {
|
|
this.rl.question('> ', (command: string) => {
|
|
this.commandHandler(command)
|
|
this.promptCommand()
|
|
})
|
|
}
|
|
}
|