-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathtext.ts
More file actions
48 lines (45 loc) · 1.09 KB
/
text.ts
File metadata and controls
48 lines (45 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import color from 'picocolors';
import Prompt, { type PromptOptions } from './prompt.js';
interface TextOptions extends PromptOptions<string, TextPrompt> {
placeholder?: string;
defaultValue?: string;
}
export default class TextPrompt extends Prompt<string> {
get userInputWithCursor() {
if (this.state === 'submit') {
return this.userInput;
}
const userInput = this.userInput;
if (this.cursor >= userInput.length) {
return `${this.userInput}█`;
}
const s1 = userInput.slice(0, this.cursor);
const [s2, ...s3] = userInput.slice(this.cursor);
return `${s1}${color.inverse(s2)}${s3.join('')}`;
}
get cursor() {
return this._cursor;
}
constructor(opts: TextOptions) {
super({
...opts,
initialUserInput: opts.initialUserInput ?? opts.initialValue,
});
this.on('userInput', (input) => {
this._setValue(input);
});
this.on('finalize', () => {
if (!this.value) {
this.value = opts.defaultValue;
}
if (this.value === undefined) {
this.value = '';
}
});
this.on('key', () => {
if (!this.value) {
this.value = opts.defaultValue;
}
});
}
}