diff --git a/README.md b/README.md index a847867..ea0e9b1 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,12 @@ This simple plugin provides a function which returns the name of current file wi It will show something like `main.c [ M]` for modified file `main.c`. +```vim +:echo FugiStateGitDir() +``` + +This will show changes, new and unversioned files, e.g. `1 change, 2 new, 3 unversioned`. + For example, to show the filename with git status using [lightline.vim](https://github.com/itchyny/lightline.vim), configure as follows. diff --git a/autoload/fugistate.vim b/autoload/fugistate.vim index 07a3b49..5ea058f 100644 --- a/autoload/fugistate.vim +++ b/autoload/fugistate.vim @@ -4,12 +4,11 @@ let s:filename_status = '' -function! fugistate#update() +let s:changed = 0 +let s:new = 0 +let s:unversioned = 0 - if empty(@%) - let s:filename_status = '' - return - endif +function! fugistate#update() if empty(g:fugistate_expand_filename) let filename = expand(@%) @@ -17,30 +16,59 @@ function! fugistate#update() let filename = expand(g:fugistate_expand_filename) endif + let s:changed = 0 + let s:new = 0 + let s:unversioned = 0 + + let s:filename_status = filename + try + let gitstatus = FugitiveExecute('status', '-s') + if gitstatus.exit_status == 0 + for filestate in gitstatus.stdout + if match(filestate, '^?') == 0 + let s:unversioned = s:unversioned + 1 + elseif match(filestate, '^A') == 0 + let s:new = s:new + 1 + elseif ! empty(filestate) + let s:changed = s:changed + 1 + endif - if ! FugitiveIsGitDir() - let s:filename_status = filename - return + if ! empty(@%) && @% == filestate[3:] + let s:filename_status = filename . " [" . filestate[0:1] . "]" + endif + endfor endif - - let gitstatus = FugitiveExecute('status', '-s', @%) - if gitstatus.exit_status != 0 || empty(gitstatus.stdout[0]) - let s:filename_status = filename - return - endif - let s:filename_status = filename . " [" . gitstatus.stdout[0][0:1] . "]" - - catch - - let s:filename_status = filename - endtry endfunction -function! fugistate#filename_status() +function! fugistate#file() return s:filename_status endfunction + +function! fugistate#gitdir() + + let s:out = [] + + if s:changed == 1 + call add(s:out, "1 change") + endif + + if s:changed > 1 + call add(s:out, s:changed . " changes") + endif + + if s:new > 0 + call add(s:out, s:new . " new") + endif + + if s:unversioned > 0 + call add(s:out, s:unversioned . " unversioned") + endif + + return join(s:out, ', ') + +endfunction diff --git a/plugin/fugistate.vim b/plugin/fugistate.vim index 4f5f0d3..eca1447 100644 --- a/plugin/fugistate.vim +++ b/plugin/fugistate.vim @@ -18,5 +18,9 @@ augroup FugiState augroup END function! FugiState() - return fugistate#filename_status() + return fugistate#file() +endfunction + +function! FugiStateGitDir() + return fugistate#gitdir() endfunction