Lance Pollard
Lance Pollard

Reputation: 79268

Ansi escape sequences in chrome web console

Wondering if chrome console.log allows you to go back and rewrite existing console.log output, like you can do in Node.js. I know you can do colors in the chrome console, but not sure about editing existing text. Would like to try adding a progress bar such as:

enter image description here

I'm getting this:

enter image description here

Upvotes: 2

Views: 1774

Answers (1)

Josh Lee
Josh Lee

Reputation: 177594

Try calling console.clear (chrome, MDN) in a loop. This works as long as you have ‘preserve log’ turned off. It flickers a bit for me in Chrome’s console, but such is life (it's actually great in Firefox).

delay=t=>new Promise(resolve=>setTimeout(resolve,t));

// the stack snippets console emulator doesn't seem
// to work if the very first call is clear.
console.log();

(async()=>{
  for(i=0;i<25;i++){
    console.clear();
    console.log('*'.repeat(i));
    await delay(100);
  }
})()

Upvotes: 3

Related Questions