diff --git a/src/helper_functions.js b/src/helper_functions.js index 4e1b0512a33792790ae6f7a15fee43f80d3547a6..77a3d6f7f9a706a5de3eb5629ac4fe72b1343df5 100644 --- a/src/helper_functions.js +++ b/src/helper_functions.js @@ -2,6 +2,12 @@ * 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. @@ -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, +};