How to align columns in git to improve formatting?

Viewed 329

I wanted to improve the formatting of my "git branch" command, where I added logic to include the commit date and commit user.

git for-each-ref refs/heads/ --format='%(HEAD) %(color:bold yellow)%(refname:short)%(color:reset) %(color:green)%(committerdate)%(color:reset) - %(contents:subject) %(color:dim white) - %(authorname)%(color:reset)'

enter image description here

Is there anyway of improving the formatting from the first (actual) output, into the second (desired) output?

This is not an urgent thing at all, but a nice to have. I've played with the git format, but my knowledge is limited.

1 Answers

I'd suggest using the %(align) format option, like this :

%(align:width=<number of chars>) <column to align> %(end)

which in your example would give

git for-each-ref refs/heads/ --format='%(HEAD) %(align:width=15)%(color:bold yellow)%(refname:short)%(end)%(color:reset) %(color:green)%(committerdate)%(color:reset) - %(contents:subject) %(color:dim white) - %(authorname)%(color:reset)'

It does the job, despite offering no dynamic value for the alignment, which would be nice but maybe not necessary, depending on your context.

Related