feat: append mode for previewers.utils.job_maker (#372)
This commit is contained in:
@@ -350,12 +350,11 @@ previewers.man = defaulter(function(_)
|
|||||||
define_preview = function(self, entry, status)
|
define_preview = function(self, entry, status)
|
||||||
putils.with_preview_window(status, nil, function()
|
putils.with_preview_window(status, nil, function()
|
||||||
local win_width = vim.api.nvim_win_get_width(self.state.winid)
|
local win_width = vim.api.nvim_win_get_width(self.state.winid)
|
||||||
putils.job_maker({'man', '-P', 'cat', entry.value},
|
putils.job_maker({'man', '-P', 'cat', entry.value}, self.state.bufnr, {
|
||||||
{ ["MANWIDTH"] = win_width },
|
env = { ["MANWIDTH"] = win_width },
|
||||||
entry.value,
|
value = entry.value,
|
||||||
self.state.bufnr,
|
bufname = self.state.bufname
|
||||||
self.state.bufname
|
})
|
||||||
)
|
|
||||||
putils.regex_highlighter(_, 'man')
|
putils.regex_highlighter(_, 'man')
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
@@ -417,7 +416,11 @@ previewers.git_branch_log = defaulter(function(_)
|
|||||||
local cmd = { 'git', '-P', 'log', '--graph', '--pretty=format:%h -%d %s (%cr)',
|
local cmd = { 'git', '-P', 'log', '--graph', '--pretty=format:%h -%d %s (%cr)',
|
||||||
'--abbrev-commit', '--date=relative', entry.value
|
'--abbrev-commit', '--date=relative', entry.value
|
||||||
}
|
}
|
||||||
putils.job_maker(cmd, nil, entry.value, self.state.bufnr, self.state.bufname, highlight_buffer)
|
putils.job_maker(cmd, self.state.bufnr, {
|
||||||
|
value = entry.value,
|
||||||
|
bufname = self.state.bufname,
|
||||||
|
callback = highlight_buffer
|
||||||
|
})
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
@@ -431,12 +434,10 @@ previewers.git_commit_diff = defaulter(function(_)
|
|||||||
|
|
||||||
define_preview = function(self, entry, status)
|
define_preview = function(self, entry, status)
|
||||||
putils.with_preview_window(status, nil, function()
|
putils.with_preview_window(status, nil, function()
|
||||||
putils.job_maker({ 'git', '-P', 'diff', entry.value .. '^!' },
|
putils.job_maker({ 'git', '-P', 'diff', entry.value .. '^!' }, self.state.bufnr, {
|
||||||
nil,
|
value = entry.value,
|
||||||
entry.value,
|
bufname = self.state.bufname
|
||||||
self.state.bufnr,
|
})
|
||||||
self.state.bufname
|
|
||||||
)
|
|
||||||
putils.regex_highlighter(_, 'diff')
|
putils.regex_highlighter(_, 'diff')
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
@@ -451,12 +452,10 @@ previewers.git_file_diff = defaulter(function(_)
|
|||||||
|
|
||||||
define_preview = function(self, entry, status)
|
define_preview = function(self, entry, status)
|
||||||
putils.with_preview_window(status, nil, function()
|
putils.with_preview_window(status, nil, function()
|
||||||
putils.job_maker({ 'git', '-P', 'diff', entry.value },
|
putils.job_maker({ 'git', '-P', 'diff', entry.value }, self.state.bufnr, {
|
||||||
nil,
|
value = entry.value,
|
||||||
entry.value,
|
bufname = self.state.bufname
|
||||||
self.state.bufnr,
|
})
|
||||||
self.state.bufname
|
|
||||||
)
|
|
||||||
putils.regex_highlighter(_, 'diff')
|
putils.regex_highlighter(_, 'diff')
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -22,20 +22,30 @@ end
|
|||||||
|
|
||||||
-- API helper functions for buffer previewer
|
-- API helper functions for buffer previewer
|
||||||
--- Job maker for buffer previewer
|
--- Job maker for buffer previewer
|
||||||
utils.job_maker = function(cmd, env, value, bufnr, bufname, callback)
|
utils.job_maker = function(cmd, bufnr, opts)
|
||||||
if bufname ~= value then
|
opts = opts or {}
|
||||||
|
opts.mode = opts.mode or "insert"
|
||||||
|
-- bufname and value are optional
|
||||||
|
-- if passed, they will be use as the cache key
|
||||||
|
-- if any of them are missing, cache will be skipped
|
||||||
|
if opts.bufname ~= opts.value or not opts.bufname or not opts.value then
|
||||||
local command = table.remove(cmd, 1)
|
local command = table.remove(cmd, 1)
|
||||||
Job:new({
|
Job:new({
|
||||||
command = command,
|
command = command,
|
||||||
args = cmd,
|
args = cmd,
|
||||||
env = env,
|
env = opts.env,
|
||||||
on_exit = vim.schedule_wrap(function(j)
|
on_exit = vim.schedule_wrap(function(j)
|
||||||
|
if opts.mode == "append" then
|
||||||
|
local count = vim.api.nvim_buf_line_count(bufnr)
|
||||||
|
vim.api.nvim_buf_set_lines(bufnr, count, -1, false, j:result())
|
||||||
|
elseif opts.mode == "insert" then
|
||||||
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, j:result())
|
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, j:result())
|
||||||
if callback then callback(bufnr, j:result()) end
|
end
|
||||||
|
if opts.callback then opts.callback(bufnr, j:result()) end
|
||||||
end)
|
end)
|
||||||
}):start()
|
}):start()
|
||||||
else
|
else
|
||||||
if callback then callback(bufnr) end
|
if opts.callback then opts.callback(bufnr) end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user