mirror of
https://github.com/vim-airline/vim-airline.git
synced 2025-07-20 20:44:46 +02:00
po-extension: Make asyncable
This commit is contained in:
parent
c39e5f82cc
commit
81ccd3bdf6
@ -3,6 +3,58 @@
|
|||||||
|
|
||||||
scriptencoding utf-8
|
scriptencoding utf-8
|
||||||
|
|
||||||
|
let s:has_async = airline#util#async
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
if s:has_async
|
||||||
|
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
|
||||||
|
call s:shorten()
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! s:DoAsyncPO(cmd, file)
|
||||||
|
if g:airline#util#is_windows || !executable('msgfmt')
|
||||||
|
" 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
|
||||||
|
else
|
||||||
|
call job_stop(get(s:jobs, a:file))
|
||||||
|
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
|
||||||
|
|
||||||
function! airline#extensions#po#apply(...)
|
function! airline#extensions#po#apply(...)
|
||||||
if &ft ==# 'po'
|
if &ft ==# 'po'
|
||||||
call airline#extensions#prepend_to_section('z', '%{airline#extensions#po#stats()}')
|
call airline#extensions#prepend_to_section('z', '%{airline#extensions#po#stats()}')
|
||||||
@ -15,7 +67,11 @@ function! airline#extensions#po#stats()
|
|||||||
return b:airline_po_stats
|
return b:airline_po_stats
|
||||||
endif
|
endif
|
||||||
|
|
||||||
let airline_po_stats = system('msgfmt --statistics -o /dev/null -- '. expand('%:p'))
|
let cmd = 'msgfmt --statistics -o /dev/null -- '
|
||||||
|
if s:has_async
|
||||||
|
call s:DoAsyncPO(cmd, expand('%:p'))
|
||||||
|
else
|
||||||
|
let airline_po_stats = system(cmd. shellescape(expand('%:p')))
|
||||||
if v:shell_error
|
if v:shell_error
|
||||||
return ''
|
return ''
|
||||||
endif
|
endif
|
||||||
@ -24,13 +80,9 @@ function! airline#extensions#po#stats()
|
|||||||
catch
|
catch
|
||||||
let b:airline_po_stats = ''
|
let b:airline_po_stats = ''
|
||||||
endtry
|
endtry
|
||||||
if exists("g:airline#extensions#po#displayed_limit")
|
call s:shorten()
|
||||||
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 - 1)].(&encoding==?'utf-8' ? '…' : '.')
|
|
||||||
endif
|
endif
|
||||||
endif
|
return get(b:, 'airline_po_stats', '')
|
||||||
return b:airline_po_stats
|
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! airline#extensions#po#init(ext)
|
function! airline#extensions#po#init(ext)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user