Created
January 16, 2020 19:39
-
-
Save ozydingo/32c6a1683b33d963b668b461e99a3971 to your computer and use it in GitHub Desktop.
Git branch parsing and terminal prompt color setting
This file contains hidden or 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
bash_colors() { | |
export BLACK=$(tput setaf 0) | |
export RED=$(tput setaf 1) | |
export GREEN=$(tput setaf 2) | |
export YELLOW=$(tput setaf 3) | |
export BLUE=$(tput setaf 4) | |
export MAGENTA=$(tput setaf 5) | |
export CYAN=$(tput setaf 6) | |
export WHITE=$(tput setaf 7) | |
export NORMAL=$(tput sgr0) | |
} | |
zsh_colors() { | |
export BLACK='black' | |
export RED='red' | |
export GREEN='green' | |
export YELLOW='yellow' | |
export BLUE='blue' | |
export MAGENTA='magenta' | |
export CYAN='cyan' | |
export WHITE='white' | |
export NORMAL='default' | |
} |
This file contains hidden or 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
# .gitbranch - Provides functions that can be used in setting promt / PS1 | |
# Assumes that env variables $GREEN, $CYAN, %YELLOW, $REDm anbd $NORMAL are set and | |
# correspond to color sequences usable by your shell's output | |
# See or source script 'prompt_colors' to get these. | |
# | |
# Version 0.1.1 | |
# | |
# To use gitbranch to set your prompt to include a colored git branch label: | |
# bash: | |
# PS1='[\[$(git_branch_color)\]$(parse_git_branch)\[${NORMAL}\]] '"$PS1" | |
# zsh: | |
# setopt PROMPT_SUBST | |
# export PROMPT='[%{$fg[$(git_branch_color)]%}$(parse_git_branch)%{$fg[default]%}] '"$PROMPT" | |
parse_git_branch () { | |
if git rev-parse --git-dir >/dev/null 2>&1; then | |
gitver=$(git branch 2>/dev/null| sed -n '/^\*/s/^\* //p') | |
else | |
gitver='none' | |
fi | |
echo -e $gitver | |
} | |
git_branch_status () { | |
if ! git rev-parse --git-dir >/dev/null 2>&1 ; then | |
echo none | |
return 0 | |
fi | |
firstline=1 | |
gitstatus=clean | |
while read line; do | |
if [ $firstline -eq 1 ]; then | |
# Read first line to determine if we are ahead or behind remote | |
firstline=0 | |
if [[ $line =~ \[ahead.+\] ]]; then gitstatus=ahead; fi | |
if [[ $line =~ \[.*behind.+\] ]]; then gitstatus=behind; fi | |
else | |
# Read sceond line: anything here means there's a modified or added file | |
[ -n "$line" ] && gitstatus=dirty | |
fi | |
done < <(git status -sb | head -2) | |
echo -ne $gitstatus | |
} | |
git_branch_color () { | |
case "$(git_branch_status)" in | |
dirty) | |
echo "$RED" | |
;; | |
ahead) | |
echo "$CYAN" | |
;; | |
behind) | |
echo "$YELLOW" | |
;; | |
clean) | |
echo "$GREEN" | |
;; | |
none) | |
echo "$NORMAL" | |
;; | |
*) | |
echo "$NORMAL" | |
echo ".gitbranch error: unrecognized status $(git_branch_status)" >&2 | |
;; | |
esac | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment