-
Notifications
You must be signed in to change notification settings - Fork 15
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
Skip preview for binary files #62
Conversation
9a24f0d
to
c97ecab
Compare
c97ecab
to
098616d
Compare
098616d
to
a76e07d
Compare
Did some testing with Leaderf, Telescope and the Fuzzyy preview window... Leaderf and Telescope have similar problems with UTF-16 and UTF-32 encoded files, even with BOMs, though they deal with them slightly differently (Leaderf detects as binary, Telescope tries to render, but as UTF-8, so with a load of NUL byte marker garbage). The Fuzzyy preview window works the same way as Leaderf and Telescope, reading files raw and populating the buffer, so has the same issue trying to render files encoded in a different encoding to the default encoding (before adding the binary check, it will include the NUL byte marker garbage, just as Telescope does, after it will just show the binary file warning). Trying to render files with NUL types as UTF-8 also seems to be very slow, so I think the binary file warning is far better. |
8ed575c
to
169ae8a
Compare
169ae8a
to
ff90d88
Compare
Previewing binary files as text can mess up the screen, characters can get rendered outside of the preview window which are not later cleared when the popups are closed. It also looks pretty awful. This adds a simple check for a NUL byte in the first 128 bytes, which is fast and should be enough to detect a binary file. This is similar to how ripgrep and other programs typically detect a binary, though they may read the entire file. The 128 bytes might seem arbitrary, but it comes from Leaderf, added in September 2023 and unchanged since then, so presumably works well enough. Telescope.nvim does things differently, actually running the file program to detect a mime type, but I wanted to avoid that, at least until trying the simpler NUL byte check. Note: as this code reads the first 128 bytes in binary mode and does no encoding conversion, it will generate false positives for file encodings that allow NUL bytes, like UTF-16 and UTF-32. This is IMHO an acceptable trade-off to keep the code simple, Leaderf also detects encodings that allow NUL bytes as binary, Telescope.nvim renders them as UTF-8, so with a load of NUL byte marker garbage, git even considers them to be binary. Rendering them in the Fuzzyy preview window would also include NUL byte marker garbage as the file is read raw, without any encoding conversion. Also note that this does not currently apply to the grep preview because all the supported grep programs are configured to skip binaries anyway.
ff90d88
to
a730715
Compare
Previewing binary files as text can mess up the screen, characters can
get rendered outside of the preview window which are not later cleared
when the popups are closed. It also looks pretty awful.
This adds a simple check for a NUL byte in the first 128 bytes, which is
fast and should be enough to detect a binary file. This is similar to
how ripgrep and other grep programs typically detect a binary, though
they may read the entire file. The 128 bytes might seem arbitrary, but
it comes from Leaderf, added in September 2023 and unchanged since then,
so presumably works well enough. Telescope.nvim does things differently,
actually running the file program to detect a mime type, but I wanted to
avoid that, at least until trying the simpler NUL byte check.
Note: as this code reads the first 128 bytes in binary mode and does no
encoding conversion, it will generate false positives for file encodings
that allow NUL bytes, like UTF-16 and UTF-32. This is IMHO an acceptable
trade-off to keep the code simple, Leaderf also detects encodings that
allow NUL bytes as binary, Telescope.nvim renders them as UTF-8 (the
default encoding in Neovim), so with a load of NUL byte marker garbage.
Rendering them in the Fuzzyy preview window would also include NUL byte
marker garbage as the file is read raw, without any encoding conversion.
Also note that this does not currently apply to the grep preview because
all the supported grep programs are configured to skip binaries anyway.
Before
![before](https://private-user-images.githubusercontent.com/138597/404678811-2363c56d-83b0-4b03-82aa-39479a43e8c3.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MzkyMzk3NzcsIm5iZiI6MTczOTIzOTQ3NywicGF0aCI6Ii8xMzg1OTcvNDA0Njc4ODExLTIzNjNjNTZkLTgzYjAtNGIwMy04MmFhLTM5NDc5YTQzZThjMy5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjUwMjExJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI1MDIxMVQwMjA0MzdaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVy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