diff --git a/.changeset/whole-emoji-stay.md b/.changeset/whole-emoji-stay.md new file mode 100644 index 00000000..3305ec58 --- /dev/null +++ b/.changeset/whole-emoji-stay.md @@ -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. diff --git a/packages/core/src/prompts/multi-line.ts b/packages/core/src/prompts/multi-line.ts index dc3d8f5d..20f50dcd 100644 --- a/packages/core/src/prompts/multi-line.ts +++ b/packages/core/src/prompts/multi-line.ts @@ -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 { placeholder?: string; defaultValue?: string; @@ -31,9 +38,10 @@ export default class MultiLinePrompt extends Prompt { 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}`; } @@ -113,6 +121,9 @@ export default class MultiLinePrompt extends Prompt { 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) { @@ -124,16 +135,14 @@ export default class MultiLinePrompt extends Prompt { } 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)) { @@ -141,7 +150,7 @@ export default class MultiLinePrompt extends Prompt { this.focused = 'editor'; } this.#insertAtCursor(char ?? ''); - this._cursor++; + this._cursor += char.length; } }); diff --git a/packages/core/test/prompts/multi-line.test.ts b/packages/core/test/prompts/multi-line.test.ts index cf220438..aefcf9d0 100644 --- a/packages/core/test/prompts/multi-line.test.ts +++ b/packages/core/test/prompts/multi-line.test.ts @@ -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, @@ -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, @@ -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, @@ -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, @@ -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; + }); }); });