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

Previewing binary files? #800

Closed
for-coursera opened this issue Jan 15, 2020 · 12 comments
Closed

Previewing binary files? #800

for-coursera opened this issue Jan 15, 2020 · 12 comments
Labels
feature-request New feature or request

Comments

@for-coursera
Copy link

Some option allowing to get a glimpse of binary files would be great. May be some first N bytes in hexdump style as mentioned here, may be just a few lines of the cat-like output.

What do you think?

@eth-p eth-p added the feature-request New feature or request label Jan 17, 2020
@eth-p
Copy link
Collaborator

eth-p commented Jan 17, 2020

Related to: #619

@for-coursera
Copy link
Author

@eth-p

Not really. --show-all affects the way non-binary files are displayed, too.

@eth-p
Copy link
Collaborator

eth-p commented Jan 18, 2020

The initial issue of using hexyl for hex-dump previews is related to this one, and I linked it for reference in case any of the previous discussion was useful. But yeah, I don't think the resolution for that issue solves this one.

@desbma
Copy link

desbma commented Mar 29, 2020

It was suggested here #619 (comment) that having a BAT_BINARY_VIEWER would be an elegant solution to this.

I would also like this to be implemented, because I sometimes use bat for example to preview archive content, and because the data is coming from a pipe, I have no way to choose whether to preview it with bat or hexyl.

bat -A changes the output for text files and does not make binary files any more "readable".

So in that case, BAT_BINARY_VIEWER=hexyl bat would be perfect.

@sharkdp
Copy link
Owner

sharkdp commented Sep 20, 2020

I'd rather not implement this. It's easy to write a wrapper script that would do this.

@sharkdp sharkdp closed this as completed Sep 20, 2020
@SuperSandro2000
Copy link

I'd rather not implement this. It's easy to write a wrapper script that would do this.

If the exit code would be different from 0. Right now it is just 0 which makes writting a generic wrapper harder.

@desbma
Copy link

desbma commented Sep 22, 2020

It's easy to write a wrapper script that would do this.

When reading data from stdin it's definitely not trivial. You'd have to :

  • read an arbitrary amount of data from the pipe to guess if it's binary or not, hoping it does end in the middle of an unicode char, and essentially duplicating the detection bat already does
  • once you have guessed if its binary or not, you can not just execvp to bat or hexyl, as the data that has already been read would be lost, so you need to create another pipe, write to it the first chunk, and then proxy between the wrapper's stdin and bat/hexyl
  • handle the SIGCHLD signal
  • probably more I forgot

I have written such script, and it is near 100 lines of Python, and very far from handling all corner cases.

It's too bad you don't want to consider a solution for this as you are in a special position to do so as the author of both bat and hexyl.

@SuperSandro2000
Copy link

SuperSandro2000 commented Sep 22, 2020

For catting a file it is easier. You can extract the filename, check with isutf8 what it is and then handle it appropriate.

@xeruf
Copy link
Contributor

xeruf commented Dec 7, 2021

I have written a wrapper script to preview almost anything using bat, ls and some extra tools - hexyl is not integrated yet, but that sounds great, too.
https://git.jfischer.org/xeruf/dotfiles/src/branch/main/.local/bin/scripts/b

I am considering to publish it at some point as I am dazzled to not have found anything like that so far.

@desbma
Copy link

desbma commented Dec 7, 2021

I also wrote rsop as general wrapper for terminal tools like bat and hexyl.
The initial motivation was precisely this issue, for example when previewing an archive and not knowing if the data is text or binary. Then it evolved into a complete xdg-open replacement and more.

@xeruf
Copy link
Contributor

xeruf commented Dec 7, 2021

@desbma Wow I had been searching for such a tool for so long until building it myself, this looks fantastic :) and maybe I can contribute something to it as well

@desbma
Copy link

desbma commented Dec 7, 2021

Yeah I wrote it for myself with this issue in mind, but now it can do much more than text/binary dispatch because it can sniff MIME type, including when piped, so you can preview images, pcap files, whatever.

You should be able to customize it for your needs with the config file, but if not, contribution welcome!

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
Projects
None yet
Development

No branches or pull requests

6 participants