Skip to content

Instantly share code, notes, and snippets.

@aal89
Last active October 1, 2020 18:41
Show Gist options
  • Save aal89/8640de0cd2c2667a97d82bb9e2e6bea2 to your computer and use it in GitHub Desktop.
Save aal89/8640de0cd2c2667a97d82bb9e2e6bea2 to your computer and use it in GitHub Desktop.
Terminal coloring with git branch if one is found
print_git_data() {
if [ $(parse_git_branch) ]; then
printf ' ('
printf $(parse_git_branch)
printf ' '
printf $(parse_latest_commit_hash)
printf ')'
fi
}
parse_git_branch() {
(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ \1/')
}
parse_latest_commit_hash() {
(git log | grep commit | head -n 1 | awk '{print $2}' | cut -c1-6)2>/dev/null
}
export PATH="$HOME/.cargo/bin:$PATH"
export PS1="\l:\[$(tput sgr0)\]\[$(tput bold)\]\[\033[38;5;10m\]\w\[$(tput sgr0)\]\[$(tput sgr0)\]\[\033[38;5;15m\]\[$(tput bold)\]\[\033[33m\]\$(print_git_data)\[\033[00m\] \[$(tput bold)\]\$?\\$ \[$(tput sgr0)\]"
@aal89
Copy link
Author

aal89 commented Jan 3, 2020

image
(old up to rev: 3)

@aal89
Copy link
Author

aal89 commented Jan 9, 2020

image
(from rev: 3 and up)

@aal89
Copy link
Author

aal89 commented Aug 4, 2020

The ZSH equivalent:

Also include setopt PROMPT_SUBST in the .zshrc.

export PS1='%B%y:%F{green}%~%f%F{yellow}$(print_git_data)%f $ %b'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment