skywind3000 / asyncrun.vim

:rocket: Run Async Shell Commands in Vim 8.0 / NeoVim and Output to the Quickfix Window !!
https://www.vim.org/scripts/script.php?script_id=5431
MIT License
1.84k stars 109 forks source link

call callback function with output #197

Open man9ourah opened 4 years ago

man9ourah commented 4 years ago

Hello,

Thanks for an Awesome plugin!

Is there a way to specify a callback function that will be called with the command output as an argument after the command finishes. Probably passing Funcref as an argument to asyncrun#run.

For example, something like:

call asyncrun#run("", {"callback": "g:AsyncGitCallback"}, cmd)

Then, function g:AsyncGitCallback(output) should be called with the argument output being the command output.

I feel it is more straightforward and suitable than having to read the quickfix in the callback function using post=call\ AsyncGitCallback()

Thanks!

Th3Whit3Wolf commented 4 years ago

I would very much like this, I am trying to lazyload vim-gfm-syntax only for markdown files in git repositories that are being pushed to github.

function! s:CheckIfGithub()
    let cwd = getcwd()
    let parent_dir = expand('%:p:h')
    exe 'cd ' . parent_dir
    " call asyncrun#run('', {'mode': 'async', 'cwd': '<root>', 'raw': 1},'git remote show origin | grep "Push  URL" | grep "github.com/" | wc -l')
    let github = system('git remote show origin | grep "Push  URL" | grep "github.com/" | wc -l')
    exe 'cd ' . cwd
    " if in a git repo
    if !v:shell_error && github > 0
        packadd vim-gfm-syntax
        let b:gfm_syntax_enable = 1
        let g:gfm_syntax_enable_always = 0
        let g:markdown_fenced_languages = ['cpp', 'ruby', 'json']
    endif
endfunction

I'd basically like to replace system with a non-blocking version of it.

wookayin commented 1 year ago

+1 for adding the callback function and more flexible API. /cc @skywind3000

For now one can use autocmd events which would triggered when job finished:

let g:AsyncGitCallback = 'echom g:asyncrun_status'   " vim command, or use call()
augroup AsyncRunEvents
  autocmd! 
  autocmd User AsyncRunStop   exec g:AsyncGitCallback
augroup END

Useful variables to use here:

But this has many downsides because the callback should be defined as a global variable/function (i.e. it's not per job). Also it seems we cannot access the information/context of the ran job. As can seen in s:AsyncRun_Job_OnFinish, all the job information is private (s:... scope variables).

See also: https://github.com/skywind3000/asyncrun.vim/wiki/Display-Progress-in-Status-Line-or-Airline