Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional number reset #351

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
23 changes: 19 additions & 4 deletions lib/elements/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const round = (number, precision) => {
* @param {Boolean} [opts.float=false] Parse input as floats
* @param {Number} [opts.round=2] Round floats to x decimals
* @param {Number} [opts.increment=1] Number to increment by when using arrow-keys
* @param {Number} [opts.resetDelay=0] Milliseconds until input is automatically reset
* @param {Function} [opts.validate] Validate function
* @param {Stream} [opts.stdin] The Readable stream to listen to
* @param {Stream} [opts.stdout] The Writable stream to write readline data to
Expand All @@ -42,7 +43,8 @@ class NumberPrompt extends Prompt {
this.color = `cyan`;
this.value = ``;
this.typed = ``;
this.lastHit = 0;
this.resetDelay = opts.resetDelay || 0;
this.resetTimer = 0;
this.render();
}

Expand All @@ -55,7 +57,21 @@ class NumberPrompt extends Prompt {
this.placeholder = false;
this.rendered = this.transform.render(`${round(v, this.round)}`);
this._value = round(v, this.round);

if (this.resetDelay > 0) {
justinseibert marked this conversation as resolved.
Show resolved Hide resolved
clearTimeout(this.resetTimer);
this.resetTimer = setTimeout(() => {
const color = this.color;
this.color = `inverse`;
this.render();
setTimeout(() => {
this.color = color;
this.reset();
}, 200);
}, this.resetDelay);
}
justinseibert marked this conversation as resolved.
Show resolved Hide resolved
}
this.typed = this._value;
this.fire();
}

Expand Down Expand Up @@ -87,6 +103,7 @@ class NumberPrompt extends Prompt {
this.value = x !== `` ? x : this.initial;
this.done = this.aborted = true;
this.error = false;
clearTimeout(this.resetTimer);
this.fire();
this.render();
this.out.write(`\n`);
Expand Down Expand Up @@ -115,6 +132,7 @@ class NumberPrompt extends Prompt {
this.done = true;
this.aborted = false;
this.error = false;
clearTimeout(this.resetTimer);
this.fire();
this.render();
this.out.write(`\n`);
Expand Down Expand Up @@ -166,10 +184,7 @@ class NumberPrompt extends Prompt {
_(c, key) {
if (!this.valid(c)) return this.bell();

const now = Date.now();
if (now - this.lastHit > 1000) this.typed = ``; // 1s elapsed
this.typed += c;
this.lastHit = now;
this.color = `cyan`;

if (c === `.`) return this.fire();
Expand Down
1 change: 1 addition & 0 deletions lib/prompts.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ $.invisible = args => {
* @param {Boolean} [opts.float=false] Parse input as floats
* @param {Number} [opts.round=2] Round floats to x decimals
* @param {Number} [opts.increment=1] Number to increment by when using arrow-keys
* @param {Number} [opts.resetDelay=0] Milliseconds until input is automatically reset
* @param {function} [args.validate] Function to validate user input
* @param {Stream} [args.stdin] The Readable stream to listen to
* @param {Stream} [args.stdout] The Writable stream to write readline data to
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@ You can type in numbers and use <kbd>up</kbd>/<kbd>down</kbd> to increase/decrea
| float | `boolean` | Allow floating point inputs. Defaults to `false` |
| round | `number` | Round `float` values to x decimals. Defaults to `2` |
| increment | `number` | Increment step when using <kbd>arrow</kbd> keys. Defaults to `1` |
| resetDelay | `number` | Milliseconds to wait before the input is automatically reset. Defaults to `0` (disabled)
| style | `string` | Render style (`default`, `password`, `invisible`, `emoji`). Defaults to `default` |
| onRender | `function` | On render callback. Keyword `this` refers to the current prompt |
| onState | `function` | On state change callback. Function signature is an `object` with two properties: `value` and `aborted` |
Expand Down