restored preview binary test using perl '-B'

main
bhagwan 3 years ago
parent 9454bcbad9
commit 510aca8658

@ -270,15 +270,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
-- TODO: why does `file --dereference --mime` returns
-- wrong result for some lua files ('charset=binary')?
--[[ if utils.file_is_binary(entry.path) then
if utils.perl_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 ]]
end
-- enable syntax highlighting
if self.syntax then
if self.syntax_delay > 0 then

@ -78,16 +78,32 @@ function M.sk_escape(str)
end)
end
-- TODO: why does `file --dereference --mime` return
-- wrong result for some lua files ('charset=binary')?
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)
local out = M.io_system("file --dereference --mime " ..
vim.fn.shellescape(filepath))
return out:match("charset=binary") ~= nil
end
M.perl_file_is_binary = function(filepath)
filepath = vim.fn.expand(filepath)
if vim.fn.executable("perl") ~= 1 or
not vim.loop.fs_stat(filepath) then
return false
end
-- can also use '-T' to test for text files
-- `perldoc -f -x` to learn more about '-B|-T'
M.io_system("perl -E 'exit((-B $ARGV[0])?0:1);' " ..
vim.fn.shellescape(filepath))
return not M.shell_error()
end
M.read_file = function(filepath)
local fd = vim.loop.fs_open(filepath, "r", 438)
if fd == nil then return '' end

Loading…
Cancel
Save