Skip to content
Snippets Groups Projects
Commit d23b0e96 authored by Brandon Rodriguez's avatar Brandon Rodriguez
Browse files

Create "user confirmation" helper function

parent 98ba4cab
Branches
No related merge requests found
...@@ -2,6 +2,12 @@ ...@@ -2,6 +2,12 @@
* Contains misc helper functions. * Contains misc helper functions.
*/ */
// System Imports.
const chalk = require('chalk');
const readline = require('readline');
// User Imports.
/** /**
* Update strings to have an equivalent the "format()" found in Python strings. * Update strings to have an equivalent the "format()" found in Python strings.
...@@ -36,4 +42,36 @@ function getCurrentDate() { ...@@ -36,4 +42,36 @@ function getCurrentDate() {
} }
module.exports = getCurrentDate; /**
* Pauses current execution to prompt user for yes/no input.
* Returns True on yes response, and False on all other responses.
*
* "Readline" logic from https://stackoverflow.com/a/50890409
*/
async function userConfirmation() {
// Create "readline" instance to get user input.
const read = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// Get user input.
return new Promise(resolve => read.question(' Is this okay? ' + chalk.blue('[ Yes | No ]\n'), (user_input) => {
// Close "readline" instance to save processing power.
read.close();
// Handle based on user response.
user_input = user_input.toLowerCase();
if (user_input == 'yes' || user_input == 'ye' || user_input == 'y') {
resolve(true);
} else {
resolve(false);
}
}));
}
module.exports = {
getCurrentDate,
userConfirmation,
};
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment