4

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

razorxan
  • 516
  • 1
  • 7
  • 20

2 Answers2

8

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())  
})
davobutt
  • 161
  • 2
4

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?

Community
  • 1
  • 1
LEQADA
  • 1,913
  • 3
  • 22
  • 41