Skip to content

Instantly share code, notes, and snippets.

@ecatmur
Last active July 19, 2022 08:24
Show Gist options
  • Save ecatmur/5220649 to your computer and use it in GitHub Desktop.
Save ecatmur/5220649 to your computer and use it in GitHub Desktop.
Minimal bash prompt with return status, command sequence and VCS status (svn, git, hg) using Latin-1 symbols. User, host, path and branch are in title bar.
RD=`tput bold; tput setaf 1`
GN=`tput bold; tput setaf 2`
YL=`tput bold; tput setaf 3`
BL=`tput bold; tput setaf 4`
MG=`tput bold; tput setaf 5`
CY=`tput bold; tput setaf 6`
NC=`tput sgr0`
VCS_PROMPT_CHAR_svn='§'
VCS_PROMPT_CHAR_git='±'
VCS_PROMPT_CHAR_hg='¿'
if [[ `</proc/1/cgroup` = *:/docker/* ]] ; then
[[ "$DOCKER_IMAGE" ]] && DOCKER_PROMPT="(${DOCKER_IMAGE##*/}) " || DOCKER_PROMPT="(docker) "
else
DOCKER_PROMPT=""
fi
RET_PROMPT='RET=$?; (( RET )) && echo -ne "\001$RD\002$RET\001$NC\002|"'
__vcs_prompt() {
local vcs=''
if [[ -d .svn ]]; then
vcs='svn'
elif [[ -d . ]]; then
local rel='.' fs=`stat --printf=%d .`
while ! [[ $rel -ef $rel/.. ]]; do
if [[ -d $rel/.git ]] \
&& ! git clean -xnd $PWD 2>/dev/null | grep -q "Would remove \./"; then
vcs='git'
elif [[ -d $rel/.hg ]]; then
vcs='hg'
fi
[[ "$vcs" ]] && break
rel=$rel/..
[[ `stat --printf=%d $rel` == $fs ]] || break
done
fi
__vcs_prompt_branch=
if [[ "$vcs" ]]; then
__vcs_prompt_char=VCS_PROMPT_CHAR_$vcs
__vcs_prompt_char=${!__vcs_prompt_char}
case "$vcs" in
svn) [[ -n `svn status --ignore-externals | grep -v ^X` ]] && __vcs_col=$RD || __vcs_col=$GN;;
git)
if git status --porcelain=v1 | grep -q '^.[MDRC?]'; then
__vcs_col=$RD # unstaged changes
elif [[ -n `git status --porcelain=v1` ]]; then
__vcs_col=$YL # staged but not committed
elif [[ -n `git cherry 2>&1` ]]; then
__vcs_col=$CY # committed but not pushed
else
__vcs_col=$GN # all safe!
fi
__vcs_prompt_branch=@`git describe --contains --all HEAD 2>&1`
;;
hg) [[ -n `hg status` ]] && __vcs_col=$RD || __vcs_col=$GN;;
esac
elif [[ -d . ]]; then
__vcs_prompt_char='$'
__vcs_col=$BL
else
__vcs_prompt_char='!'
__vcs_col=$RD
fi
}
if [ "$TERM" = xterm ] || [ "$TERM" = xterm-256color ] || ([ -n "$COLORTERM" ] && [ -n "$DISPLAY" ]); then
VCS_PROMPT='__vcs_prompt; echo -e "\001$__vcs_col\002$__vcs_prompt_char\001$NC\033]0;\u@\H \w$__vcs_prompt_branch\007\002"'
PS1="\\[\r\\]$DOCKER_PROMPT\$($RET_PROMPT)\#\$($VCS_PROMPT) "
else
VCS_PROMPT='__vcs_prompt; echo -e "\001$__vcs_col\002$__vcs_prompt_char\001$NC\002"'
PS1="\[$GN\]\u@\h \w\\[\r\\]$DOCKER_PROMPT\$($RET_PROMPT)\$($VCS_PROMPT) "
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment