Implemented using MutableStdOut Writable
Last active
June 3, 2024 09:14
-
-
Save kcak11/7da7eddf99d2e342dbfa4efa17b118c7 to your computer and use it in GitHub Desktop.
NodeJS Input Text and Password from Terminal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //#!/usr/bin/env node | |
| const readline = require('readline'); | |
| var Writable = require('stream').Writable; | |
| var mutableStdout = new Writable({ | |
| write: function(chunk, encoding, callback) { | |
| if (!this.muted) | |
| process.stdout.write(chunk, encoding); | |
| callback(); | |
| } | |
| }); | |
| mutableStdout.muted = false; // Default: Make the output stream visible | |
| var rl = readline.createInterface({ | |
| input: process.stdin, | |
| output: mutableStdout, | |
| terminal: true | |
| }); | |
| rl.question("Enter username: ", (uname)=>{ | |
| rl.question("Enter password: ", (pass)=>{ | |
| mutableStdout.muted = false; // Make the output stream visible again for further inputs | |
| rl.clearLine(mutableStdout, 0); | |
| //console.log(); // An alternate for rl.clearLine(mutableStdout, 0); | |
| rl.question("Enter age: ", (age)=>{ | |
| rl.question("Gender: ", (gender)=>{ | |
| logData(uname, pass, age, gender); | |
| rl.close(); | |
| }); | |
| }); | |
| }); | |
| mutableStdout.muted = true; // Hide the output stream while entering password | |
| }); | |
| var logData = function() { | |
| for (var i = 0; i < arguments.length; i++) { | |
| console.log(arguments[i]); | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment