英文:
Git branch --format with %(if) and %(align)
问题
我想改善使用--format参数显示git branch的方式。
首先,我想到了一个解决方案,复制默认格式:
git branch --format "%(if)%(HEAD)%(then)%(HEAD)%(color:green)%(else) %(end) %(refname:short)"
然后,我想在此基础上添加其他信息。我试图使用%(align:...)将它们右对齐:
git branch --format "%(align:50,left)%(if)%(HEAD)%(then)%(HEAD)%(color:green)%(else) %(end)%(refname:short)%(end)AAA"
但结果如下:
git branch --format "%(align:50,left)%(if)%(HEAD)%(then)%(HEAD)%(color:green)%(else) %(end)%(refname:short)%(end)AAA"
  testbranch1                                      AAA
  testbranch2                                      AAA
* testbranch3                                  AAA
任何使用%(if)的地方都会影响对齐。我猜测align函数计算%(if)%(HEAD)内打印的字符数,即使它们没有显示出来。
有解决办法吗?
英文:
I wanted to improve the display of my git branch using --format
First I came up with a solution that copies the default format
git branch --format "%(if)%(HEAD)%(then)%(HEAD)%(color:green)%(else) %(end) %(refname:short)"
Then I wanted to build up on that, adding other infos to the right. I figured I could align them with %(align:...)
git branch --format "%(align:50,left)%(if)%(HEAD)%(then)%(HEAD)%(color:green)%(else) %(end)%(refname:short)%(end)AAA"
But the result is the following:
git branch --format "%(align:50,left)%(if)%(HEAD)%(then)%(HEAD)%(color:green)%(else) %(end)%(refname:short)%(end)AAA"
  testbranch1                                      AAA
  testbranch2                                      AAA
* testbranch3                                  AAA
Any use of %(if) will mess with the align. I guess the align function counts the characters printed inside the %(if)%(HEAD), even if they are not displayed.
Any solution to that?
答案1
得分: 2
您需要将%(align:50,left) ... %(end) 和 %(if) ... %(then) ... %(else) ... %(end) 结构区分开来,它们不能很好地混合使用:
$ git branch --format "%(if)%(HEAD)%(then)%(HEAD)%(color:green)%(else) %(end)%(align:50,left)%(refname:short)%(end)AAA"
英文:
You'd have to get the %(align:50,left) ... %(end) and the %(if) ... %(then) ... %(else) ... %(end) constructs distinct, they don't mix well:
$ git branch --format "%(if)%(HEAD)%(then)%(HEAD)%(color:green)%(else) %(end)%(align:50,left)%(refname:short)%(end)AAA"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论