How to display git "tag" in lightline?

132 views Asked by At

I work on git tags on a daily basis & switch between 'em. I have my lightline configured where it does display git branch in status bar of lightline. But I am trying to display git tag as well. gitbranch component from lightline does display the commit id when I checkout / switch to a tag but those commit ids are pretty much unusable.

Here is my lightline block in neovim config:

let g:lightline = {
        \ 'colorscheme': 'powerline',
        \ 'active': {
        \   'left': [['statuslinetabs', 'lineinfo'], ['gitbranch',  'gittag', 'readonly', 'modified']],
        \   'right': [['mode'], ['paste'], ['absolutepath']],
        \ },
        \ 'inactive': {
        \   'left': [[], ['line', 'relativepath', 'readonly', 'modified']],
        \   'right': [[], [], ['gitbranch']],
        \ },
        \ 'component_expand': {
        \   'statuslinetabs': 'LightlineStatuslineTabs',
        \ },
        \ 'component_function': {
        \   'gitbranch': 'MyFugitiveHead'
        \ },
        \  'component': {
        \    'clock': '%{strftime("%a %d %b %I:%M%p")}'
        \  },
        \ }

In help of fugitive's AUTOCOMMANDS section it does say -

    AUTOCOMMANDS                                    fugitive-autocommands

A handful of User autocommands are provided to allow extending and
overriding Fugitive behaviors.  Example usage:

        autocmd User FugitiveBlob,FugitiveStageBlob call s:BlobOverrides()

                                                User_FugitiveTag
FugitiveTag             After loading a tag object.

But I am unable to figure how to utilize that as neither component or component_fuction.

Please advice!

Thanks.

2

There are 2 answers

0
user1365433 On BEST ANSWER

Found a solution, not the vim-fugitive but native git way - via system calls. Just declared below as a component (not function) and since I work with multiple tags sometimes, used xargs to show all tags pointing to head. Includes non-git files scenario command output sending to /dev/null.

        \  'component': {
        \    'gittag': '%{substitute(system("git tag --points-at HEAD 2>/dev/null | xargs"), "\n", "|", "g")}',
        \  },
4
rcasia On

You have set gittag in the active list:

\ 'active': {
        \   'left': [['statuslinetabs', 'lineinfo'], ['gitbranch',  'gittag', 'readonly', 'modified']],
        \   'right': [['mode'], ['paste'], ['absolutepath']],
        \ },

Then you should add:

...
'component_function': {
        \   'gitbranch': 'MyFugitiveHead'
         \   'gittag': 'FugitiveTag'
        \ },
...