Created
March 16, 2018 20:46
-
-
Save donwilson/a90ac3288ac660684de3b3885ce4ca10 to your computer and use it in GitHub Desktop.
Add git repo and branch names to colorized bash prompt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function parse_git_repo { | |
local repo_path=$(git rev-parse --show-toplevel 2>/dev/null) | |
[[ ! -z "$repo_path" ]] && basename $repo_path | |
} | |
function parse_git_branch { | |
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/' | |
} | |
function parse_git_info { | |
local repo_name=$(parse_git_repo) | |
local branch_name=$(parse_git_branch) | |
[[ ! -z "${repo_name// }" && ! -z "${branch_name// }" ]] && printf "{\[$(tput sgr0)\]\[\033[38;5;37m\]$repo_name\[$(tput sgr0)\]\[\033[38;5;15m\]/\[$(tput bold)\]\[$(tput sgr0)\]\[\033[38;5;111m\]$branch_name\[$(tput sgr0)\]\[$(tput sgr0)\]\[\033[38;5;15m\]} " | |
} | |
function set_bash_prompt { | |
local git_info=$(parse_git_info) | |
PS1="\[\033[38;5;7m\][\[$(tput sgr0)\]\[\033[38;5;229m\]\u\[$(tput sgr0)\]\[\033[38;5;249m\]@\[$(tput sgr0)\]\[\033[38;5;51m\]\h\[$(tput sgr0)\]\[\033[38;5;7m\]]\[$(tput sgr0)\]\[\033[38;5;15m\] $git_info\[$(tput bold)\]\[$(tput sgr0)\]\[\033[38;5;39m\]\w\[$(tput sgr0)\]\[$(tput sgr0)\]\[\033[38;5;15m\]\n\[$(tput sgr0)\]\[\033[38;5;245m\]\\$\[$(tput sgr0)\]\[\033[38;5;15m\] \[$(tput sgr0)\]" | |
} | |
# http://bashrcgenerator.com/ | |
PROMPT_COMMAND=set_bash_prompt |
Author
donwilson
commented
Mar 16, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment