forked from enquirer/enquirer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
countdown.js
40 lines (36 loc) · 1.02 KB
/
countdown.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const { dim, green, red, yellow } = require('ansi-colors');
const { Input } = require('enquirer');
const color = t => t >= 7 ? green(t) : t > 3 ? yellow(t) : red(t);
let time = 5;
let int;
/**
* This example shows how to create a "countdown" effect. You can
* put the countdown timer in the prefix, footer, header, separator,
* or whatever prompt position makes sense for your goal.
*/
const prompt = new Input({
name: 'name',
header() {
return `${dim('You have')} ${color(time)} ${dim('seconds left to answer!')}`;
},
separator() {
return ''; // hide separator
},
message(state) {
if (state.submitted && !state.input) return 'Really? Your own name?';
return state.submitted ? 'Well done,' : 'What is your full name!!!';
}
});
prompt.once('close', () => clearInterval(int));
prompt.once('run', () => {
int = setInterval(() => {
if (time-- === 0) {
prompt.state.input = '';
prompt.cancel();
} else {
prompt.render();
}
}, 1000);
});
prompt.run()
.catch(console.error);