Repository: jestjs/jest (TypeScript)

Issue:
<C-u> to clear watch mode pattern prompt
<!-- Love Jest? Please consider supporting our collective: 👉  https://opencollective.com/jest/donate -->

## 🚀 Feature Proposal

Currently, pressing Ctrl+u in a watch mode pattern prompt inserts an invisible character that also somewhat breaks searching because the files/tests searched for do not contain the character.
In most terminal environments, Ctrl+u clears the whole line. I think this should happen in the watch mode pattern prompt as well.

## Motivation

Many users will expect Ctrl+u to behave as elsewhere in their terminal environment.
The workaround requires pressing backspace a lot to clear the line manually.

## Pitch

Why does this feature belong in the [Jest core platform](https://www.youtube.com/watch?v=NtjyeojAOBs)?

Since `jest-watcher`s `Prompt` is reused in third-party packages like `jest-watch-typeahead`, making the improvement here would benefit those as well as users of plain Jest watch mode prompts.


Hints:
This is a Bash thing, but it probably works in most shell emulators (it does in zsh which I use at least).

If we add ctrl+u, we should also do others, like meta/ctrl+b and meta/ctrl+f etc. Feels like a somewhat slippery slope, but perhaps there's some library we can use which parses the input and returns a "command" of some sort that's easier to understand? Or even better would be some "cursor" library which, given a string and a position can parse terminal input and return a new string (if it modifies it) and cursor position. I've done zero research into whether such a thing already exists 🙂 
> This is a Bash thing, but it probably works in most shell emulators (it does in zsh which I use at least).

Yeah, but it's also supported in many non-shell places in "the terminal", like in Vim insert mode, when editing a tmux command line, ..., hence why I phrased it more generally as "terminal environments".

Not sure how much of a slippery slope it is, I don't think there are many common commands that would be meaningful in the pattern prompt, and especially none that are as ubiquitous across different applications as <C-u>.
We should definitely have moving backwards and forwards (plus deleting words) if we start adding support for "commands". That might be it, though?
> Yeah, but it's also supported in many non-shell places in "the terminal"

Oh, and perhaps notably <C-u> is supported in the login prompt on Linux as well, before you even get a shell.

> We should definitely have moving backwards and forwards (plus deleting words) if we start adding support for "commands". That might be it, though?

Yeah it seems moving forward and backward is about as ubiquitous. Would be nice if a library gives us everything for free - I've edited the description to say this should be checked - but I wouldn't mind introducing one without the other if it's easier since it's better than nothing.
Deleting words with <C-w> also seems useful and almost as common (not supported in login prompt, where I guess it wouldn't be very helpful anyway, but just about anywhere else).
Hi @jeysal @SimenB  could I work on this issue? I think I can implement the `CTRL + U` functionality.

Here is what I have in mind:
- add `CONTROL_U` in the `KEYS` map in `constants.ts`
- in the `put` method in the `Prompt` class whenever `CONTROL_U` is pressed set `this._value = ""`, `this._offset = -1`, `this._selection = null` & call `this._onChange()`
@parthsharma2 sounds about right yeah :)

Patch:
diff --git a/packages/jest-watcher/src/constants.ts b/packages/jest-watcher/src/constants.ts
--- a/packages/jest-watcher/src/constants.ts
+++ b/packages/jest-watcher/src/constants.ts
@@ -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',
 };
diff --git a/packages/jest-watcher/src/lib/Prompt.ts b/packages/jest-watcher/src/lib/Prompt.ts
--- a/packages/jest-watcher/src/lib/Prompt.ts
+++ b/packages/jest-watcher/src/lib/Prompt.ts
@@ -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;


Test patch:
diff --git a/packages/jest-watcher/src/lib/__tests__/prompt.test.ts b/packages/jest-watcher/src/lib/__tests__/prompt.test.ts
--- a/packages/jest-watcher/src/lib/__tests__/prompt.test.ts
+++ b/packages/jest-watcher/src/lib/__tests__/prompt.test.ts
@@ -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);
+});