Add optional error callback to vim Defer command
This commit is contained in:
parent
313cefd5d1
commit
f82b2fc418
1 changed files with 38 additions and 15 deletions
|
@ -1,34 +1,57 @@
|
||||||
function s:jobstart(command, options)
|
function s:exit(code, buffer, start, end, callbacks)
|
||||||
if has("nvim")
|
if len(a:callbacks)>1
|
||||||
let a:options["on_exit"] = a:options["close_cb"]
|
let [l:Success, l:Error] = a:callbacks
|
||||||
return jobstart(a:command, a:options)
|
elseif len(a:callbacks)>0
|
||||||
|
let l:Success = a:callbacks[0]
|
||||||
|
let l:Error = a:callbacks[0]
|
||||||
else
|
else
|
||||||
return job_start(a:command, a:options)
|
let l:Success = { a -> a }
|
||||||
|
let l:Error = { a -> a }
|
||||||
end
|
end
|
||||||
|
if (a:code == 0)
|
||||||
|
return l:Success({"output": a:buffer, "tstart": a:start, "tend": a:end, "code": a:code})
|
||||||
|
else
|
||||||
|
return l:Error({"output": a:buffer, "tstart": a:start, "tend": a:end, "code": a:code})
|
||||||
|
endif
|
||||||
endfun
|
endfun
|
||||||
|
|
||||||
function Defer(command, callback)
|
function Defer(command, ...)
|
||||||
let l:start = strftime("%s")
|
let l:start = strftime("%s")
|
||||||
let l:buffer = []
|
let l:buffer = []
|
||||||
call s:jobstart(a:command, {
|
let l:callbacks = a:000
|
||||||
|
if has("nvim")
|
||||||
|
call jobstart(a:command, {
|
||||||
\ "out_io": "pipe",
|
\ "out_io": "pipe",
|
||||||
\ "out_cb": { pipe, text -> add(l:buffer, text) },
|
\ "out_cb": { pipe, text -> add(l:buffer, text) },
|
||||||
\ "close_cb": { pipe -> a:callback({"output": l:buffer, "tstart": l:start, "tend":strftime("%s")}) }
|
\ "on_exit": { id, code -> <SID>exit(code, l:buffer, l:start, strftime("%s"), l:callbacks) }
|
||||||
\ })
|
\ })
|
||||||
|
else
|
||||||
|
call job_start(a:command, {
|
||||||
|
\ "out_io": "pipe",
|
||||||
|
\ "out_cb": { pipe, text -> add(l:buffer, text) },
|
||||||
|
\ "close_cb": { pipe -> <SID>exit(0, l:buffer, l:start, strftime("%s"), a:000) }
|
||||||
|
\ })
|
||||||
|
end
|
||||||
endfun
|
endfun
|
||||||
|
|
||||||
function s:expand(string)
|
function s:expand(string)
|
||||||
return substitute(a:string, '%[:a-z]*', '\=expand(submatch(0))', 'g')
|
return substitute(a:string, '%[:a-z]*', '\=expand(submatch(0))', 'g')
|
||||||
endfun
|
endfun
|
||||||
|
|
||||||
function s:echo(message)
|
function s:echo(message, ...)
|
||||||
|
if (a:0 > 0)
|
||||||
|
exec "echohl ".a:1
|
||||||
|
end
|
||||||
echom a:message
|
echom a:message
|
||||||
|
if (a:0 > 0)
|
||||||
|
echohl None
|
||||||
|
end
|
||||||
endfun
|
endfun
|
||||||
|
|
||||||
function s:notify(message)
|
function s:notify(message)
|
||||||
call Defer('notify-send "Vim" "'.a:message.'"', { b -> 0 })
|
call Defer('notify-send "Vim" "'.a:message.'"', { b -> 0 })
|
||||||
endfun
|
endfun
|
||||||
|
|
||||||
comm -complete=shellcmd -nargs=* Defer call Defer(s:expand(<q-args>), { r -> 0 })
|
comm -complete=shellcmd -nargs=* Defer call Defer(s:expand(<q-args>))
|
||||||
comm -complete=shellcmd -nargs=* DeferEcho call Defer(s:expand(<q-args>), { result -> <SID>echo("Deferred job completed (".(result['tend']-result['tstart'])."s): ".s:expand(<q-args>)) })
|
comm -complete=shellcmd -nargs=* DeferEcho call Defer(s:expand(<q-args>), { result -> <SID>echo("Deferred job completed (".(result['tend']-result['tstart'])."s): ".s:expand(<q-args>)) }, { result -> <SID>echo("Deferred job errored with ".result['code']." (".(result['tend']-result['tstart'])."s): ".s:expand(<q-args>), 'WarningMsg') })
|
||||||
comm -complete=shellcmd -nargs=* DeferNotify call Defer(s:expand(<q-args>), { result -> <SID>notify("Deferred job completed (".(result['tend']-result['tstart'])."s):\n$ ".s:expand(<q-args>)) })
|
comm -complete=shellcmd -nargs=* DeferNotify call Defer(s:expand(<q-args>), { result -> <SID>notify("Deferred job completed (".(result['tend']-result['tstart'])."s):\n$ ".s:expand(<q-args>)) }, { result -> <SID>notify("Deferred job errored with ".result['code']." (".(result['tend']-result['tstart'])."s):\n$ ".s:expand(<q-args>)) })
|
||||||
|
|
Loading…
Reference in a new issue