Add width controls to the new stl_format flags.

This commit is contained in:
LCD 47 2015-09-22 12:27:55 +03:00
parent d73d7601cc
commit e0a3cf753e
4 changed files with 78 additions and 12 deletions

View File

@ -107,16 +107,16 @@ function! syntastic#util#rmrf(what) abort " {{{2
endif
endfunction " }}}2
"search the first 5 lines of the file for a magic number and return a map
"containing the args and the executable
" Search the first 5 lines of the file for a magic number and return a map
" containing the args and the executable
"
"e.g.
" e.g.
"
"#!/usr/bin/perl -f -bar
" #!/usr/bin/perl -f -bar
"
"returns
" returns
"
"{'exe': '/usr/bin/perl', 'args': ['-f', '-bar']}
" {'exe': '/usr/bin/perl', 'args': ['-f', '-bar']}
function! syntastic#util#parseShebang() abort " {{{2
for lnum in range(1, 5)
let line = getline(lnum)
@ -183,7 +183,7 @@ function! syntastic#util#screenWidth(str, tabstop) abort " {{{2
return width
endfunction " }}}2
"print as much of a:msg as possible without "Press Enter" prompt appearing
" Print as much of a:msg as possible without "Press Enter" prompt appearing
function! syntastic#util#wideMsg(msg) abort " {{{2
let old_ruler = &ruler
let old_showcmd = &showcmd
@ -226,7 +226,7 @@ function! syntastic#util#bufIsActive(buffer) abort " {{{2
return 0
endfunction " }}}2
" start in directory a:where and walk up the parent folders until it finds a
" Start in directory a:where and walk up the parent folders until it finds a
" file named a:what; return path to that file
function! syntastic#util#findFileInParent(what, where) abort " {{{2
let old_suffixesadd = &suffixesadd
@ -236,7 +236,7 @@ function! syntastic#util#findFileInParent(what, where) abort " {{{2
return file
endfunction " }}}2
" start in directory a:where and walk up the parent folders until it finds a
" Start in directory a:where and walk up the parent folders until it finds a
" file matching a:what; return path to that file
function! syntastic#util#findGlobInParent(what, where) abort " {{{2
let here = fnamemodify(a:where, ':p')
@ -303,7 +303,7 @@ function! syntastic#util#argsescape(opt) abort " {{{2
return []
endfunction " }}}2
" decode XML entities
" Decode XML entities
function! syntastic#util#decodeXMLEntities(string) abort " {{{2
let str = a:string
let str = substitute(str, '\m&lt;', '<', 'g')
@ -350,6 +350,64 @@ function! syntastic#util#float2str(val) abort " {{{2
return s:_float2str(a:val)
endfunction " }}}2
" Crude printf()-like width formatter. Handles wide characters.
function! syntastic#util#wformat(format, str) abort " {{{2
if a:format ==# ''
return a:str
endif
echomsg string(a:format) . ', ' . string(a:str)
let specs = matchlist(a:format, '\v^(-?)(0?)(%([1-9]\d*))?%(\.(\d+))?$')
if len(specs) < 5
return a:str
endif
let flushleft = specs[1] ==# '-'
let lpad = specs[2] ==# '0' ? '0' : ' '
let minlen = str2nr(specs[3])
let maxlen = str2nr(specs[4])
let out = substitute(a:str, "\t", ' ', 'g')
if maxlen && s:_width(out) > maxlen
let chars = filter(split(a:str, '\zs\ze', 1), 'v:val !=# ""')
let totlen = 0
let out = ''
if flushleft
for c in chars
if totlen + s:_width(c) < maxlen
let out .= c
let totlen += s:_width(c)
else
let out .= &encoding ==# 'utf-8' && &termencoding ==# 'utf-8' ? "\u2026" : '>'
break
endif
endfor
else
call reverse(chars)
for c in chars
if totlen + s:_width(c) < maxlen
let out = c . out
let totlen += s:_width(c)
else
let out = (&encoding ==# 'utf-8' && &termencoding ==# 'utf-8' ? "\u2026" : '<') . out
break
endif
endfor
endif
endif
if minlen && s:_width(out) < minlen
if flushleft
let out .= repeat(' ', minlen - s:_width(out))
else
let out = repeat(lpad, minlen - s:_width(out)) . out
endif
endif
return out
endfunction " }}}2
" }}}1
" Private functions {{{1

View File

@ -585,6 +585,13 @@ magic flags are available to insert information:
%fw - line number of first warning
%F - line number of first warning or error
These flags accept width and alignment controls similar to the ones used by
|'statusline'| flags:
%-0{minwid}.{maxwid}{flag}
All fields except {flag} are optional. A single percent sign can be given as
"%%".
Several additional flags are available to hide text under certain conditions:
%E{...} - hide the text in the brackets unless there are errors
%W{...} - hide the text in the brackets unless there are warnings

View File

@ -19,7 +19,7 @@ if has('reltime')
lockvar! g:_SYNTASTIC_START
endif
let g:_SYNTASTIC_VERSION = '3.6.0-170'
let g:_SYNTASTIC_VERSION = '3.6.0-171'
lockvar g:_SYNTASTIC_VERSION
" Sanity checks {{{1

View File

@ -111,6 +111,7 @@ function! g:SyntasticLoclist.getStatuslineFlag() abort " {{{2
let output = substitute(output, '\m\C%B{\([^}]*\)}', (num_warnings && num_errors) ? '\1' : '' , 'g')
let flags = {
\ '%': '%',
\ 't': num_issues,
\ 'e': num_errors,
\ 'w': num_warnings,
@ -123,7 +124,7 @@ function! g:SyntasticLoclist.getStatuslineFlag() abort " {{{2
\ 'nw': (num_warnings ? fnamemodify( bufname(warnings[0]['bufnr']), ':t') : ''),
\ 'pw': (num_warnings ? fnamemodify( bufname(warnings[0]['bufnr']), ':p:~:.') : ''),
\ 'fw': (num_warnings ? warnings[0]['lnum'] : '') }
let output = substitute(output, '\v\C\%([npf][ew]|[NPFtew])', '\=flags[submatch(1)]', 'g')
let output = substitute(output, '\v\C\%(-?\d*%(\.\d+)?)([npf][ew]|[NPFtew%])', '\=syntastic#util#wformat(submatch(1), flags[submatch(2)])', 'g')
let self._stl_flag = output
else