From cfd088051c03de20340d7785f9458e800acdc14d Mon Sep 17 00:00:00 2001 From: Tovi Jaeschke-Rogers Date: Sat, 23 Sep 2023 11:58:09 +0930 Subject: [PATCH] Fix large file preview on telescope --- .config/nvim/lua/config/telescope.lua | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/.config/nvim/lua/config/telescope.lua b/.config/nvim/lua/config/telescope.lua index b160909..89c53f9 100644 --- a/.config/nvim/lua/config/telescope.lua +++ b/.config/nvim/lua/config/telescope.lua @@ -1,21 +1,32 @@ -local pickers = require('telescope.pickers') -local finders = require('telescope.finders') local previewers = require('telescope.previewers') -local action_state = require('telescope.actions.state') -local conf = require('telescope.config').values +local previewers_utils = require('telescope.previewers.utils') local builtin = require('telescope.builtin') local sorters = require('telescope.sorters') local actions = require('telescope.actions') +local max_size = 100000 +local truncate_large_files = function(filepath, bufnr, opts) + opts = opts or {} + + filepath = vim.fn.expand(filepath) + vim.loop.fs_stat(filepath, function(_, stat) + if not stat then return end + if stat.size > max_size then + local cmd = {"head", "-c", max_size, filepath} + previewers_utils.job_maker(cmd, bufnr, opts) + else + previewers.buffer_previewer_maker(filepath, bufnr, opts) + end + end) +end + require('telescope').setup({ defaults = { file_sorter = sorters.get_fzy_sorter, prompt_prefix = ' >', color_devicons = true, - file_previewer = previewers.vim_buffer_cat.new, - grep_previewer = previewers.vim_buffer_vimgrep.new, - qflist_previewer = previewers.vim_buffer_qflist.new, + buffer_previewer_maker = truncate_large_files, initial_mode = "insert", selection_strategy = "reset",