aboutsummaryrefslogblamecommitdiff
path: root/vim/autoload/fortune.vim
blob: 339b1aef956812a3bc3edbe803e41016bd3a4096 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13



                                             








                             
                                             







                                           

                          






                                                   
                          
 



                    

               

                
 





                             
 

                                
                                   


              
 


                
 
           
let s:paths = [
      \ $HOME.'/.fortunes',
      \ $HOME.'/.local/share/games/fortunes',
      \]

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

function! s:Highlight() abort
  highlight Fortune
        \ term=NONE
        \ cterm=NONE ctermfg=244 ctermbg=NONE
        \ gui=NONE guifg=#585858 guibg=NONE
endfunction
augroup fortune
  autocmd!
  autocmd ColorScheme *
        \ call s:Highlight()
augroup END
doautocmd fortune ColorScheme

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',
        \)

  echohl Fortune
  echo fortune
  echohl None

endfunction