Skip to content

Instantly share code, notes, and snippets.

@yehchge
Created July 22, 2022 02:24
Show Gist options
  • Save yehchge/101221f3c4fb2cb2f363e0715671687f to your computer and use it in GitHub Desktop.
Save yehchge/101221f3c4fb2cb2f363e0715671687f to your computer and use it in GitHub Desktop.
linux prompt bashrc 2
function __repository_name() {
toplevel="$(git rev-parse --show-toplevel 2> /dev/null)"
if [ -n "$toplevel" ]; then
printf "$(basename $toplevel)"
fi
}
function __branch_name() {
name="$(git symbolic-ref --short HEAD 2> /dev/null)"
if [ "$name" = 'master' -o "$name" = 'main' -o "$name" = 'develop' ]; then
printf "\033[41m$name\033[0m"
elif [ -n "$name" ]; then
printf "\033[95m$name\033[0m"
fi
}
function __git_info() {
hash=$(git rev-parse --short HEAD 2> /dev/null)
if [ -n "$hash" ]; then
branch=$(__branch_name)
ts=$(git show -s --format="%ad" --date=relative HEAD)
printf " $branch:\033[92m$hash\033[0m ($ts)"
fi
}
function __git_path() {
dir=$(pwd)
root=$(git rev-parse --show-toplevel 2> /dev/null)
if [ "$root" != "" ]; then
dir=${root##*/}$(echo $dir | sed -E "s|^$root||")
elif [[ "$dir" == "$HOME"* ]]; then
dir="~$(echo $dir | sed -E "s|^$HOME||")"
fi
printf "$dir"
}
export PS1='\n\[\033[37m\][\[\033[34m\]\t \[\033[32m\]\u\[\033[37m\]@\[\033[36m\]\h\[\033[37m\] \[\033[33m\]$(__git_path)\[\033[0m\]\[\033[37m\]]\[\033[0m\]$(__git_info)\n$ '
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment