Neovim在:help job-control
中的作业控制示例适用于bash脚本。但是,我无法让它适用于红宝石。请考虑以下示例:
set nocp
set buftype=nowrite
call jobstart('shell', 'bash', ['-c', 'for ((i = 0; i < 5; i++)); do sleep 2 && printf "Hello Bash!\n"; done'])
call jobstart('shell', 'ruby', ['-e', '5.times do sleep 2 and puts "Hello Ruby!" end'])
function JobHandler()
if v:job_data[1] == 'exit'
let str = v:job_data[0] . ' exited'
else
let str = join(v:job_data[2])
endif
call append(line('$'), str)
endfunction
au JobActivity shell* call JobHandler()
运行nvim -u NONE -S <filename>
会产生以下输出:
Hello Bash!
Hello Bash!
Hello Bash!
Hello Bash!
Hello Bash!
1 exited
Hello Ruby! Hello Ruby! Hello Ruby! Hello Ruby! Hello Ruby!
2 exited
我们如何让ruby示例像bash那样工作?
答案 0 :(得分:2)
事实证明,ruby的输出正在被缓冲。必须强制刷新才能看到所需的输出。
call jobstart('shell', 'ruby', ['-e', '$stdout.sync = true; 5.times do sleep 1 and puts "Hello Ruby!\n" end'])
我最初的问题是异步运行ruby测试。要使其正常工作,我必须将$stdout.sync = true
写入文件,并使用-r
来求它:
call jobstart('shell', 'ruby', ['-r', '/tmp/opts.rb', '-I', 'test', 'test/unit/user_test.rb'])