Skip to content

Commit

Permalink
feat: add support for <C-u> to clear line in prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
parthsharma2 committed Apr 28, 2021
1 parent c5e2a59 commit ad78494
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/jest-watcher/src/constants.ts
Expand Up @@ -15,6 +15,7 @@ export const KEYS = {
BACKSPACE: Buffer.from(isWindows ? '08' : '7f', 'hex').toString(),
CONTROL_C: '\u0003',
CONTROL_D: '\u0004',
CONTROL_U: '\u0015',
ENTER: '\r',
ESCAPE: '\u001b',
};
6 changes: 6 additions & 0 deletions packages/jest-watcher/src/lib/Prompt.ts
Expand Up @@ -89,6 +89,12 @@ export default class Prompt {
case KEYS.ARROW_LEFT:
case KEYS.ARROW_RIGHT:
break;
case KEYS.CONTROL_U:
this._value = '';
this._offset = -1;
this._selection = null;
this._onChange();
break;
default:
this._value =
key === KEYS.BACKSPACE ? this._value.slice(0, -1) : this._value + key;
Expand Down
17 changes: 17 additions & 0 deletions packages/jest-watcher/src/lib/__tests__/prompt.test.ts
Expand Up @@ -62,3 +62,20 @@ it('calls handler on cancel prompt', () => {

expect(onCancel).toHaveBeenCalled();
});

it('clears the line when CONTROL_U is pressed', () => {
const prompt = new Prompt();
const onChange = jest.fn();
const options = {max: 10, offset: -1};

prompt.enter(onChange, jest.fn(), jest.fn());

prompt.put('t');
prompt.put('e');
prompt.put('s');
prompt.put('t');
expect(onChange).toHaveBeenLastCalledWith('test', options);

prompt.put(KEYS.CONTROL_U);
expect(onChange).toHaveBeenLastCalledWith('', options);
});

0 comments on commit ad78494

Please sign in to comment.