From ad4e168e994dfd3e1a0597079d5776ad4b8d5401 Mon Sep 17 00:00:00 2001 From: bhagwan Date: Sat, 5 Mar 2022 23:05:41 -0800 Subject: [PATCH] buitin previewer: do not preview file size>10MB (configurable) --- README.md | 1 + doc/fzf-lua.txt | 1 + lua/fzf-lua/config.lua | 3 ++- lua/fzf-lua/previewer/builtin.lua | 30 +++++++++++++++++++++--------- 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 27ced16..c20e8c9 100644 --- a/README.md +++ b/README.md @@ -434,6 +434,7 @@ require'fzf-lua'.setup { syntax = true, -- preview syntax highlight? syntax_limit_l = 0, -- syntax limit (lines), 0=nolimit syntax_limit_b = 1024*1024, -- syntax limit (bytes), 0=nolimit + limit_b = 1024*1024*10, -- preview limit (bytes), 0=nolimit }, }, -- provider setup diff --git a/doc/fzf-lua.txt b/doc/fzf-lua.txt index 034d04c..d40d7da 100644 --- a/doc/fzf-lua.txt +++ b/doc/fzf-lua.txt @@ -476,6 +476,7 @@ Consult the list below for available settings: syntax = true, -- preview syntax highlight? syntax_limit_l = 0, -- syntax limit (lines), 0=nolimit syntax_limit_b = 1024*1024, -- syntax limit (bytes), 0=nolimit + limit_b = 1024*1024*10, -- preview limit (bytes), 0=nolimit }, }, -- provider setup diff --git a/lua/fzf-lua/config.lua b/lua/fzf-lua/config.lua index 32f5910..4d61ebc 100644 --- a/lua/fzf-lua/config.lua +++ b/lua/fzf-lua/config.lua @@ -178,7 +178,8 @@ M.globals = { syntax = true, syntax_delay = 0, syntax_limit_l = 0, - syntax_limit_b = 1024*1024, + syntax_limit_b = 1024*1024, -- 1MB + limit_b = 1024*1024*10, -- 10MB _ctor = previewers.builtin.buffer_or_file, }, }, diff --git a/lua/fzf-lua/previewer/builtin.lua b/lua/fzf-lua/previewer/builtin.lua index 69ab1fa..ea01798 100644 --- a/lua/fzf-lua/previewer/builtin.lua +++ b/lua/fzf-lua/previewer/builtin.lua @@ -22,6 +22,7 @@ function Previewer.base:new(o, opts, fzf_win) self.syntax_delay = o.syntax_delay self.syntax_limit_b = o.syntax_limit_b self.syntax_limit_l = o.syntax_limit_l + self.limit_b = o.limit_b self.backups = {} return self end @@ -304,16 +305,27 @@ function Previewer.buffer_or_file:populate_preview_buf(entry_str) entry.path = path.relative(vim.api.nvim_buf_get_name(entry.bufnr), vim.loop.cwd()) end -- make sure the file is readable (or bad entry.path) - if not entry.path or not vim.loop.fs_stat(entry.path) then return end + local fs_stat = vim.loop.fs_stat(entry.path) + if not entry.path or not fs_stat then return end local tmpbuf = self:get_tmp_buffer() - if utils.perl_file_is_binary(entry.path) then - vim.api.nvim_buf_set_lines(tmpbuf, 0, -1, false, { - "Preview is not supported for binary files." - }) - -- swap preview buffer with new one - self:set_preview_buf(tmpbuf) - self:preview_buf_post(entry) - return + do + local lines = nil + if utils.perl_file_is_binary(entry.path) then + lines = { "Preview is not supported for binary files." } + elseif tonumber(self.limit_b)>0 and fs_stat.size>self.limit_b then + lines = { + ("Preview file size limit (>%dMB) reached, file size %dMB.") + :format(self.limit_b/(1024*1024), fs_stat.size/(1024*1024)), + -- "(configured via 'previewers.builtin.limit_b')" + } + end + if lines then + vim.api.nvim_buf_set_lines(tmpbuf, 0, -1, false, lines) + -- swap preview buffer with new one + self:set_preview_buf(tmpbuf) + self:preview_buf_post(entry) + return + end end -- read the file into the buffer utils.read_file_async(entry.path, vim.schedule_wrap(function(data)