Skip to content

Commit

Permalink
Add sy#repo#get_stats_decorated()
Browse files Browse the repository at this point in the history
It's a common request, so here is the beefed up version of sy#repo#get_stats().
  • Loading branch information
mhinz committed Dec 4, 2019
1 parent 25f3d4b commit ec57a86
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 11 deletions.
21 changes: 21 additions & 0 deletions autoload/sy/repo.vim
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,27 @@ function! sy#repo#get_stats(...) abort
return empty(sy) ? [-1, -1, -1] : sy.stats
endfunction

" #get_stats_decorated {{{1
function! sy#repo#get_stats_decorated(...)
let bufnr = a:0 ? a:1 : bufnr('')
let [added, modified, removed] = sy#repo#get_stats(bufnr)
let symbols = ['+', '-', '~']
let stats = [added, removed, modified] " reorder
let statline = ''

for i in range(3)
if stats[i] > 0
let statline .= printf('%s%s ', symbols[i], stats[i])
endif
endfor

if !empty(statline)
let statline = printf('[%s]', statline[:-2])
endif

return statline
endfunction

" #debug_detection {{{1
function! sy#repo#debug_detection()
if !exists('b:sy')
Expand Down
45 changes: 34 additions & 11 deletions doc/signify.txt
Original file line number Diff line number Diff line change
Expand Up @@ -563,37 +563,60 @@ color for |hl-Normal| but for |hl-SignColumn|. To avoid that visible difference:
==============================================================================
FAQ *signify-faq*

|signify-faq-01| What about vim-flagship support?
|signify-faq-01| How to display changes in the statusline?
|signify-faq-02| The plugin is slow!
|signify-faq-03| Line highlighting without showing signs?

------------------------------------------------------------------------------
*signify-faq-01*
What about vim-flagship support?~
How to display changes in the statusline?~

sy#repo#get_stats() returns a list with 3 integers for added, modified and
removed lines. Create a wrapper function around it and return a string:
Use either of the following two functions. Both take an optional buffer number
and default to the current one.

- sy#repo#get_stats(...)~

Returns a list with the number of added, modified, and removed lines.

- sy#repo#get_stats_decorated(...)~

Similar to sy#repo#get_stats(), but with added decorations.
Example: "[+3 -8 ~5]"

Using 'statusline':
>
function! MyStatusline()
return ' %f '. sy#repo#get_stats_decorated()
endfunction

set statusline=%!MyStatusline()
<
The above is the short form of:
>
function! s:sy_stats_wrapper()
let symbols = ['+', '-', '~']
let [added, modified, removed] = sy#repo#get_stats()
let symbols = ['+', '-', '~']
let stats = [added, removed, modified] " reorder
let hunkline = ''
let statline = ''

for i in range(3)
if stats[i] > 0
let hunkline .= printf('%s%s ', symbols[i], stats[i])
let statline .= printf('%s%s ', symbols[i], stats[i])
endif
endfor

if !empty(hunkline)
let hunkline = printf('[%s]', hunkline[:-2])
if !empty(statline)
let statline = printf('[%s]', statline[:-2])
endif

return hunkline
return statline
endfunction

function! MyStatusline()
return ' %f '. s:sy_stats_wrapper()
endfunction

autocmd User Flags call Hoist('buffer', function('s:sy_stats_wrapper'))
set statusline=%!MyStatusline()
<
------------------------------------------------------------------------------
*signify-faq-02*
Expand Down

0 comments on commit ec57a86

Please sign in to comment.