fix: Close termopen jobs when done

TODO: Still need to fix the rest of em. And I see lots of repeated
code... we will need to fix that.

Also adjusted some bat options.
This commit is contained in:
TJ DeVries
2020-09-07 10:47:11 -04:00
parent 1b04f99e42
commit dffcef9470

View File

@@ -10,7 +10,10 @@ local previewers = {}
local Previewer = {} local Previewer = {}
Previewer.__index = Previewer Previewer.__index = Previewer
local bat_options = " --style=grid --paging=always " -- TODO: Should play with these some more, ty @clason
local bat_options = " --style=numbers --plain --paging=never --pager=cat"
bat_options = " --style=grid --plain --pager=cat "
local previewer_ns = vim.api.nvim_create_namespace('telescope.previewers') local previewer_ns = vim.api.nvim_create_namespace('telescope.previewers')
-- --terminal-width=%s -- --terminal-width=%s
@@ -173,14 +176,21 @@ previewers.cat = defaulter(function(opts)
setup = function() setup = function()
local command_string = "cat '%s'" local command_string = "cat '%s'"
if 1 == vim.fn.executable("bat") then if 1 == vim.fn.executable("bat") then
command_string = "bat '%s' --style=grid --paging=always" command_string = "bat '%s' " .. bat_options
end end
return { return {
command_string = command_string command_string = command_string,
termopen_id = nil,
} }
end, end,
teardown = function(self)
if self.state.termopen_id then
pcall(vim.fn.chanclose, self.state.termopen_id)
end
end,
preview_fn = function(self, entry, status) preview_fn = function(self, entry, status)
local bufnr = vim.api.nvim_create_buf(false, true) local bufnr = vim.api.nvim_create_buf(false, true)
@@ -195,7 +205,7 @@ previewers.cat = defaulter(function(opts)
term_opts.cwd = opts.cwd term_opts.cwd = opts.cwd
with_preview_window(status, function() with_preview_window(status, function()
vim.fn.termopen(string.format(self.state.command_string, path), term_opts) self.state.termopen_id = vim.fn.termopen(string.format(self.state.command_string, path), term_opts)
end) end)
vim.api.nvim_buf_set_name(bufnr, tostring(bufnr)) vim.api.nvim_buf_set_name(bufnr, tostring(bufnr))