2

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 那样工作?

4

1 回答 1

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'])
于 2014-12-03T03:50:10.037 回答