Vim script で Twitter stream を簡単にしてみた


mattn さんのコードを元に、今考えているロジックだとどんな感じになるのかちょっと書き換えてみた。

[ソース]

" base code : https://github.com/mattn/streamer-vim/blob/master/autoload/streamer.vim

function! s:stream()
  Coroutine

  Let username = input('Username: ')
  if len(username) == 0 | return | endif
  Let password = inputsecret('Password: ')
  if len(password) == 0 | return | endif

  Let cmd  = printf("curl -s -u %s:%s %s",
  \  username,
  \  password,
  \  "https://stream.twitter.com/1.1/statuses/sample.json")
  Let proc = vimproc#plineopen2(cmd)

  Let id = g:post(chained#this_func())

  while 1
    try
      Let content = proc.stdout.read_line()
      if content != ''
        Let status = webapi#json#decode(content)
        Let &titlestring = status.user.screen_name . ' ' .  status.text
        redraw
      endif
    catch
    endtry
    Yield
  endwhile
endfunction
call s:stream()


基本的には以前書いたコルーチンのコードで処理しています。
g:post 関数は、引数に渡した関数を autocmd CursorHold,CursorHoldI から呼ばれるようにする為の関数です。
これで毎回自分が呼ばれるような感じにしています。
複数の関数に別れてなかったり s: を使っていない分、コードは小さくまとまっていると思うのですが、さて。
読みやすいかなぁ…。