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

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

Merged
merged 1 commit into from
May 24, 2024

Conversation

arcstur
Copy link
Contributor

@arcstur arcstur commented May 23, 2024

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

This seems to be the source of the issue
rust-lang/rust#46016

@arcstur
Copy link
Contributor Author

arcstur commented May 23, 2024

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

@shantanuraj
Copy link
Member

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

arcstur commented May 24, 2024

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

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

arcstur commented May 24, 2024

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

With head -1
image

Copy link
Member

@shantanuraj shantanuraj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm; I'll fix the other panic with the keychain and create a release

@shantanuraj shantanuraj merged commit 4048970 into watercooler-labs:main May 24, 2024
5 checks passed
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

Successfully merging this pull request may close these issues.

2 participants