Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. " Append lines to a buffer. Use the Neovim API instead of the traditional
  2. "
  3. " call append(line('$'), a:data)
  4. "
  5. " Reason: nvim_buf_set_lines allows adding lines to a buffer which is not
  6. " the active one. This means we don't have to fiddle with the users' active
  7. " buffers.
  8. function s:appendlines(buf, list)
  9. if !empty(a:list)
  10. call nvim_buf_set_lines(a:buf, -1, -1, v:false, a:list)
  11. endif
  12. endfunction
  13.  
  14. function cb.on_stdout(job_id, data, event) dict
  15. " If we have more than a single line, we can be sure there's some newline in
  16. " there, so flush stdoutbuf.
  17. if len(a:data) > 1
  18. " Technically, this could make us add an empty line to the buffer: if
  19. " self.stdoutbuf and a:data[0] are empty. I think this is intentional...
  20. "
  21. " But what happens if self.stdoutbuf was already '' (from a newline), and
  22. " we get a newline now in a:data? Wouldn't we be collapsing that newline?
  23. call s:appendlines(self.buf, [self.stdoutbuf . remove(a:data, 0)])
  24. let self.stdoutbuf = ''
  25. endif
  26.  
  27. " Print all lines save for the last, which may be incomplete.
  28. call s:appendlines(self.buf, a:data[:-2])
  29.  
  30. if !empty(a:data[-1])
  31. let self.stdoutbuf = a:data[-1]
  32. endif
  33.  
  34. " I guess we just ignore empty last list items... (TODO)
  35. endfunction
  36.  
  37. function cb.on_exit(job_id, data, event) dict
  38. if !empty(self.stdoutbuf)
  39. call s:appendlines(self.buf, [self.stdoutbuf])
  40. endif
  41. unlet self.stdoutbuf
  42. endfunction
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement