diff --git a/doc/telescope.txt b/doc/telescope.txt index a2b4b0a587..f0bc21a0be 100644 --- a/doc/telescope.txt +++ b/doc/telescope.txt @@ -2296,9 +2296,9 @@ utils.transform_path({opts}, {path}) *telescope.utils.transform_path()* Parameters: ~ - {opts} (table) The opts the users passed into the picker. Might - contains a path_display key - {path} (string?) The path that should be formatted + {opts} (table) The opts the users passed into the picker. Might + contains a path_display key + {path} (string|nil) The path that should be formatted Return: ~ string: The transformed path ready to be displayed diff --git a/lua/telescope/previewers/buffer_previewer.lua b/lua/telescope/previewers/buffer_previewer.lua index dfb11eb7cf..8d58e2e133 100644 --- a/lua/telescope/previewers/buffer_previewer.lua +++ b/lua/telescope/previewers/buffer_previewer.lua @@ -192,9 +192,8 @@ previewers.file_maker = function(filepath, bufnr, opts) else if opts.preview.check_mime_type == true and has_file and opts.ft == "" then -- avoid SIGABRT in buffer previewer happening with utils.get_os_command_output - local output = capture(string.format([[file --mime-type -b "%s"]], filepath)) - local mime_type = vim.split(output, "/") - if mime_type[1] ~= "text" and mime_type[1] ~= "inode" and mime_type[2] ~= "json" then + local mime_type = capture(string.format([[file --mime-type -b "%s"]], filepath)) + if putils.binary_mime_type(mime_type) then if type(opts.preview.mime_hook) == "function" then vim.schedule_wrap(opts.preview.mime_hook)(filepath, bufnr, opts) else diff --git a/lua/telescope/previewers/term_previewer.lua b/lua/telescope/previewers/term_previewer.lua index 9eb5c38536..d0357ea03b 100644 --- a/lua/telescope/previewers/term_previewer.lua +++ b/lua/telescope/previewers/term_previewer.lua @@ -3,6 +3,7 @@ local utils = require "telescope.utils" local Path = require "plenary.path" local from_entry = require "telescope.from_entry" local Previewer = require "telescope.previewers.previewer" +local putil = require "telescope.previewers.utils" local defaulter = utils.make_default_callable @@ -65,9 +66,8 @@ local cat_maker = function(filename, _, start, _) end if 1 == vim.fn.executable "file" then - local output = utils.get_os_command_output { "file", "--mime-type", "-b", filename } - local mime_type = vim.split(output[1], "/")[1] - if mime_type ~= "text" then + local mime_type = utils.get_os_command_output({ "file", "--mime-type", "-b", filename })[1] + if putil.binary_mime_type(mime_type) then return { "echo", "Binary file found. These files cannot be displayed!" } end end diff --git a/lua/telescope/previewers/utils.lua b/lua/telescope/previewers/utils.lua index 746f5727eb..d8348373f3 100644 --- a/lua/telescope/previewers/utils.lua +++ b/lua/telescope/previewers/utils.lua @@ -212,4 +212,21 @@ utils.set_preview_message = function(bufnr, winid, message, fillchar) end end +--- Check if mime type is binary. +--- NOT an exhaustive check, may get false negatives. Ideally should check +--- filetype with `vim.filetype.match` or `filetype_detect` first for filetype +--- info. +---@param mime_type string +---@return boolean +utils.binary_mime_type = function(mime_type) + local type_, subtype = unpack(vim.split(mime_type, "/")) + if vim.tbl_contains({ "text", "inode" }, type_) then + return false + end + if vim.tbl_contains({ "json", "javascript" }, subtype) then + return false + end + return true +end + return utils