From ed8a9ecda518c37580ddb561aaa427e4ea16fe51 Mon Sep 17 00:00:00 2001 From: Paul-Christian Volkmer Date: Sun, 9 Jan 2022 06:43:55 +0100 Subject: [PATCH] Initial import --- LICENSE | 19 +++++++++++++++++ README.md | 35 ++++++++++++++++++++++++++++++++ autoload/fugistate.vim | 46 ++++++++++++++++++++++++++++++++++++++++++ plugin/fugistate.vim | 22 ++++++++++++++++++++ 4 files changed, 122 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100644 autoload/fugistate.vim create mode 100644 plugin/fugistate.vim diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..a4a1e0b --- /dev/null +++ b/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) 2022 Paul-Christian Volkmer + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..a847867 --- /dev/null +++ b/README.md @@ -0,0 +1,35 @@ +# vim-fugistate + +## Usage +This simple plugin provides a function which returns the name of current file with git status if available using +[vim-fugitive](https://github.com/tpope/vim-fugitive) and is intended for statusline integration. + +```vim +:echo FugiState() +``` + +It will show something like `main.c [ M]` for modified file `main.c`. + +For example, to show the filename with git status using [lightline.vim](https://github.com/itchyny/lightline.vim), +configure as follows. + +```vim +let g:lightline = { + \ 'active': { + \ 'left': [ [ 'mode', 'paste' ], + \ [ 'readonly', 'filenamestatus', 'modified' ] ] + \ }, + \ 'component_function': { + \ 'filenamestatus': 'FugiState' + \ }, + \ } +``` + +### Filename configuration + +The plugin provides `g:fugistate_expand_filename` to modify the value the filename should be expanded to. +Default value is set to `'%:t'` and will show filename without path. Use available values like `'%:p'` for full path +or `''` to use `@%` for directory/filename relative to current working directory. + +## License +This software is released under the MIT License, see LICENSE. diff --git a/autoload/fugistate.vim b/autoload/fugistate.vim new file mode 100644 index 0000000..07a3b49 --- /dev/null +++ b/autoload/fugistate.vim @@ -0,0 +1,46 @@ +" Filename: plugin/fugistate.vim +" Author: Paul-Christian Volkmer +" License: MIT License + +let s:filename_status = '' + +function! fugistate#update() + + if empty(@%) + let s:filename_status = '' + return + endif + + if empty(g:fugistate_expand_filename) + let filename = expand(@%) + else + let filename = expand(g:fugistate_expand_filename) + endif + + try + + if ! FugitiveIsGitDir() + let s:filename_status = filename + return + 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() + + return s:filename_status + +endfunction diff --git a/plugin/fugistate.vim b/plugin/fugistate.vim new file mode 100644 index 0000000..4f5f0d3 --- /dev/null +++ b/plugin/fugistate.vim @@ -0,0 +1,22 @@ +" Filename: plugin/fugistate.vim +" Author: Paul-Christian Volkmer +" License: MIT License + +if exists('g:loaded_fugistate') || v:version < 800 + finish +endif +let g:loaded_fugistate = 1 + +if ! exists('g:fugistate_expand_filename') + let g:fugistate_expand_filename = '%:t' +endif + +augroup FugiState + autocmd! + autocmd BufEnter,BufWritePost,FocusGained * :call fugistate#update() + autocmd User FugitiveChanged :call fugistate#update() +augroup END + +function! FugiState() + return fugistate#filename_status() +endfunction