Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide console's user input after return/new line

What I'd like to do is hiding/erasing all users input from a nodejs console app once entered, so that when the user inserts some text and then types enter, he won't be able to read what he just entered in the console anymore (so basically remove the line right after it's been entered).

This should be pretty simple to achieve but I have no idea how to do it =).

Thank you in advance

EDIT: Let's say we have this code:

const readline = require('readline')

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
 })

rl.question('How ya doin?\n', input => {
    console.log('seems like you\'r doing ' + input.toString())  
})
  • The app prompts a question
  • The user answers "Fine" (This line shouldn't be there anymore after the user presses enter)
  • The program says "seems like .... Fine"

Output

like image 862
razorxan Avatar asked Oct 30 '25 15:10

razorxan


2 Answers

Looks like readline can already handle this for you..

const readline = require('readline')

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
 })

rl.question('How ya doin?\n', input => {
    readline.moveCursor(process.stdout, 0,-1)
    console.log('seems like you\'r doing ' + input.toString())  
})
like image 139
davobutt Avatar answered Nov 02 '25 04:11

davobutt


According to Bash HOWTO

Move the cursor up N lines:

\033[<N>A

To overwrite user's 1 line input with your output you should move 1 line up and print your output. It will look like this:

console.log('\033[1A' + 'seems like you\'r doing ' + input.toString());

UPDATE: Found a nice answer :)

How do you edit existing text (and move the cursor around) in the terminal?

like image 42
LEQADA Avatar answered Nov 02 '25 04:11

LEQADA



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!