bubkoo / ascii-progress

🍓 Ascii progress-bar(s) in the terminal.
MIT License
207 stars 21 forks source link

Progress display messes up on console input #29

Open erhhung opened 6 years ago

erhhung commented 6 years ago

I've noticed that with any of the examples, but especially multi-bar ones, that if I start typing while the progress bars are moving, the console just gets filled with uninterpreted ANSI escape sequences on every tick, and the bar rendering completely breaks.

I don't see a need to accept typed input during progress monitoring, except perhaps ESC to cancel, but the console rendering positions should not break down.

I'm running the examples in a Mac Terminal window with Bash if that makes a difference.

erhhung commented 6 years ago

Perhaps this can only be handled outside of the core component. I've solved this issue by putting stdin into raw mode and explicitly handling keys like ESC and SIGINT before I begin the concurrent progress bars:

const {stdin} = process;

stdin.setRawMode(true);
stdin.setEncoding('utf8');
stdin.on('data', (key) => {
  if (key === '\u001b' || // ESC
      key === '\u0003') { // Ctrl-C
    process.exit();
  }
});
stdin.resume();

As I'm just writing a CLI app, I just terminate the process, but you could resolve a promise or do some other signaling if you wanted to stop or pause the processing.