如何在lightline中显示git “tag”?

huangapple go评论58阅读模式
英文:

How to display git "tag" in lightline?

问题

以下是您要翻译的内容:

我每天都在使用Git标签并在它们之间切换。我已经配置了我的Lightline以在状态栏中显示Git分支,但我正在尝试显示Git标签。Lightline中的gitbranch组件在我切换到标签时显示提交ID,但这些提交ID几乎无法使用。

这是我的Neovim配置中的Lightline块:

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")}',
        \  },
        \ }

在Fugitive的AUTOCOMMANDS部分的帮助文档中,它提到:

AUTOCOMMANDS                                    fugitive-autocommands

提供了一些用户自动命令,以允许扩展和覆盖Fugitive的行为。示例用法:

autocmd User FugitiveBlob,FugitiveStageBlob call s:BlobOverrides()

User_FugitiveTag
FugitiveTag 在加载标签对象之后。

但我无法弄清楚如何利用它,既不是作为组件也不是作为组件功能。

请给予建议!

谢谢。

英文:

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.

答案1

得分: 0

你已经在 active 列表中设置了 gittag

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

然后你应该添加:

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

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'
        \ },
...

答案2

得分: 0

I found a solution, not using vim-fugitive, but the native git method - by using system calls. Here is the code declared as a component (not a function). Since I sometimes work with multiple tags, I used xargs to display all tags pointing to the head. This also includes handling non-git files scenario with the command output sent to /dev/null.

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

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")}',
        \  },

huangapple
  • 本文由 发表于 2023年6月1日 08:43:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76378037.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定