Files
server/src/application/console/consolePrompt.ts
2024-12-28 21:26:31 +01:00

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()
})
}
}