title: Unable to clear line when using readline prompt / setPrompt in Node
body: <p>I'm having trouble clearing a line in <code>stdout</code> when using <a href="https://nodejs.org/api/readline.html#readline_rl_prompt_preservecursor" rel="noreferrer"><code>readline prompt</code></a>. </p>
  
  <p>I code similar to this:</p>
  
  <pre><code>import readline from 'readline'
  
  const rl = readline.createInterface(process.stdin, process.stdout)
  let input = ''
  
  readline.emitKeypressEvents(process.stdin)
  process.stdin.on('keypress', registerInput)
  
  function registerInput (str, key) {
    if (key.name === 'backspace') {
      input = input.slice(0, input.length - 1)
    } else {
      input += str
    }
    if (input === '42') {
      input = ''
      console.log(' ✓')
      nextPrompt()
    }
  }
  
  function nextPrompt () {
    readline.clearLine(process.stdout, 0)
    rl.setPrompt('What is the meaning of life? ')
    rl.prompt()
  }
  
  nextPrompt()
  </code></pre>
  
  <p>In the real code, arithmetic questions are generated.</p>
  
  <p><a href="https://asciinema.org/a/e2pfqvzr2wkkjvp8l5ra2wnhh" rel="noreferrer">Here is a screen capture of the output</a>.</p>
  
  <h2>What happens</h2>
  
  <p>The cursor resets (using <code>.prompt(false)</code> keeps cursor in position), but the input is still there in the new prompt.</p>
  
  <h2>Expected</h2>
  
  <p>The input <code>42</code> should not appear in the new prompt on the next line.</p>
  
  <p>Why am I using <code>.on('keypress'</code> instead of <code>rl.on('line'</code>? I want the response in the game to be snap, avoiding pressing <em>enter</em>.</p>
  
  <p>What I have tried:</p>
  
  <ul>
  <li><code>readline.clearLine(process.stdout, -1)</code></li>
  <li><code>readline.clearLine(process.stdout, 1)</code></li>
  <li><code>readline.clearLine(process.stdin, 0)</code></li>
  <li><code>process.stdout.clearLine()</code></li>
  </ul>
  
  <h2>Environment</h2>
  
  <p>I'm using node v6.0.0 and babel-cli v6.7.7 (here babel just for the import statement, but in the real code I use <a href="https://github.com/sebmarkbage/ecmascript-rest-spread" rel="noreferrer">object-rest-spread</a> and therefor need babel). <a href="https://gist.github.com/arve0/bafb14f1848a1c9bbb45903450393625" rel="noreferrer">Here is a gist of the generated code</a> if you do not have babel installed.</p>
  
  <h2>Question</h2>
  
  <p>Is <code>rl.prompt</code> incompatible with <code>readline.clearLine</code>? How can this be solved?</p>