2016-02-29 16:38:39 -05:00
|
|
|
" MIT License. Copyright (c) 2013-2016 Bailey Ling.
|
|
|
|
" vim: et ts=2 sts=2 sw=2
|
|
|
|
|
2016-09-23 20:16:30 -04:00
|
|
|
scriptencoding utf-8
|
|
|
|
|
2016-09-28 16:01:07 -04:00
|
|
|
function! s:shorten()
|
|
|
|
if exists("g:airline#extensions#po#displayed_limit")
|
|
|
|
let w:displayed_po_limit = g:airline#extensions#po#displayed_limit
|
|
|
|
if len(b:airline_po_stats) > w:displayed_po_limit - 1
|
|
|
|
let b:airline_po_stats = b:airline_po_stats[0:(w:displayed_po_limit - 2)].(&encoding==?'utf-8' ? '…' : '.'). ']'
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2017-08-21 15:29:16 -04:00
|
|
|
if g:airline#init#async
|
2016-09-28 16:01:07 -04:00
|
|
|
let s:jobs = {}
|
|
|
|
|
|
|
|
function! s:on_stdout(channel, msg) dict abort
|
|
|
|
let self.buf = a:msg
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:on_exit(channel) dict abort
|
|
|
|
if !empty(self.buf)
|
|
|
|
let b:airline_po_stats = printf("[%s]", self.buf)
|
|
|
|
else
|
|
|
|
let b:airline_po_stats = ''
|
|
|
|
endif
|
2016-12-29 05:17:18 -05:00
|
|
|
if has_key(s:jobs, self.file)
|
|
|
|
call remove(s:jobs, self.file)
|
|
|
|
endif
|
2016-09-28 16:01:07 -04:00
|
|
|
call s:shorten()
|
|
|
|
endfunction
|
|
|
|
|
2016-10-09 09:47:23 -04:00
|
|
|
function! s:get_msgfmt_stat_async(cmd, file)
|
2017-08-21 15:29:16 -04:00
|
|
|
if g:airline#init#is_windows || !executable('msgfmt')
|
2016-09-28 16:01:07 -04:00
|
|
|
" no msgfmt on windows?
|
|
|
|
return
|
|
|
|
else
|
|
|
|
let cmd = ['sh', '-c', a:cmd. shellescape(a:file)]
|
|
|
|
endif
|
|
|
|
|
|
|
|
let options = {'buf': '', 'file': a:file}
|
|
|
|
if has_key(s:jobs, a:file)
|
|
|
|
if job_status(get(s:jobs, a:file)) == 'run'
|
|
|
|
return
|
2016-10-09 09:47:23 -04:00
|
|
|
elseif has_key(s:jobs, a:file)
|
2016-09-28 16:01:07 -04:00
|
|
|
call remove(s:jobs, a:file)
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
let id = job_start(cmd, {
|
|
|
|
\ 'err_io': 'out',
|
|
|
|
\ 'out_cb': function('s:on_stdout', options),
|
|
|
|
\ 'close_cb': function('s:on_exit', options)})
|
|
|
|
let s:jobs[a:file] = id
|
|
|
|
endfu
|
|
|
|
endif
|
|
|
|
|
2016-02-29 16:38:39 -05:00
|
|
|
function! airline#extensions#po#apply(...)
|
|
|
|
if &ft ==# 'po'
|
|
|
|
call airline#extensions#prepend_to_section('z', '%{airline#extensions#po#stats()}')
|
|
|
|
autocmd airline BufWritePost * unlet! b:airline_po_stats
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! airline#extensions#po#stats()
|
|
|
|
if exists('b:airline_po_stats') && !empty(b:airline_po_stats)
|
|
|
|
return b:airline_po_stats
|
|
|
|
endif
|
|
|
|
|
2016-09-28 16:01:07 -04:00
|
|
|
let cmd = 'msgfmt --statistics -o /dev/null -- '
|
2017-08-21 15:29:16 -04:00
|
|
|
if g:airline#init#async
|
2016-10-09 09:47:23 -04:00
|
|
|
call s:get_msgfmt_stat_async(cmd, expand('%:p'))
|
2016-09-28 16:01:07 -04:00
|
|
|
else
|
|
|
|
let airline_po_stats = system(cmd. shellescape(expand('%:p')))
|
|
|
|
if v:shell_error
|
|
|
|
return ''
|
2016-02-29 16:38:39 -05:00
|
|
|
endif
|
2016-09-28 16:01:07 -04:00
|
|
|
try
|
|
|
|
let b:airline_po_stats = '['. split(airline_po_stats, '\n')[0]. ']'
|
2016-11-22 06:24:09 -05:00
|
|
|
let b:airline_po_stats = substitute(b:airline_po_stats, ' \(message\|translation\)s*\.*', '', 'g')
|
2016-09-28 16:01:07 -04:00
|
|
|
catch
|
|
|
|
let b:airline_po_stats = ''
|
|
|
|
endtry
|
|
|
|
call s:shorten()
|
2016-02-29 16:38:39 -05:00
|
|
|
endif
|
2016-09-28 16:01:07 -04:00
|
|
|
return get(b:, 'airline_po_stats', '')
|
2016-02-29 16:38:39 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! airline#extensions#po#init(ext)
|
|
|
|
call a:ext.add_statusline_func('airline#extensions#po#apply')
|
|
|
|
endfunction
|