chalk-animation icon indicating copy to clipboard operation
chalk-animation copied to clipboard

Support "process.stdout.write" in logging

Open spidy0x0 opened this issue 2 years ago • 7 comments

Hi, i love this project and i wanted to suggest a feature .

i normally use this code to update figures/values on the same line without creating newlines. just like console.log,but it doesn't create a newline always. Example is when creating a countdown/timer in the terminal.

process.stdout.write(`${currentTime}\r`)

and it works perfectly. But when i add it to the chalk-animation syntax like

chalkAnimation.rainbow(process.stdout.write(`${currentTime}\r`));

i get this error

.../node_modules/chalk-animation/index.js:134
		text: str.split(/\r\n|\r|\n/),
		          ^

TypeError: str.split is not a function

Is there any way to do this? Thanks

spidy0x0 avatar Apr 29 '22 17:04 spidy0x0

Hi !

You can replace the text of an animation that has already started.

For example, you can add a dot every second like this :

let str = 'Loading...';
const rainbow = chalkAnimation.rainbow(str); 
setInterval(() => { 	rainbow.replace(str += '.'); }, 1000);

(sorry about formatting, I'm on mobile)

bokub avatar Apr 29 '22 17:04 bokub

hi, i tried doing this

        let left = hours + ":" + minutes + ":" + seconds ;

        const rainbow = chalkAnimation.rainbow(left); 
        setInterval(() => { 	rainbow.replace(left); }, 1000);

but it keep logging on a new line always image and i want the countdown on a single line

spidy0x0 avatar Apr 29 '22 18:04 spidy0x0

this is how i want it asciicast

spidy0x0 avatar Apr 29 '22 18:04 spidy0x0

I found a solution to this.

I created a pr for it #25

spidy0x0 avatar Apr 29 '22 22:04 spidy0x0

Hi,

There is not need to do a PR that changes the whole thing, just use .replace() as I told you before !

For example, here is what I can achieve with the following code:

// server.js
import chalkAnimation from 'chalk-animation';

let seconds = 43;

let left = '06:00:' + seconds;

const rainbow = chalkAnimation.rainbow(left);
setInterval(() => {
  seconds = seconds - 1;
  left = '06:00:' + seconds;
  rainbow.replace(left);
}, 1000);

Peek 2022-05-04 15-21

bokub avatar May 04 '22 13:05 bokub

hi, i tried what you suggested and it wasn't working. I waited for hours for a working example but you weren't available. So i had to find a workaround. Thanks for the POC buddy

spidy0x0 avatar May 04 '22 15:05 spidy0x0

Yeah sorry I'm usually away from my computer on weekends.

Is the last example working for you?

bokub avatar May 04 '22 15:05 bokub