Bat: Pipe image into bat gives `stream did not contain valid UTF-8`

Created on 23 May 2018  Â·  6Comments  Â·  Source: sharkdp/bat

cat myimage.png | base64 works a treat. Using bat instead errors with [bat error]: stream did not contain valid UTF-8

bug

Most helpful comment

After some major refactorings, I was able to implement the first part (pipe through binary data 1:1) in 226d9a573acb8025278d7afc19359ca167841536.

So this works now:

â–¶ bat test.png | base64 
iVBORw0KGgoAAAANSUhEUgAAAIAAAABECAIAAADGJao+AAAAwklEQVR4Xu3UgQbDMBRA0bc03f//
b7N0VuqJEmwoc+KqNEkDh9b+2HuJu1KNO4f+AQCAAAAQAAACAEAAAAgAAAEAIAAABACAAAAQAAAC
AEAAAAgAAAEAIAAAANReamRLlPWYfNH0klxcPs+cP3NxWF+vi3lb7pa2R+vx6tHOtuN1O+a5lY3H
zgM5ya/GM5N7ZjfPq7/5yS8IgAAAEAAAAgBAAAAIAAABACAAAAQAgAAAEAAAAgBAAAAIAAABACAA
AIw322gDIPvtlmUAAAAASUVORK5CYII=

â–¶ md5sum test.png
0d7e0edf18a31b2576074e0dbd5933c4  test.png

â–¶ bat test.png | md5sum 
0d7e0edf18a31b2576074e0dbd5933c4  -

The second part (detecting binary files in interactive mode) should be easy to implement with the content_inspector crate.

All 6 comments

Binary data is currently not handled properly in bat (this is already addressed in #134, point "The standard output shall contain the sequence of bytes read from the input files. Nothing else shall be written to the standard output.").

I currently think the best way to proceed here would be to:

  • Pipe through binary data 1:1 if we are writing to a pipe/file.
  • Show a user-friendly error message ("Skipping binary file" or similar) if we are writing to an interactive terminal.

What do you think?

Yeah, that makes sense. Sorta matches what httpie does: https://github.com/jakubroztocil/httpie/blob/master/README.rst#binary-data

In order to start with this, I have created this small Rust library: content_inspector (docs).

After some major refactorings, I was able to implement the first part (pipe through binary data 1:1) in 226d9a573acb8025278d7afc19359ca167841536.

So this works now:

â–¶ bat test.png | base64 
iVBORw0KGgoAAAANSUhEUgAAAIAAAABECAIAAADGJao+AAAAwklEQVR4Xu3UgQbDMBRA0bc03f//
b7N0VuqJEmwoc+KqNEkDh9b+2HuJu1KNO4f+AQCAAAAQAAACAEAAAAgAAAEAIAAABACAAAAQAAAC
AEAAAAgAAAEAIAAAANReamRLlPWYfNH0klxcPs+cP3NxWF+vi3lb7pa2R+vx6tHOtuN1O+a5lY3H
zgM5ya/GM5N7ZjfPq7/5yS8IgAAAEAAAAgBAAAAIAAABACAAAAQAgAAAEAAAAgBAAAAIAAABACAA
AIw322gDIPvtlmUAAAAASUVORK5CYII=

â–¶ md5sum test.png
0d7e0edf18a31b2576074e0dbd5933c4  test.png

â–¶ bat test.png | md5sum 
0d7e0edf18a31b2576074e0dbd5933c4  -

The second part (detecting binary files in interactive mode) should be easy to implement with the content_inspector crate.

I'm going to close this ticket as the reported bug has been fixed. I will open a new ticket that tracks the second part.

Sounds good!

For prosperity, see #248

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mjlbach picture mjlbach  Â·  3Comments

samuelcolvin picture samuelcolvin  Â·  3Comments

adamtabrams picture adamtabrams  Â·  3Comments

yum-feng picture yum-feng  Â·  3Comments

jkaan picture jkaan  Â·  3Comments