watercooler-labs / toggl-cli

🕐 Command Line App for Toggl Track
MIT License
31 stars 7 forks source link

fix: use BufWriter in list command to avoid println! in a loop #75

Closed arcstur closed 3 months ago

arcstur commented 3 months ago

Piping the output of toggl list would panic, probably because of calling println! in a loop, which we should not do:

https://rust-cli.github.io/book/tutorial/output.html#a-note-on-printing-performance

I updated the list command code to use a BufWriter and writeln!. I used expect to deal with writeln errors. I think we could have an error type in the future that absorbs io::Error, what do you think?

Anyway, with this the list command stopped panicking when piping.

shantanuraj commented 3 months ago

This seems to be the source of the issue https://github.com/rust-lang/rust/issues/46016

arcstur commented 3 months ago

I see, makes sense! But I think it's still better to use writeln! than println! in the loop, right?

shantanuraj commented 3 months ago

Yeah I was just curious about the source of the problem.

Could you share what you're piping toggl list to?

This is the my comparison of main and your pr image

arcstur commented 3 months ago

When piping to head it panics for me:

toggl list | head
... # my time entries
thread 'main' panicked at library/std/src/io/stdio.rs:1030:9:
failed printing to stdout: Broken pipe (os error 32)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
shantanuraj commented 3 months ago

Hmm I can't reproduce the panic on main

image

But I'm good with the fix if it works for you. For reference what operating system / arch are you using?

arcstur commented 3 months ago

I think that's becasuse you have only a few items in there. It panicks when there are more than head needs. If I do head -n 100 it does not panic. Try doing toggl list | head -1

I'm using Arch 6.9.1-arch1-1

shantanuraj commented 3 months ago

With head -1

image