Skip to content

Commit c8d55e4

Browse files
committed
feat(core, prompts): clear autocomplete search on escape before cancelling
Add a `_shouldCancel()` hook to `Prompt` so a prompt can skip a cancel action. `AutocompletePrompt` uses it to clear a non-empty search input instead of cancelling; a second Escape on the empty input cancels as before. Selections in multiple mode are kept. Ctrl+C always cancels. The behaviour can be turned off with `clearOnCancel: false`, which the `path` prompt uses so Escape still cancels it. `_clearUserInput()` now moves the readline cursor to the end of the line before deleting, so the whole input is cleared even when the cursor is not at the end. Closes #519
1 parent f325a53 commit c8d55e4

7 files changed

Lines changed: 189 additions & 1 deletion

File tree

‎.changeset/calm-keys-clear.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@clack/prompts": minor
3+
"@clack/core": minor
4+
---
5+
6+
Pressing Escape in `autocomplete` and `autocompleteMultiselect` now clears the search input when it has text, and only cancels the prompt once the input is empty. Selections are kept. Ctrl+C still cancels right away, and the `path` prompt keeps cancelling on Escape. Pass `clearOnCancel: false` to keep the previous behavior. Prompts built on `@clack/core` can override the new `_shouldCancel()` hook to skip a cancel.

‎packages/core/src/prompts/autocomplete.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ export interface AutocompleteOptions<T extends OptionLike>
6363
* currently focused option's value, as if the user had typed it.
6464
*/
6565
completeOnTab?: boolean;
66+
/**
67+
* When `true` (the default), triggering the cancel action (e.g. pressing Escape)
68+
* while the input has text clears the input instead of cancelling the prompt.
69+
* Ctrl+C always cancels.
70+
*/
71+
clearOnCancel?: boolean;
6672
}
6773

6874
export default class AutocompletePrompt<T extends OptionLike> extends Prompt<
@@ -80,6 +86,7 @@ export default class AutocompletePrompt<T extends OptionLike> extends Prompt<
8086
#options: T[] | (() => T[]);
8187
#placeholder: string | undefined;
8288
#completeOnTab: boolean;
89+
#clearOnCancel: boolean;
8390

8491
get cursor(): number {
8592
return this.#cursor;
@@ -111,6 +118,7 @@ export default class AutocompletePrompt<T extends OptionLike> extends Prompt<
111118
this.#options = opts.options;
112119
this.#placeholder = opts.placeholder;
113120
this.#completeOnTab = opts.completeOnTab === true;
121+
this.#clearOnCancel = opts.clearOnCancel !== false;
114122
const options = this.options;
115123
this.filteredOptions = [...options];
116124
this.multiple = opts.multiple === true;
@@ -156,6 +164,14 @@ export default class AutocompletePrompt<T extends OptionLike> extends Prompt<
156164
);
157165
}
158166

167+
protected override _shouldCancel(char: string | undefined, _key: Key): boolean {
168+
if (this.#clearOnCancel && this.userInput !== '' && char !== '\x03') {
169+
this._clearUserInput();
170+
return false;
171+
}
172+
return true;
173+
}
174+
159175
#onKey(_char: string | undefined, key: Key): void {
160176
const isUpKey = key.name === 'up';
161177
const isDownKey = key.name === 'down';

‎packages/core/src/prompts/prompt.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@ export default class Prompt<TValue> {
205205
return true;
206206
}
207207

208+
protected _shouldCancel(_char: string | undefined, _key: Key): boolean {
209+
return true;
210+
}
211+
208212
protected _setValue(value: TValue | undefined): void {
209213
this.value = value;
210214
this.emit('value', this.value);
@@ -220,6 +224,7 @@ export default class Prompt<TValue> {
220224
}
221225

222226
protected _clearUserInput(): void {
227+
this.rl?.write(null, { ctrl: true, name: 'e' });
223228
this.rl?.write(null, { ctrl: true, name: 'u' });
224229
this._setUserInput('');
225230
}
@@ -277,7 +282,7 @@ export default class Prompt<TValue> {
277282
}
278283
}
279284

280-
if (isActionKey([char, key?.name, key?.sequence], 'cancel')) {
285+
if (isActionKey([char, key?.name, key?.sequence], 'cancel') && this._shouldCancel(char, key)) {
281286
this.state = 'cancel';
282287
}
283288

‎packages/core/test/prompts/autocomplete.test.ts‎

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { cursor } from 'sisteransi';
22
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
33
import { default as AutocompletePrompt } from '../../src/prompts/autocomplete.js';
4+
import { isCancel } from '../../src/utils/index.js';
45
import { MockReadable } from '../mock-readable.js';
56
import { MockWritable } from '../mock-writable.js';
67

@@ -328,4 +329,120 @@ describe('AutocompletePrompt', () => {
328329

329330
expect(instance.userInput).to.equal('ban');
330331
});
332+
333+
test('escape clears the search input instead of cancelling', async () => {
334+
const instance = new AutocompletePrompt({
335+
input,
336+
output,
337+
render: () => 'foo',
338+
options: testOptions,
339+
});
340+
341+
const promise = instance.prompt();
342+
input.emit('keypress', 'b', { name: 'b' });
343+
input.emit('keypress', 'a', { name: 'a' });
344+
expect(instance.filteredOptions.length).to.equal(1);
345+
346+
input.emit('keypress', '', { name: 'escape' });
347+
348+
expect(instance.state).to.equal('active');
349+
expect(instance.userInput).to.equal('');
350+
expect(instance.filteredOptions.length).to.equal(testOptions.length);
351+
352+
input.emit('keypress', 'c', { name: 'c' });
353+
expect(instance.userInput).to.equal('c');
354+
355+
input.emit('keypress', '', { name: 'return' });
356+
expect(await promise).to.equal('cherry');
357+
});
358+
359+
test('escape cancels once the search input is empty', async () => {
360+
const instance = new AutocompletePrompt({
361+
input,
362+
output,
363+
render: () => 'foo',
364+
options: testOptions,
365+
});
366+
367+
const promise = instance.prompt();
368+
input.emit('keypress', 'b', { name: 'b' });
369+
input.emit('keypress', '', { name: 'escape' });
370+
input.emit('keypress', '', { name: 'escape' });
371+
372+
expect(isCancel(await promise)).to.equal(true);
373+
expect(instance.state).to.equal('cancel');
374+
});
375+
376+
test('escape clears the whole search input when the cursor is not at the end', () => {
377+
const instance = new AutocompletePrompt({
378+
input,
379+
output,
380+
render: () => 'foo',
381+
options: testOptions,
382+
});
383+
384+
instance.prompt();
385+
input.emit('keypress', 'b', { name: 'b' });
386+
input.emit('keypress', 'a', { name: 'a' });
387+
input.emit('keypress', 'n', { name: 'n' });
388+
input.emit('keypress', '', { name: 'left' });
389+
input.emit('keypress', '', { name: 'left' });
390+
input.emit('keypress', '', { name: 'escape' });
391+
392+
expect(instance.userInput).to.equal('');
393+
394+
input.emit('keypress', 'g', { name: 'g' });
395+
expect(instance.userInput).to.equal('g');
396+
});
397+
398+
test('escape keeps selections when clearing the search input in multiple mode', async () => {
399+
const instance = new AutocompletePrompt({
400+
input,
401+
output,
402+
render: () => 'foo',
403+
options: testOptions,
404+
multiple: true,
405+
});
406+
407+
const promise = instance.prompt();
408+
input.emit('keypress', 'b', { name: 'b' });
409+
input.emit('keypress', '\t', { name: 'tab' });
410+
input.emit('keypress', '', { name: 'escape' });
411+
input.emit('keypress', 'c', { name: 'c' });
412+
input.emit('keypress', '\t', { name: 'tab' });
413+
input.emit('keypress', '', { name: 'return' });
414+
415+
expect(await promise).to.deep.equal(['banana', 'cherry']);
416+
});
417+
418+
test('ctrl-c cancels even when the search input has text', async () => {
419+
const instance = new AutocompletePrompt({
420+
input,
421+
output,
422+
render: () => 'foo',
423+
options: testOptions,
424+
});
425+
426+
const promise = instance.prompt();
427+
input.emit('keypress', 'b', { name: 'b' });
428+
input.emit('keypress', '\x03', { name: 'c', ctrl: true });
429+
430+
expect(isCancel(await promise)).to.equal(true);
431+
});
432+
433+
test('escape cancels with search input when clearOnCancel is false', async () => {
434+
const instance = new AutocompletePrompt({
435+
input,
436+
output,
437+
render: () => 'foo',
438+
options: testOptions,
439+
clearOnCancel: false,
440+
});
441+
442+
const promise = instance.prompt();
443+
input.emit('keypress', 'b', { name: 'b' });
444+
input.emit('keypress', '', { name: 'escape' });
445+
446+
expect(isCancel(await promise)).to.equal(true);
447+
});
331448
});

‎packages/prompts/src/autocomplete.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ interface AutocompleteSharedOptions<Value> extends CommonOptions {
8181
* Custom filter function to match options against the search input.
8282
*/
8383
filter?: (search: string, option: Option<Value>) => boolean;
84+
85+
/**
86+
* When `true` (the default), pressing Escape while the search input has text
87+
* clears the search instead of cancelling the prompt. Ctrl+C always cancels.
88+
*/
89+
clearOnCancel?: boolean;
8490
}
8591

8692
export interface AutocompleteOptions<Value> extends AutocompleteSharedOptions<Value> {
@@ -133,6 +139,7 @@ export const autocomplete = <Value>(opts: AutocompleteOptions<Value>) => {
133139
initialUserInput: opts.initialUserInput,
134140
placeholder: opts.placeholder,
135141
completeOnTab: opts.completeOnTab,
142+
clearOnCancel: opts.clearOnCancel,
136143
filter:
137144
opts.filter ??
138145
((search: string, opt: Option<Value>) => {
@@ -342,6 +349,7 @@ export const autocompleteMultiselect = <Value>(opts: AutocompleteMultiSelectOpti
342349
options: opts.options,
343350
multiple: true,
344351
placeholder: opts.placeholder,
352+
clearOnCancel: opts.clearOnCancel,
345353
filter:
346354
opts.filter ??
347355
((search, opt) => {

‎packages/prompts/src/path.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export const path = (opts: PathOptions) => {
6666
initialUserInput: opts.initialValue ?? opts.root ?? process.cwd(),
6767
maxItems: 5,
6868
completeOnTab: true,
69+
clearOnCancel: false,
6970
validate(value) {
7071
if (Array.isArray(value)) {
7172
// Shouldn't ever happen since we don't enable `multiple: true`

‎packages/prompts/test/autocomplete.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,41 @@ describe('autocompleteMultiselect', () => {
496496
const value = await result;
497497
expect(value).toEqual([]);
498498
});
499+
500+
test('Escape clears the search and keeps current selections', async () => {
501+
const result = autocompleteMultiselect({
502+
message: 'Select fruits',
503+
options: testOptions,
504+
input,
505+
output,
506+
});
507+
508+
input.emit('keypress', 'g', { name: 'g' });
509+
input.emit('keypress', '', { name: 'tab' });
510+
input.emit('keypress', '', { name: 'escape' });
511+
input.emit('keypress', 'o', { name: 'o' });
512+
input.emit('keypress', '', { name: 'tab' });
513+
input.emit('keypress', '', { name: 'return' });
514+
515+
const value = await result;
516+
expect(value).toEqual(['grape', 'orange']);
517+
});
518+
519+
test('Escape cancels when clearOnCancel is false', async () => {
520+
const result = autocompleteMultiselect({
521+
message: 'Select fruits',
522+
options: testOptions,
523+
clearOnCancel: false,
524+
input,
525+
output,
526+
});
527+
528+
input.emit('keypress', 'g', { name: 'g' });
529+
input.emit('keypress', '', { name: 'escape' });
530+
531+
const value = await result;
532+
expect(isCancel(value)).toBe(true);
533+
});
499534
});
500535

501536
describe('autocomplete with custom filter', () => {

0 commit comments

Comments
 (0)