aboutsummaryrefslogtreecommitdiff
path: root/vim/autoload/fortune.vim
blob: 6bbe6b3bc89eb34c4a81188c7c2b383dd70d84af (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
let s:paths = [
      \ $HOME.'/.fortunes',
      \ $HOME.'/.local/share/games/fortunes',
      \]

let s:executables = [
      \ 'fortune',
      \ 'timeout',
      \]

function! fortune#() abort

  for executable in s:executables
    if !executable(executable)
      echoerr 'Missing executable "'.executable.'"'
    endif
  endfor

  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:][:space:]]\+',
        \ ' ',
        \ 'g',
        \)

  echomsg fortune

endfunction