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/clever-tigers-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@clack/core": minor
---

Press number keys 1–9 in the select prompt to jump to the corresponding option.
10 changes: 10 additions & 0 deletions packages/core/src/prompts/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,15 @@ export default class SelectPrompt<T extends { value: any; disabled?: boolean }>
}
this.changeValue();
});

this.on('key', (char, key) => {
if (key.ctrl || key.meta) return;
if (!char || char.length !== 1) return;
if (char < '1' || char > '9') return;
const target = Number(char) - 1;
if (target >= this.options.length) return;
this.cursor = this.options[target].disabled ? findCursor<T>(target, 1, this.options) : target;
this.changeValue();
});
}
}
36 changes: 36 additions & 0 deletions packages/core/test/prompts/select.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,41 @@ describe('SelectPrompt', () => {
instance.prompt();
expect(instance.cursor).to.equal(1);
});

test('number key jumps to option', () => {
const instance = new SelectPrompt({
input,
output,
render: () => 'foo',
options: [{ value: 'foo' }, { value: 'bar' }, { value: 'baz' }],
});
instance.prompt();
input.emit('keypress', '3', { name: '3' });
expect(instance.cursor).to.equal(2);
});

test('number key skips disabled option', () => {
const instance = new SelectPrompt({
input,
output,
render: () => 'foo',
options: [{ value: 'foo' }, { value: 'bar', disabled: true }, { value: 'baz' }],
});
instance.prompt();
input.emit('keypress', '2', { name: '2' });
expect(instance.cursor).to.equal(2);
});

test('out-of-range number key is ignored', () => {
const instance = new SelectPrompt({
input,
output,
render: () => 'foo',
options: [{ value: 'foo' }, { value: 'bar' }],
});
instance.prompt();
input.emit('keypress', '9', { name: '9' });
expect(instance.cursor).to.equal(0);
});
});
});
Loading