Skip to content

Instantly share code, notes, and snippets.

@844196
Created March 3, 2023 00:18
Show Gist options
  • Save 844196/28b25f0ca701ae9f3118e5f6fbc22618 to your computer and use it in GitHub Desktop.
Save 844196/28b25f0ca701ae9f3118e5f6fbc22618 to your computer and use it in GitHub Desktop.
#!/bin/zsh
readonly local format=(
'%(if)%(HEAD)%(then)1%(else)0%(end)' # is current (0|1)
'0' # is topic branch (0|1)
'0' # is gone (0|1)
'%(refname:short)' # branch name
'%(color:bold black)%(subject)%(color:reset)' # commit message
'%(color:bold black)%(upstream:track)%(color:reset)' # upstream status (e.g. [ahead 1], [behind 40], [gone])
)
git branch --color=always -v --format ${(pj:\t:)format} \
| awk -F $'\t' '
BEGIN {
OFS="\t"
}
# "[ahead 1, behind 40]" "feat: Implement xxx"
# 👇
# "feat: Implement xxx [ahead 1, behind 40]"
{
$5=$5 " " $6
}
# if current branch
$1 == 1 {
# colorize green
$4="\x1b[32m" $4 "\x1b[m"
}
# if branch name contains "/" (i.e. is topic branch?)
$4 ~ /\// {
$2=1
}
# if upstream status contains "gone"
$6 ~ /gone/ {
$3=1
# colorize magenta and draw strikethrough
$4="\x1b[9;35m" $4 "\x1b[m"
}
{
print $0
}
' \
| sort -t $'\t' -k1r,1 -k3,3 -k2,2 -k4,4 \
| cut -d$'\t' -f4,5 \
| column -ts $'\t' -o ' ' \
| emojify
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment