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

Silence warnings #823

Closed
rien333 opened this issue Feb 10, 2020 · 3 comments
Closed

Silence warnings #823

rien333 opened this issue Feb 10, 2020 · 3 comments
Labels
feature-request New feature or request good first issue Good for newcomers

Comments

@rien333
Copy link

rien333 commented Feb 10, 2020

I use bat to preview files in fzf. When bat is called on a binary file, it will display a warning:

$ bat foo.pdf 2> /dev/null
[bat warning]: Binary content from file 'foo.pdf' will not be printed to the terminal (but will be present if the output of 'bat' is piped). You can use 'bat -A' to show the binary file contents.

As you can see, there is no way to silence this warning. For my specific use case, using bat as a "previewer", I would rather it be silent than that it displays an "error". Would it be a good idea to make it possible to silence warnings somehow?

In terms of implementation, I was thinking one could implement the warnings of bat such they are directed to stderr (seemingly, they are directed to stdout now?). In that way, users that don't need the warnings, can redirect stderr to /dev/null, without a lot of changes to bat itself. Alternatively, one could add a new switch to bat, that makes it less verbose in case of warnings.

edit:

I'm not sure wether it's customary to print warnings to stderr, btw.

@sharkdp
Copy link
Owner

sharkdp commented Feb 10, 2020

Thank you for your feedback.

When bat is called on a binary file, it will display a warning:

This only happens if you disable the "header" component in bats output style. We do this because there would be no output at all for binary file otherwise, which could be confusing if a user unknowingly prints a binary file or calls bat on multiple files.

I am assuming that you have a config file which sets a different default style (or you used the BAT_STYLE environment variable). In this sense, you can "disable" the warning by re-enabling the header style component. That's not a real "fix" of course, more intended as an explanation.

In terms of implementation, I was thinking one could implement the warnings of bat such they are directed to stderr (seemingly, they are directed to stdout now?)

Yes, this is on purpose. We print warnings to stdout because they would interfere with paging support otherwise. They would be printed "in the background" and would not be visible in the pager output.

I guess we could print warnings to stderr if we are not piping to a pager, which would at least solve this particular use case.

Alternatively, one could add a new switch to bat, that makes it less verbose in case of warnings.

That's always an option, but I'd like to avoid introducing a new command-line option, if possible.

@rien333
Copy link
Author

rien333 commented Feb 10, 2020

Thanks for your detailed reply! I

In this sense, you can "disable" the warning by re-enabling the header style component. That's not a real "fix" of course, more intended as an explanation.

I'm pretty much satisfied with your solution, re-enabling headers indeed rids of the warning. My bad for not noticing that.

I guess we could print warnings to stderr if we are not piping to a pager, which would at least solve this particular use case.

I won't have time to implement this in the considerable future (I don't know rust), but might one day implement this, if you think there is no better solution.

(You can close this if you're not super interested, I'm largely satisfied with how things stand now)

@eth-p eth-p added feature-request New feature or request good first issue Good for newcomers labels Feb 15, 2020
@sharkdp
Copy link
Owner

sharkdp commented Mar 6, 2020

Thought about this again. I think we'll leave the current behavior. Changing from stdout to stderr when not printing to a pager could lead to other surprising behaviors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

3 participants