Skip to content

Commit

Permalink
Fixes cursor position for TextPrompt (#220)
Browse files Browse the repository at this point in the history
natemoo-re authored Dec 28, 2024
1 parent 0ada41e commit 8cba8e3
Showing 2 changed files with 16 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/seven-fireants-cover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@clack/core": patch
---

Fixes a rendering bug with cursor positions for `TextPrompt`
22 changes: 11 additions & 11 deletions packages/core/src/prompts/text.ts
Original file line number Diff line number Diff line change
@@ -7,7 +7,17 @@ export interface TextOptions extends PromptOptions<TextPrompt> {
}

export default class TextPrompt extends Prompt {
valueWithCursor = '';
get valueWithCursor() {
if (this.state === 'submit') {
return this.value;
}
if (this.cursor >= this.value.length) {
return `${this.value}${color.inverse(color.hidden('_'))}`;
}
const s1 = this.value.slice(0, this.cursor);
const [s2, ...s3] = this.value.slice(this.cursor);
return `${s1}${color.inverse(s2)}${s3.join('')}`;
}
get cursor() {
return this._cursor;
}
@@ -18,16 +28,6 @@ export default class TextPrompt extends Prompt {
if (!this.value) {
this.value = opts.defaultValue;
}
this.valueWithCursor = this.value;
});
this.on('value', () => {
if (this.cursor >= this.value.length) {
this.valueWithCursor = `${this.value}${color.inverse(color.hidden('_'))}`;
} else {
const s1 = this.value.slice(0, this.cursor);
const s2 = this.value.slice(this.cursor);
this.valueWithCursor = `${s1}${color.inverse(s2[0])}${s2.slice(1)}`;
}
});
}
}

0 comments on commit 8cba8e3

Please sign in to comment.