aboutsummaryrefslogtreecommitdiff
path: root/vim/plugin/fortune.vim
blob: e52f50af8b24b2bd7a3b9376d1ca8847d0ce5a4f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
let s:paths = [
      \ $HOME.'/.fortunes',
      \ $HOME.'/.local/share/games/fortunes',
      \]
highlight Fortune
      \ term=NONE
      \ cterm=NONE ctermfg=248 ctermbg=NONE

function! s:Fortune() abort
  if !has('unix')
    echoerr 'Only works on *nix'
  endif
  if !executable('fortune')
    echoerr 'Missing "fortune" executable'
  endif
  if !executable('timeout')
    echoerr 'Missing "timeout" executable'
  endif
  let limit = &columns - 1
  let command = [
        \ 'timeout',
        \ '0.3s',
        \ 'fortune',
        \ '-s', 
        \ '-n', 
        \ limit,
        \]
  for path in s:paths
    if isdirectory(path)
      call add(command, path)
      break
    endif
  endfor
  let fortune = substitute(
        \ system(join(command)),
        \ '[[:cntrl:]]\+',
        \ ' ',
        \ 'g',
        \)
  echohl Fortune
  echo fortune
  echohl None
endfunction
command! -bar Fortune
      \ call s:Fortune()
augroup fortune
  autocmd!
  autocmd VimEnter *
        \ if !argc() && line2byte('$') == -1 | Fortune | endif
augroup END