diff options
Diffstat (limited to '.vim/lua/internal')
| -rw-r--r-- | .vim/lua/internal/job.lua | 65 | ||||
| -rw-r--r-- | .vim/lua/internal/misc.lua | 10 |
2 files changed, 37 insertions, 38 deletions
diff --git a/.vim/lua/internal/job.lua b/.vim/lua/internal/job.lua index c85fd67..5e3b6e1 100644 --- a/.vim/lua/internal/job.lua +++ b/.vim/lua/internal/job.lua @@ -9,6 +9,21 @@ local strip_ansi = function(line) line, _ = line:gsub(string.char(27) .. '[[0-9;]*m', '') return line end +local setqf = function(opts, pid, data) + if data == nil then + return + end + vim.fn.setqflist({}, 'a', { + title = opts.format_title, + lines = {strip_ansi(data)}, + efm = '%m', + context = { + cmd = {opts.command, unpack(opts.args)}, + pid = pid, + source = 'jobstart', + } + }) +end --create a new job and register --@return ~/.local/share/nvim/site/pack/packer/start/plenary.nvim/lua/plenary/job.lua @@ -18,14 +33,16 @@ function M.jobstart(opts) -- default options -- opts.enable_recording = true opts.enable_spinner = (opts.enable_spinner or true) and spinner_ok - opts.populate_quickfix = opts.populate_quickfix or true - - local title - if opts.format_title then - title = opts.format_title(opts.command, opts.args) - opts.format_title = nil + opts.populate_quickfix = opts.populate_quickfix or 'onerror' + + if opts.format_title == nil then + opts.format_title = table.concat({opts.command, unpack(opts.args)}, ' ') + elseif type(opts.format_title) == 'function' then + opts.format_title = opts.format_title(opts.command, opts.args) + elseif type(opts.format_title) == 'string' then + -- noop else - title = table.concat({opts.command, unpack(opts.args)}, ' ') + error(string.format('Invalid field "format_title" of type %s', type(opts.format_title))) end opts.on_start = vim.schedule_wrap(function(j) @@ -35,7 +52,7 @@ function M.jobstart(opts) vim.fn.setqflist({}, 'r') -- clear quickfix end if opts.enable_spinner then - spinner.on_attach(j.pid, title, vim.fn.bufnr()) + spinner.on_attach(j.pid, opts.format_title, vim.fn.bufnr()) spinner.on_progress('begin', j.pid, j.pid) end end) @@ -46,13 +63,13 @@ function M.jobstart(opts) if not (code == 0) then vim.notify(string.format('[%d] exit %d', j.pid, code), vim.log.levels.ERROR) end - if opts.populate_quickfix then - vim.cmd.doautocmd [[QuickFixCmdPost]] - end if opts.enable_spinner then spinner.on_progress('end', j.pid, j.pid) spinner.on_exit(nil, nil, j.pid) end + if opts.populate_quickfix then + vim.cmd.doautocmd [[QuickFixCmdPost]] + end end) opts.on_stdout = vim.schedule_wrap(function(err, data, j) @@ -60,17 +77,8 @@ function M.jobstart(opts) vim.notify(string.format('[%d] error: %s', j.pid, err), vim.log.levels.ERROR) end -- handle error? - if opts.populate_quickfix and data ~= nil then - vim.fn.setqflist({}, 'a', { - title = title, - lines = {strip_ansi(data)}, - efm = '%m', - context = { - cmd = {opts.command, unpack(opts.args)}, - pid = j.pid, - source = 'jobstart', - } - }) + if opts.populate_quickfix and opts.populate_quickfix ~= 'onerror' then + setqf(opts, j.pid, data) end end) @@ -79,17 +87,8 @@ function M.jobstart(opts) vim.notify(string.format('[%d] error: %s', j.pid, err), vim.log.levels.ERROR) end -- handle error? - if opts.populate_quickfix and data ~= nil then - vim.fn.setqflist({}, 'a', { - title = title, - lines = {strip_ansi(data)}, - efm = '%m', - context = { - cmd = {opts.command, unpack(opts.args)}, - pid = j.pid, - source = 'jobstart', - } - }) + if opts.populate_quickfix then + setqf(opts, j.pid, data) end end) diff --git a/.vim/lua/internal/misc.lua b/.vim/lua/internal/misc.lua index 5a2ae21..6f79508 100644 --- a/.vim/lua/internal/misc.lua +++ b/.vim/lua/internal/misc.lua @@ -187,15 +187,14 @@ function M.link_preview() local buf = vim.api.nvim_get_current_buf() local ln = (vim.api.nvim_win_get_cursor(0)[1]) - 1 - local j = require('job').jobstart({ + require('job').jobstart({ format_title = function() return "link-preview" end, command = vim.env.SHELL, args = {'-c', [[ curl -sSfL ]] .. link .. [[ | grep -iPo '(?<=property="og:title" content=")([^\"]+)(?=")' ]]}, populate_quickfix = false, enable_recording = true, - }) - - j:after_success(vim.schedule_wrap(function(j, code, _) + }):after_success( + vim.schedule_wrap(function(j, code, _) local results = j:result() if not code == 0 or #results == 0 then return end @@ -204,7 +203,8 @@ function M.link_preview() virt_text = {{results[1], 'Error'}}, virt_text_pos = 'eol' }) - end)) + end) + ) end return M |