Skip to content

Instantly share code, notes, and snippets.

@danieldietrich
Last active December 11, 2021 05:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danieldietrich/157e87b23bf0c4fe986eaac5321cc9ee to your computer and use it in GitHub Desktop.
Save danieldietrich/157e87b23bf0c4fe986eaac5321cc9ee to your computer and use it in GitHub Desktop.
Git prompt for shell
#
# Usage: `source <(curl -sL https://bashrc.danieldietrich.dev)`
#
export LANG=en_US.UTF-8
export LC_ALL=${LANG}
export TERM=xterm-256color
export GREP_OPTIONS='--color=auto' GREP_COLOR='1;32'
export CLICOLOR=1
export LSCOLORS=gxBxhxDxfxhxhxhxhxcxcx
prompt() {
color() {
if [ -n "$ZSH_VERSION" ]; then
echo -e "%{\e[$1m%}"
else
echo -e "\001\033[$1m\002"
fi
}
# reset the default color
COLOR_RESET=$(color "0")
# used by __git_status()
COLOR_GIT_TEXT=$(color "38;5;8") # gray
COLOR_GIT_STATUS=$(color "38;5;9") # red
COLOR_GIT_UNPUSHED=$(color "38;5;11") # yellow
# used by __pwd()
COLOR_PWD_DARK=$(color "38;5;24") # dark blue
COLOR_PWD_LIGHT=$(color "38;5;39") # light blue
# see https://git-blame.blogspot.com/2013/06/checking-current-branch-programatically.html
__git_status() {
local dir=${1:-.} branch unpushed state
if branch=$(git -C "$dir" symbolic-ref --short -q HEAD 2>/dev/null); then
if git -C "$dir" rev-parse 2>/dev/null; then
if ! unpushed=$(set -o pipefail; git -C "$dir" log origin/"$branch"..HEAD -- 2>/dev/null | head -c1 | if [ "$(wc -c)" -gt "0" ]; then echo "↑"; fi); then
unpushed="•"
fi
state=$(set -o pipefail; git -C "$dir" status --porcelain 2>/dev/null | head -c1 | if [ "$(wc -c)" -gt "0" ]; then echo "✗"; fi)
fi
printf " %s⎇ %s%s%s%s%s%s" "${COLOR_GIT_TEXT}" "${branch}" "${COLOR_GIT_STATUS}" "${state}" "${COLOR_GIT_UNPUSHED}" "${unpushed}" "${COLOR_RESET}"
fi
}
__pwd() {
local pwd="${PWD/#$HOME/~}" dir prefix suffix
dir=$(dirname "$pwd" 2>/dev/null)
prefix=$(if [[ "$dir" == '.' ]] || [[ -z "$dir" ]]; then echo ''; elif [[ "$dir" == '/' ]]; then echo '/'; else echo "$dir/"; fi)
dir=$(basename "$pwd" 2>/dev/null)
suffix=$(if [[ "$dir" == '/' ]]; then echo ''; else echo "$dir"; fi)
printf "%s%s%s%s%s" "${COLOR_PWD_DARK}" "${prefix/#\~/${COLOR_PWD_LIGHT}~${COLOR_PWD_DARK}}" "${COLOR_PWD_LIGHT}" "${suffix}" "${COLOR_RESET}"
}
PS1=$1
}
prompt "\[\e[1m\]\$(__pwd)\$(__git_status) \[\e[38;5;49m\]❯\[\e[0m\] "
#
# Usage: `source <(curl -sL https://zshrc.danieldietrich.dev)`
#
export ZSH_DISABLE_COMPFIX="true"
export LANG=en_US.UTF-8
export LC_ALL=${LANG}
export TERM=xterm-256color
export GREP_OPTIONS='--color=auto' GREP_COLOR='1;32'
export CLICOLOR=1
export LSCOLORS=gxBxhxDxfxhxhxhxhxcxcx
prompt() {
color() {
if [ -n "$ZSH_VERSION" ]; then
echo -e "%{\e[$1m%}"
else
echo -e "\001\033[$1m\002"
fi
}
# reset the default color
COLOR_RESET=$(color "0")
# used by __git_status()
COLOR_GIT_TEXT=$(color "38;5;8") # gray
COLOR_GIT_STATUS=$(color "38;5;9") # red
COLOR_GIT_UNPUSHED=$(color "38;5;11") # yellow
# used by __pwd()
COLOR_PWD_DARK=$(color "38;5;24") # dark blue
COLOR_PWD_LIGHT=$(color "38;5;39") # light blue
# see https://git-blame.blogspot.com/2013/06/checking-current-branch-programatically.html
__git_status() {
local dir=${1:-.} branch unpushed state
if branch=$(git -C "$dir" symbolic-ref --short -q HEAD 2>/dev/null); then
if git -C "$dir" rev-parse 2>/dev/null; then
if ! unpushed=$(set -o pipefail; git -C "$dir" log origin/"$branch"..HEAD -- 2>/dev/null | head -c1 | if [ "$(wc -c)" -gt "0" ]; then echo "↑"; fi); then
unpushed="•"
fi
state=$(set -o pipefail; git -C "$dir" status --porcelain 2>/dev/null | head -c1 | if [ "$(wc -c)" -gt "0" ]; then echo "✗"; fi)
fi
printf " %s⎇ %s%s%s%s%s%s" "${COLOR_GIT_TEXT}" "${branch}" "${COLOR_GIT_STATUS}" "${state}" "${COLOR_GIT_UNPUSHED}" "${unpushed}" "${COLOR_RESET}"
fi
}
__pwd() {
local pwd="${PWD/#$HOME/~}" dir prefix suffix
dir=$(dirname "$pwd" 2>/dev/null)
prefix=$(if [[ "$dir" == '.' ]] || [[ -z "$dir" ]]; then echo ''; elif [[ "$dir" == '/' ]]; then echo '/'; else echo "$dir/"; fi)
dir=$(basename "$pwd" 2>/dev/null)
suffix=$(if [[ "$dir" == '/' ]]; then echo ''; else echo "$dir"; fi)
printf "%s%s%s%s%s" "${COLOR_PWD_DARK}" "${prefix/#\~/${COLOR_PWD_LIGHT}~${COLOR_PWD_DARK}}" "${COLOR_PWD_LIGHT}" "${suffix}" "${COLOR_RESET}"
}
PS1=$1
}
setopt PROMPT_SUBST
prompt "%\$((\$COLUMNS / 2))<…<%B\$(__pwd)\$(__git_status) %F{49}❯%f%b "
RPROMPT="%F{99}%@%f"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment