Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/whole-emoji-stay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@clack/core": patch
---

Stop the multi-line prompt from splitting emoji and other astral characters in half when typing after them, deleting them or moving the cursor over them.
29 changes: 19 additions & 10 deletions packages/core/src/prompts/multi-line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ function isControlCharacter(char: string): boolean {
return code === 0x7f || (code < 0x20 && char !== '\t' && char !== '\n');
}

// true when `index` sits between the two halves of a surrogate pair (an emoji, for example)
function splitsSurrogatePair(text: string, index: number): boolean {
const high = text.charCodeAt(index - 1);
const low = text.charCodeAt(index);
return high >= 0xd800 && high <= 0xdbff && low >= 0xdc00 && low <= 0xdfff;
}

export interface MultiLineOptions extends PromptOptions<string, MultiLinePrompt> {
placeholder?: string;
defaultValue?: string;
Expand All @@ -31,9 +38,10 @@ export default class MultiLinePrompt extends Prompt<string> {
if (this.cursor >= userInput.length) {
return `${userInput}█`;
}
const cursorEnd = this.cursor + (splitsSurrogatePair(userInput, this.cursor + 1) ? 2 : 1);
const preCursor = userInput.slice(0, this.cursor);
const cursorChar = userInput.slice(this.cursor, this.cursor + 1);
const rest = userInput.slice(this.cursor + 1);
const cursorChar = userInput.slice(this.cursor, cursorEnd);
const rest = userInput.slice(cursorEnd);
if (cursorChar === '\n') return `${preCursor}█\n${rest}`;
return `${preCursor}${styleText('inverse', cursorChar)}${rest}`;
}
Expand Down Expand Up @@ -113,6 +121,9 @@ export default class MultiLinePrompt extends Prompt<string> {
if (key?.name && cursorActions.has(key.name as CursorAction)) {
this.#lastKeyWasReturn = false;
this.#handleCursor(key.name as CursorAction);
if (splitsSurrogatePair(this.userInput, this._cursor)) {
this._cursor += key.name === 'right' ? 1 : -1;
}
return;
}
if (char === '\t' && this.#showSubmit) {
Expand All @@ -124,24 +135,22 @@ export default class MultiLinePrompt extends Prompt<string> {
}
this.#lastKeyWasReturn = false;
if (key?.name === 'backspace' && this.cursor > 0) {
this._setUserInput(
this.userInput.slice(0, this.cursor - 1) + this.userInput.slice(this.cursor)
);
this._cursor--;
const start = this.cursor - (splitsSurrogatePair(this.userInput, this.cursor - 1) ? 2 : 1);
this._setUserInput(this.userInput.slice(0, start) + this.userInput.slice(this.cursor));
this._cursor = start;
return;
}
if (key?.name === 'delete' && this.cursor < this.userInput.length) {
this._setUserInput(
this.userInput.slice(0, this.cursor) + this.userInput.slice(this.cursor + 1)
);
const end = this.cursor + (splitsSurrogatePair(this.userInput, this.cursor + 1) ? 2 : 1);
this._setUserInput(this.userInput.slice(0, this.cursor) + this.userInput.slice(end));
return;
}
if (char && !isControlCharacter(char)) {
if (this.#showSubmit && this.focused === 'submit') {
this.focused = 'editor';
}
this.#insertAtCursor(char ?? '');
this._cursor++;
this._cursor += char.length;
}
});

Expand Down
104 changes: 104 additions & 0 deletions packages/core/test/prompts/multi-line.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,19 @@ describe('MultiLinePrompt', () => {
expect(instance.userInputWithCursor).to.equal(`fo${styleText('inverse', 'o')}`);
});

test('highlights a whole astral character', () => {
const instance = new MultiLinePrompt({
input,
output,
render: () => 'foo',
});
instance.prompt();
input.emit('keypress', 'x', { name: 'x' });
input.emit('keypress', '👍', { name: undefined });
input.emit('keypress', undefined, { name: 'left' });
expect(instance.userInputWithCursor).to.equal(`x${styleText('inverse', '👍')}`);
});

test('shows cursor at end if beyond value', () => {
const instance = new MultiLinePrompt({
input,
Expand Down Expand Up @@ -246,6 +259,23 @@ describe('MultiLinePrompt', () => {
expect(instance.userInput).to.equal('xX');
});

test('typing continues after an astral character', async () => {
const instance = new MultiLinePrompt({
input,
output,
render: () => 'foo',
});
const resultPromise = instance.prompt();
input.emit('keypress', '👍', { name: undefined });
input.emit('keypress', 'x', { name: 'x' });
expect(instance.userInput).to.equal('👍x');
expect(instance.cursor).to.equal(3);
input.emit('keypress', '', { name: 'return' });
input.emit('keypress', '', { name: 'return' });
const result = await resultPromise;
expect(result).to.equal('👍x');
});

test('backspace deletes previous char', async () => {
const instance = new MultiLinePrompt({
input,
Expand Down Expand Up @@ -279,6 +309,40 @@ describe('MultiLinePrompt', () => {
expect(result).to.equal('x');
});

test('backspace deletes a whole astral character', async () => {
const instance = new MultiLinePrompt({
input,
output,
render: () => 'foo',
});
const resultPromise = instance.prompt();
input.emit('keypress', 'x', { name: 'x' });
input.emit('keypress', '👍', { name: undefined });
input.emit('keypress', '\x7f', { name: 'backspace' });
expect(instance.userInput).to.equal('x');
expect(instance.cursor).to.equal(1);
input.emit('keypress', '', { name: 'escape' });
await resultPromise;
});

test('delete deletes a whole astral character', async () => {
const instance = new MultiLinePrompt({
input,
output,
render: () => 'foo',
});
const resultPromise = instance.prompt();
input.emit('keypress', '👍', { name: undefined });
input.emit('keypress', 'x', { name: 'x' });
input.emit('keypress', '', { name: 'left' });
input.emit('keypress', '', { name: 'left' });
input.emit('keypress', '', { name: 'delete' });
expect(instance.userInput).to.equal('x');
expect(instance.cursor).to.equal(0);
input.emit('keypress', '', { name: 'escape' });
await resultPromise;
});

test('delete does nothing at end', async () => {
const instance = new MultiLinePrompt({
input,
Expand Down Expand Up @@ -397,6 +461,26 @@ describe('MultiLinePrompt', () => {
expect(result).to.equal('xyz');
});

test('left and right move over an astral character', async () => {
const instance = new MultiLinePrompt({
input,
output,
render: () => 'foo',
});
const resultPromise = instance.prompt();
input.emit('keypress', 'x', { name: 'x' });
input.emit('keypress', '👍', { name: undefined });
input.emit('keypress', 'y', { name: 'y' });
input.emit('keypress', '', { name: 'left' });
input.emit('keypress', '', { name: 'left' });
input.emit('keypress', 'a', { name: 'a' });
input.emit('keypress', '', { name: 'right' });
input.emit('keypress', 'b', { name: 'b' });
expect(instance.userInput).to.equal('xa👍by');
input.emit('keypress', '', { name: 'escape' });
await resultPromise;
});

test('left moves across lines', async () => {
const instance = new MultiLinePrompt({
input,
Expand Down Expand Up @@ -471,5 +555,25 @@ describe('MultiLinePrompt', () => {
const result = await resultPromise;
expect(result).to.equal('x\nyz');
});

test('down does not land inside an astral character', async () => {
const instance = new MultiLinePrompt({
input,
output,
render: () => 'foo',
});
const resultPromise = instance.prompt();
input.emit('keypress', 'x', { name: 'x' });
input.emit('keypress', 'y', { name: 'y' });
input.emit('keypress', '', { name: 'return' });
input.emit('keypress', '👍', { name: undefined });
input.emit('keypress', '', { name: 'up' });
input.emit('keypress', '', { name: 'left' });
input.emit('keypress', '', { name: 'down' });
input.emit('keypress', 'z', { name: 'z' });
expect(instance.userInput).to.equal('xy\nz👍');
input.emit('keypress', '', { name: 'escape' });
await resultPromise;
});
});
});
Loading