|
| 1 | +let s:save_cpoptions = &cpoptions |
| 2 | +set cpoptions&vim |
| 3 | + |
| 4 | +let s:is_windows = has('win32') " This means any versions of windows https://github.com/vim-jp/vital.vim/wiki/Coding-Rule#how-to-check-if-the-runtime-os-is-windows |
| 5 | +let s:is_nvim = has('nvim') |
| 6 | + |
| 7 | +let s:TYPE_DICT = type({}) |
| 8 | +let s:TYPE_LIST = type([]) |
| 9 | +let s:TYPE_STRING = type('') |
| 10 | + |
| 11 | +function! s:_vital_loaded(V) abort |
| 12 | + let s:V = a:V |
| 13 | + let s:Process = a:V.import('Process') |
| 14 | +endfunction |
| 15 | + |
| 16 | +function! s:_vital_depends() abort |
| 17 | + return ['Process'] |
| 18 | +endfunction |
| 19 | + |
| 20 | +function! s:_ensure_buffer_string(string) abort |
| 21 | + if s:is_windows |
| 22 | + return s:Process.iconv(a:string, 'char', &encoding) |
| 23 | + else |
| 24 | + return a:string |
| 25 | + endif |
| 26 | +endfunction |
| 27 | + |
| 28 | +if !s:is_nvim |
| 29 | + " inner callbacks for Vim |
| 30 | + function! s:_inner_out_cb(user_out_cb, ch, msg) abort |
| 31 | + call a:user_out_cb(s:_ensure_buffer_string(a:msg)) |
| 32 | + endfunction |
| 33 | + |
| 34 | + function! s:_inner_exit_cb(user_exit_cb, job, exit_code) abort |
| 35 | + call a:user_exit_cb(a:exit_code) |
| 36 | + endfunction |
| 37 | + |
| 38 | + function! s:_inner_err_cb(user_err_cb, ch, msg) abort |
| 39 | + call a:user_err_cb(s:_ensure_buffer_string(result)) |
| 40 | + endfunction |
| 41 | +else |
| 42 | + " inner callbacks for Neovim |
| 43 | + function! s:_inner_out_cb(user_out_cb, job_id, data, event) abort |
| 44 | + for line in a:data |
| 45 | + if line !=# '' |
| 46 | + call a:user_out_cb(line) |
| 47 | + endif |
| 48 | + endfor |
| 49 | + endfunction |
| 50 | + |
| 51 | + function! s:_inner_exit_cb(user_exit_cb, job_id, exit_code, event) abort |
| 52 | + call a:user_exit_cb(a:exit_code) |
| 53 | + endfunction |
| 54 | + |
| 55 | + function! s:_inner_err_cb(user_err_cb, job_id, data, event) abort |
| 56 | + for line in a:data |
| 57 | + if line !=# '' |
| 58 | + call a:user_err_cb(line) |
| 59 | + endif |
| 60 | + endfor |
| 61 | + endfunction |
| 62 | +endif |
| 63 | + |
| 64 | +" execute({command}, {options}) |
| 65 | +" {command} = string |
| 66 | +" {options} = { |
| 67 | +" out_cb: function(stdout_msg), " call per line |
| 68 | +" err_cb: function(stderr_msg), " call per line |
| 69 | +" exit_cb: function(exit_code), " call on exit |
| 70 | +" timeout: Number(ms), |
| 71 | +" } |
| 72 | +function! s:execute(command, options) abort |
| 73 | + if !type(a:options) is s:TYPE_DICT |
| 74 | + throw 'vital: AsyncProcess: invalid argument (value type:' . type(a:options) . ')' |
| 75 | + endif |
| 76 | + |
| 77 | + " Process a:command argument. |
| 78 | + if type(a:command) is s:TYPE_STRING |
| 79 | + let command = a:command |
| 80 | + elseif type(a:command) is s:TYPE_LIST |
| 81 | + let command = join(a:command, ' ') |
| 82 | + else |
| 83 | + throw 'vital: AsyncProcess: invalid argument (value type:' . type(a:command) . ')' |
| 84 | + endif |
| 85 | + |
| 86 | + " build args |
| 87 | + let args = [] |
| 88 | + if stridx(&shell, 'cmd.exe') != -1 |
| 89 | + " cmd.exe |
| 90 | + let args = args + ['/c'] |
| 91 | + else |
| 92 | + " sh, bash, pwsh, etc. |
| 93 | + let args = args + ['-c'] |
| 94 | + endif |
| 95 | + let args = args + [command] |
| 96 | + |
| 97 | + let job_id = -1 |
| 98 | + if s:is_nvim |
| 99 | + let options = {} |
| 100 | + if has_key(a:options, 'out_cb') |
| 101 | + let options['on_stdout'] = function('s:_inner_out_cb', [a:options.out_cb]) |
| 102 | + endif |
| 103 | + if has_key(a:options, 'err_cb') |
| 104 | + let options['on_stderr'] = function('s:_inner_err_cb', [a:options.err_cb]) |
| 105 | + endif |
| 106 | + if has_key(a:options, 'exit_cb') |
| 107 | + let options['on_exit'] = function('s:_inner_exit_cb', [a:options.exit_cb]) |
| 108 | + endif |
| 109 | + |
| 110 | + let job_id = jobstart([&shell] + args, options) |
| 111 | + |
| 112 | + if has_key(a:options, 'timeout') |
| 113 | + if a:options.timeout > 0 |
| 114 | + call timer_start(a:options.timeout, {-> jobstop(job_id)}) |
| 115 | + endif |
| 116 | + endif |
| 117 | + |
| 118 | + return { |
| 119 | + \ 'stop': function('jobstop', [job_id]), |
| 120 | + \ } |
| 121 | + else |
| 122 | + let options = {} |
| 123 | + if has_key(a:options, 'out_cb') |
| 124 | + let options['out_cb'] = function('s:_inner_out_cb', [a:options.out_cb]) |
| 125 | + endif |
| 126 | + if has_key(a:options, 'err_cb') |
| 127 | + let options['err_cb'] = function('s:_inner_err_cb', [a:options.err_cb]) |
| 128 | + endif |
| 129 | + if has_key(a:options, 'exit_cb') |
| 130 | + let options['exit_cb'] = function('s:_inner_exit_cb', [a:options.exit_cb]) |
| 131 | + endif |
| 132 | + |
| 133 | + let job = job_start([&shell] + args, options) |
| 134 | + |
| 135 | + if has_key(a:options, 'timeout') |
| 136 | + if a:options.timeout > 0 |
| 137 | + call timer_start(a:options.timeout, {-> job_stop(job)}) |
| 138 | + endif |
| 139 | + endif |
| 140 | + |
| 141 | + return { |
| 142 | + \ 'stop': function('job_stop', [job]), |
| 143 | + \ } |
| 144 | + } |
| 145 | + |
| 146 | + endif |
| 147 | +endfunction |
| 148 | + |
| 149 | +let &cpoptions = s:save_cpoptions |
| 150 | +unlet s:save_cpoptions |
| 151 | + |
0 commit comments