cspell
Version:
A Spelling Checker for Code!
52 lines • 1.52 kB
JavaScript
import * as readline from 'node:readline';
export function simpleRepl() {
return new SimpleRepl();
}
export class SimpleRepl {
prompt;
beforeEach;
completer;
_history;
rl;
constructor(prompt = '> ') {
this.prompt = prompt;
this._history = [];
this.rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt,
history: this._history,
historySize: 100,
completer: (line) => this._completer(line),
});
this.rl.on('history', (h) => ((this._history = h), undefined));
}
question(query) {
return new Promise((resolve) => {
this.rl.question(query, resolve);
});
}
_completer(line) {
// console.log('Complete: %s', line);
// console.log('History: %o', this._history);
if (this.completer)
return this.completer(line);
const hist = this._history.filter((h) => h.startsWith(line));
return [hist, line];
}
get history() {
return this._history;
}
[Symbol.asyncIterator]() {
const next = () => {
if (this.beforeEach)
this.beforeEach();
// console.log('%o', this.rl);
return this.question(this.prompt)
.then((value) => ({ value }))
.catch(() => ({ done: true, value: undefined }));
};
return { next };
}
}
//# sourceMappingURL=index.js.map