Skip to content

Instantly share code, notes, and snippets.

@eevmanu
Created February 27, 2022 22:14
Show Gist options
  • Save eevmanu/fa78346cc1d0de653b8f7e6987480a2e to your computer and use it in GitHub Desktop.
Save eevmanu/fa78346cc1d0de653b8f7e6987480a2e to your computer and use it in GitHub Desktop.
custom prompt I don't use anymore because I prefer [startship](https://github.com/starship/starship)
function custom_prompt {
# https://tldp.org/HOWTO/Bash-Prompt-HOWTO/bash-prompt-escape-sequences.html
# \e == \033
# Select Graphic Rendition parameters
# 30–37 selected the foreground color
# 40–47 selected the background color
# prompt
FMT_RESET="\[\e[0m\]"
FMT_BOLD="\[\e[1m\]"
FMT_DIM="\[\e[2m\]"
FMT_NORMAL="\[\e[22m\]"
FG_BLACK="\[\e[30m\]"
FG_RED="\[\e[31m\]"
FG_GREEN="\[\e[32m\]"
FG_YELLOW="\[\e[33m\]"
FG_BLUE="\[\e[34m\]"
FG_MAGENTA="\[\e[35m\]"
FG_CYAN="\[\e[36m\]"
FG_WHITE="\[\e[37m\]"
BG_BLACK="\[\e[40m\]"
BG_RED="\[\e[41m\]"
BG_GREEN="\[\e[42m\]"
BG_YELLOW="\[\e[43m\]"
BG_BLUE="\[\e[44m\]"
BG_MAGENTA="\[\e[45m\]"
BG_CYAN="\[\e[46m\]"
BG_WHITE="\[\e[47m\]"
FG_BRIGHT_WHITE="\[\e[97m\]"
PS1="\n "
PS1+="${FG_BLUE}╭─" # begin arrow to prompt
PS1+="${FMT_RESET}"
PS1+="${FG_CYAN}" # begin USERNAME container
PS1+="${BG_CYAN} "
PS1+="${BG_CYAN}👋" # print OS icon
PS1+="${BG_CYAN} "
# DT="\D{%Y-%m-%d} \t\[$(tput sgr0)\]"
DT="\t\[$(tput sgr0)\]"
PS1+="${BG_CYAN}${FG_BLACK}${DT}"
PS1+="${FMT_NORMAL}"
# not reliable enough
# https://stackoverflow.com/q/1871549
if [ ! -z "$VIRTUAL_ENV" -a "$VIRTUAL_ENV" != " " ]; then
PS1+="${BG_CYAN} "
PS1+="${BG_CYAN}${FG_BRIGHT_WHITE}($(basename ${VIRTUAL_ENV}))"
PS1+="${FMT_NORMAL}"
fi
if [ ! -z "$VIRTUAL_ENV" -a "$VIRTUAL_ENV" != " " ]
then
PYTHON_VERSION="[🐍 `python -c "print(__import__('sys').version.split(' ')[0])"`]"
PS1+="${BG_CYAN} "
PS1+="${BG_CYAN}${FG_BRIGHT_WHITE}${PYTHON_VERSION}"
PS1+="${FMT_NORMAL}"
elif command -v pyenv 1>/dev/null 2>&1
then
if [[ `pyenv version-name` != "system" ]]
then
PYTHON_VERSION="[🐍 `pyenv version-name`]"
PS1+="${BG_CYAN} "
PS1+="${BG_CYAN}${FG_BRIGHT_WHITE}${PYTHON_VERSION}"
PS1+="${FMT_NORMAL}"
fi
fi
PS1+="${BG_CYAN} "
PS1+="${FG_CYAN}${BG_MAGENTA}" # begin USERNAME container
PS1+="${BG_MAGENTA} "
PS1+="${FG_BRIGHT_WHITE}${FMT_BOLD}\u@\H" # print username
PS1+="${FMT_NORMAL}"
PS1+="${BG_MAGENTA} " # end USERNAME container
PS1+="${FG_MAGENTA}${BG_BLUE}" # begin DIRECTORY container
PS1+="${BG_BLUE} "
PS1+="${FG_WHITE}\w" # print directory
PS1+="${FMT_NORMAL}"
PS1+="${BG_BLUE} " # end DIRECTORY container
PS1+="${FG_BLUE}${BG_CYAN}" # begin FILES container
PS1+="${BG_CYAN} "
PS1+="${FG_BLACK}"
PS1+=" \$(find . -mindepth 1 -maxdepth 1 -type d | wc -l) " # print number of folders
PS1+=" \$(find . -mindepth 1 -maxdepth 1 -type f | wc -l) " # print number of files
PS1+=" \$(find . -mindepth 1 -maxdepth 1 -type l | wc -l) " # print number of symlinks
PS1+="${FMT_RESET}${FG_CYAN}"
BRANCH=`git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'`
if [ ! "${BRANCH}" == "" ]
then
STAT=`parse_git_dirty`
PS1+="${BG_GREEN}"
PS1+="${BG_GREEN} "
PS1+="${BG_GREEN}${FG_BLACK}"
PS1+="${FMT_NORMAL}"
PS1+="${BG_GREEN} "
PS1+="${BG_GREEN}${FG_BLACK}${BRANCH}${STAT}"
PS1+="${FMT_RESET}"
PS1+="${FG_GREEN}"
fi
PS1+="\n " # end last container (either FILES or BRANCH)
PS1+="${FG_BLUE}╰ " # end arrow to prompt
PS1+="${FG_CYAN}\\$ " # print prompt
PS1+="${FMT_RESET}"
}
# time (hour, minute, seconds)
# if virtual env activated
# python version
# path of working directory
# git branch (and states)
# elapsed time last command
PROMPT_COMMAND=custom_prompt
# get current status of git repo
function parse_git_dirty {
status=`git status 2>&1 | tee`
dirty=`echo -n "${status}" 2> /dev/null | grep "modified:" &> /dev/null; echo "$?"`
untracked=`echo -n "${status}" 2> /dev/null | grep "Untracked files" &> /dev/null; echo "$?"`
ahead=`echo -n "${status}" 2> /dev/null | grep "Your branch is ahead of" &> /dev/null; echo "$?"`
newfile=`echo -n "${status}" 2> /dev/null | grep "new file:" &> /dev/null; echo "$?"`
renamed=`echo -n "${status}" 2> /dev/null | grep "renamed:" &> /dev/null; echo "$?"`
deleted=`echo -n "${status}" 2> /dev/null | grep "deleted:" &> /dev/null; echo "$?"`
bits=''
if [ "${renamed}" == "0" ]; then
bits=">${bits}"
fi
if [ "${ahead}" == "0" ]; then
bits="*${bits}"
fi
if [ "${newfile}" == "0" ]; then
bits="+${bits}"
fi
if [ "${untracked}" == "0" ]; then
bits="?${bits}"
fi
if [ "${deleted}" == "0" ]; then
bits="x${bits}"
fi
if [ "${dirty}" == "0" ]; then
bits="!${bits}"
fi
if [ ! "${bits}" == "" ]; then
echo " ${bits}"
else
echo ""
fi
}
# don't remember where I call to PROMPT_COMMAND or overwrite PS1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment