-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
56 lines (47 loc) · 1.53 KB
/
cli.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const keypress = require('keypress');
let currentIndex = 0;
let menuItems = [
['Create Regex', './createRegex.js'],
['String Starts With', './startsWith.js'],
['Read File', './readFile.js'],
['Math - Power', './math/pow.js'],
];
// Make `process.stdin` begin emitting "keypress" events
keypress(process.stdin);
// Listen for the "keypress" event
process.stdin.on('keypress', function (ch, key) {
if (key && key.ctrl && key.name == 'c') process.stdin.pause();
else if (key && key.name == 'down') {
currentIndex++;
renderMenu();
} else if (key && key.name == 'up') {
currentIndex--;
renderMenu();
} else if (key && (key.name == 'enter' || key.name == 'return')) {
let filePath = menuItems[currentIndex][1];
process.stdin.pause();
console.clear();
console.log(`Staring to run: ${filePath}`);
require(filePath);
} else renderMenu();
});
process.stdin.setRawMode(true);
process.stdin.resume();
renderMenu();
/**
* Render the menu
*/
function renderMenu() {
console.clear(); // Starting by clearing the console
if (currentIndex < 0) currentIndex = menuItems.length - 1;
currentIndex %= menuItems.length;
console.log(
'Welcome to JS Benchmarks! Please select a benchmark to run by using up/down arrows and enter/return to choose:'
);
menuItems.forEach((item, i) => {
item = item[0];
let start = ' ';
if (i === currentIndex) start = ' >';
console.log(`${start} ${i + 1}. ${item}.`);
});
}