atanunq / viu

Terminal image viewer with native support for iTerm and Kitty
MIT License
2.5k stars 54 forks source link

Use read instead of read_exact during image format guessing #78

Closed DavidBuchanan314 closed 3 years ago

DavidBuchanan314 commented 3 years ago

Fixes #77

For files shorter than 20 bytes, the remaining bytes in the buffer will stay zeroes. The format detection logic seems to be fine with this.

One side-effect of this change is that attempting to open a zero-length file will now error with:

Image(Unsupported(UnsupportedError { format: Unknown, kind: Format(Unknown) }))

Instead of the original behavior:

IO(Custom { kind: UnexpectedEof, error: "failed to fill whole buffer" })

IMHO this is fine, although a check could be added to make sure that a non-zero number of bytes were read.

DavidBuchanan314 commented 3 years ago

Oops, I should have checked for linter warnings first

atanunq commented 3 years ago

Thank you for the fix! Is there a reason why you check for == 0? I don't think that brings any useful information. Returning Unsupported on empty files sounds fine to me.

DavidBuchanan314 commented 3 years ago

Oops, that was actually a mistake, I left that in after pondering an if statement, then changed my mind.