16
loading...
This website collects cookies to deliver better user experience
node star-rating.js <rating>
m
to increase the rating and l
to decrease the rating, and pressing Ctrl + C
to submit/quit).
let the red ansi escape be \u001b[31m
let the reset ansi escape be \u001b[0m
let the stars be ★ ★ ★ ★ ★ // Add more stars if you need
let the current rating be 0
log the rating(rating = 0, message = \n\nRate us!) {
clear the previous log
log the star rating and the message to the console
}
handle the rating(rating) {
do something with the rating here (maybe send to an external service or something)
exit from process
}
increase rating () {
if (rating is less than number of stars) rating = rating + 1
}
decrease rating () {
if (rating is more than 0) rating = rating - 1
}
if there is a first argument {
handle rating(first argument)
} else {
listen to keydown events
when a key is pressed {
if (the key is m) increase the rating
if (the key is l) decrease the rating
if (the key is a number) set the rating as the correct number and render again
if (the key is ctrl+c) handle rating
}
log the rating(currentRating)
}
if
block:if (first argument) {
} else {
}
process.argv
.process.argv
is an array of the arguments provided when running node somefile.js arguments...
. It looks like this:['/path/to/node', '/path/to/executed/file.js', 'rest', 'of', 'the', 'arguments']
if (+process.argv[2]) {
} else {
}
handleRating
it:function handleRating(rating) {
// Do something useful here
console.log(`You rated us ${rating}`);
// Exit – just in case
process.exit(0);
}
if (+process.argv[2]) {
handleRating(+process.argv[2]);
} else {
}
else
block. And we can do that using the keypress
module (Fun fact, the keydown event was part of the NodeJS core, but somehow it was removed). Don't forget to install the package!keypress
usage is fairly simple:else {
const keypress = require('keypress');
keypress(process.stdin);
process.stdin.addEventListener('keypress', (ch, key) => {
// Here `ch` contains the key which was pressed and `key contains more data on the pressed key`
})
}
let currentRating = 0;
switch
and do accordingly:switch (ch) {
case '1':
currentRating = 1;
break;
case '2':
currentRating = 2;
break;
case '3':
currentRating = 3;
break;
case '4':
currentRating = 4;
break;
case '5':
currentRating = 5;
break;
case 'm':
increaseRating();
break;
case 'l':
decreaseRating();
break;
}
if (key && key.ctrl && key.name == 'c') handleRating(currentRating);
stdin
, so we call .resume
outside our event listener at the end of the else
:process.stdin.setRawMode(true);
process.stdin.resume();
logStarRating
function which clears the console and logs a star rating (with a message).stdout
tricks, but it doesn't work everywhere, so I decided to use another library to achieve this: log-update
function logStarRating(rating, message) {
// Code...
}
// All the stars (or whatever shapes you like)
const allStars = '★ ★ ★ ★ ★';
// Ansi escape codes. If you enter any of these in the console, the style of the text will change.
const redAnsi = '\u001b[31m', resetAnsi = '\u001b[0m';
// The star
const star =
// Add the red escape code
redAnsi +
// Split the stars into an array
allStars.split(' ')
// If we have reached the right place, insert the reset ansi code
.map((item, index) => index >= number ? resetAnsi + item : item)
// Join everything back together
.join(' ');
logUpdate
to update our stars:const logUpdate = require('log-update');
// Message comes from the second function argument
logUpdate(`${star}${message}`);
logStarRating
after our earlier switch
:// ...
case 'l':
decreaseRating();
break;
}
logStarRating(currentRating)