fix(picker.preview): only show binary message when binary and no ft. Closes #729

This commit is contained in:
Folke Lemaitre 2025-01-28 16:17:58 +01:00
parent a17090d6cd
commit ea838e2838
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040

View file

@ -109,6 +109,11 @@ function M.file(ctx)
local file = assert(io.open(path, "r"))
local is_binary = false
local ft = ctx.picker.opts.previewers.file.ft or vim.filetype.match({ filename = path })
if ft == "bigfile" then
ft = nil
end
local lines = {}
for line in file:lines() do
---@cast line string
@ -117,14 +122,20 @@ function M.file(ctx)
end
-- Check for binary data in the current line
if line:find("[%z\1-\8\11\12\14-\31]") then
ctx.preview:notify("binary file", "warn")
return
is_binary = true
if not ft then
ctx.preview:notify("binary file", "warn")
return
end
end
table.insert(lines, line)
end
file:close()
if is_binary then
ctx.preview:wo({ number = false, relativenumber = false, cursorline = false, signcolumn = "no" })
end
ctx.preview:set_lines(lines)
ctx.preview:highlight({ file = path, ft = ctx.picker.opts.previewers.file.ft, buf = ctx.buf })
end