Skip to content

Support case-sensitive keys in SelectKeyPrompt #435

@bcheung

Description

@bcheung

Is your feature request related to a problem? Please describe.
Current implementation lowercases option keys and prevents ability to use distinct lowercase and uppercase keys. This makes common CLI patterns impossible (e.g. 'a' = accept, 'A' = accept all).
Side note: the initialValue lookup is also compared against the un-normalized value, which can place the cursor incorrectly. (see code below)

Describe the solution you'd like
Add support for case-sensitive key matching by removing the lowercasing normalization so comparisons are exact (case-sensitive).
If this is too invasive, consider adding an option to opt into this behavior.

Additional context
I believe this is where everything is normalized to lowercase:

export default class SelectKeyPrompt<T extends { value: string }> extends Prompt<T['value']> {
options: T[];
cursor = 0;
constructor(opts: SelectKeyOptions<T>) {
super(opts, false);
this.options = opts.options;
const keys = this.options.map(({ value: [initial] }) => initial?.toLowerCase());
this.cursor = Math.max(keys.indexOf(opts.initialValue), 0);
this.on('key', (key) => {
if (!key || !keys.includes(key)) return;
const value = this.options.find(({ value: [initial] }) => initial?.toLowerCase() === key);
if (value) {
this.value = value.value;
this.state = 'submit';
this.emit('submit');
}
});
}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    Status

    Needs triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions