From d8befe243752bd1b295d84e35a53d16a0de55aa9 Mon Sep 17 00:00:00 2001 From: Simon Hauser Date: Fri, 6 Nov 2020 01:53:57 +0100 Subject: [PATCH] Cat will no longer display binary files (#224) --- lua/telescope/previewers.lua | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lua/telescope/previewers.lua b/lua/telescope/previewers.lua index 8925a9e..0fe2ed2 100644 --- a/lua/telescope/previewers.lua +++ b/lua/telescope/previewers.lua @@ -51,7 +51,16 @@ local bat_maker = function(filename, lnum, start, finish) end -- TODO: Add other options for cat to do this better -local cat_maker = function(filename, lnum, start, finish) +local cat_maker = function(filename, _, _, _) + if vim.fn.executable('file') then + local handle = io.popen('file --mime-type -b ' .. filename) + local mime_type = vim.split(handle:read('*a'), '/')[1] + handle:close() + if mime_type ~= "text" then + return { "echo", "Binary file found. These files cannot be displayed!" } + end + end + return { "cat", "--", filename }