* Fixed ask function
This commit is contained in:
parent
1117c1e3b2
commit
2d1c2034e4
8
package-lock.json
generated
8
package-lock.json
generated
@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "@ztimson/css-utils",
|
||||
"version": "1.1.1",
|
||||
"name": "@ztimson/node-utils",
|
||||
"version": "1.0.0",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "@ztimson/css-utils",
|
||||
"version": "1.1.1",
|
||||
"name": "@ztimson/node-utils",
|
||||
"version": "1.0.0",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@types/node": "^22.10.1",
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ztimson/node-utils",
|
||||
"version": "1.0.0",
|
||||
"version": "1.0.1",
|
||||
"description": "CSS Utility Classes",
|
||||
"author": "ztimson",
|
||||
"license": "MIT",
|
||||
|
40
src/input.ts
40
src/input.ts
@ -1,49 +1,43 @@
|
||||
import readline from 'node:readline';
|
||||
|
||||
/**
|
||||
* Retrieve input form the CLI
|
||||
* Retrieve input from the CLI
|
||||
*
|
||||
* @param {string} prompt Prepend a prompt before the cursor carrot
|
||||
* @param {boolean} hide Hide user input (replacing with stars)
|
||||
* @return {Promise<string>} User input
|
||||
*/
|
||||
export function ask(prompt: string, hide = false): Promise<string> {
|
||||
const rl: any = readline.createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout,
|
||||
terminal: true
|
||||
});
|
||||
|
||||
export function ask(prompt: string, hide = false) {
|
||||
const rl: any = readline.createInterface({input: process.stdin, output: process.stdout, terminal: true});
|
||||
return new Promise((resolve) => {
|
||||
if (!hide) {
|
||||
rl.question(prompt, (answer: string) => {
|
||||
rl.close();
|
||||
resolve(answer);
|
||||
rl.close();
|
||||
});
|
||||
} else {
|
||||
rl.output.write(prompt);
|
||||
let input = '';
|
||||
|
||||
// Listen for 'keypress' to handle masking
|
||||
rl.input.on('keypress', (char: string, key: any) => {
|
||||
const onKeyPress = (char: string, key: any) => {
|
||||
if (key && key.name === 'return') {
|
||||
rl.output.write('\n'); // Submit on new line
|
||||
rl.input.setRawMode(false);
|
||||
rl.input.removeListener('keypress', onKeyPress);
|
||||
rl.close();
|
||||
resolve(input);
|
||||
} else if (key && key.name === 'backspace') {
|
||||
if (input.length > 0) {
|
||||
input = input.slice(0, -1);
|
||||
rl.output.write(`\r${prompt}${input.replaceAll(/./g, '*')} \b`);
|
||||
}
|
||||
} else {
|
||||
input += char;
|
||||
rl.output.write('\b*'); // Mask the input with '*'
|
||||
if (key && key.name === 'backspace') {
|
||||
if (input.length > 0) input = input.slice(0, -1);
|
||||
} else {
|
||||
input += char;
|
||||
}
|
||||
rl.output.write(`\r${prompt}${'*'.repeat(input.length)} `);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// Restore settings
|
||||
// Attach keypress event
|
||||
rl.input.on('keypress', onKeyPress);
|
||||
rl.input.setRawMode(true);
|
||||
rl.input.resume();
|
||||
rl.output.write(prompt);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user