Skip to content

Commit 8cba8e3

Browse files
authored
Fixes cursor position for TextPrompt (bombshell-dev#220)
1 parent 0ada41e commit 8cba8e3

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

.changeset/seven-fireants-cover.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@clack/core": patch
3+
---
4+
5+
Fixes a rendering bug with cursor positions for `TextPrompt`

packages/core/src/prompts/text.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,17 @@ export interface TextOptions extends PromptOptions<TextPrompt> {
77
}
88

99
export default class TextPrompt extends Prompt {
10-
valueWithCursor = '';
10+
get valueWithCursor() {
11+
if (this.state === 'submit') {
12+
return this.value;
13+
}
14+
if (this.cursor >= this.value.length) {
15+
return `${this.value}${color.inverse(color.hidden('_'))}`;
16+
}
17+
const s1 = this.value.slice(0, this.cursor);
18+
const [s2, ...s3] = this.value.slice(this.cursor);
19+
return `${s1}${color.inverse(s2)}${s3.join('')}`;
20+
}
1121
get cursor() {
1222
return this._cursor;
1323
}
@@ -18,16 +28,6 @@ export default class TextPrompt extends Prompt {
1828
if (!this.value) {
1929
this.value = opts.defaultValue;
2030
}
21-
this.valueWithCursor = this.value;
22-
});
23-
this.on('value', () => {
24-
if (this.cursor >= this.value.length) {
25-
this.valueWithCursor = `${this.value}${color.inverse(color.hidden('_'))}`;
26-
} else {
27-
const s1 = this.value.slice(0, this.cursor);
28-
const s2 = this.value.slice(this.cursor);
29-
this.valueWithCursor = `${s1}${color.inverse(s2[0])}${s2.slice(1)}`;
30-
}
3131
});
3232
}
3333
}

0 commit comments

Comments
 (0)