disable preview for binary files (closes issue #157)

main
bhagwan 3 years ago
parent 7a554bb2b2
commit 28f3bcac27

@ -257,6 +257,13 @@ function Previewer.buffer_or_file:populate_preview_buf(entry_str)
self.preview_bufloaded = true
-- make sure the file is readable (or bad entry.path)
if not vim.loop.fs_stat(entry.path) then return end
if utils.file_is_binary(entry.path) then
vim.api.nvim_buf_set_lines(self.preview_bufnr, 0, -1, false, {
"Preview is not supported for binary files."
})
self:preview_buf_post(entry)
return
end
-- read the file into the buffer
utils.read_file_async(entry.path, vim.schedule_wrap(function(data)
if not vim.api.nvim_buf_is_valid(self.preview_bufnr) then

@ -78,6 +78,16 @@ function M.sk_escape(str)
end)
end
M.file_is_binary = function(filepath)
filepath = vim.fn.expand(filepath)
if vim.fn.executable("file") ~= 1 or
not vim.loop.fs_stat(filepath) then
return false
end
local out = M.io_system("file --dereference --mime " .. filepath)
return out:match("charset=binary") ~= nil
end
M.read_file = function(filepath)
local fd = vim.loop.fs_open(filepath, "r", 438)
if fd == nil then return '' end

Loading…
Cancel
Save