Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support "process.stdout.write" in logging #24

Open
spidy0x0 opened this issue Apr 29, 2022 · 7 comments
Open

Support "process.stdout.write" in logging #24

spidy0x0 opened this issue Apr 29, 2022 · 7 comments

Comments

@spidy0x0
Copy link

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

@bokub
Copy link
Owner

bokub commented Apr 29, 2022

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)

@spidy0x0
Copy link
Author

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
Copy link
Author

this is how i want it
asciicast

@spidy0x0
Copy link
Author

spidy0x0 commented Apr 29, 2022

I found a solution to this.

I created a pr for it #25

@bokub
Copy link
Owner

bokub commented May 4, 2022

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

@spidy0x0
Copy link
Author

spidy0x0 commented May 4, 2022

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

@bokub
Copy link
Owner

bokub commented May 4, 2022

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

Is the last example working for you?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants