Last active
October 25, 2023 18:33
-
-
Save chiuki/3430887 to your computer and use it in GitHub Desktop.
Bash prompt with time, path, git branch and exit status
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
function prompt_command { | |
exitstatus="$?" | |
BOLD="\[\033[1m\]" | |
RED="\[\033[1;31m\]" | |
GREEN="\[\e[32;1m\]" | |
BLUE="\[\e[34;1m\]" | |
PURPLE="\[\e[35;1m\]" | |
CYAN="\[\e[36;1m\]" | |
OFF="\[\033[m\]" | |
time=`date +"%H:%M"` | |
branch=`git symbolic-ref HEAD 2> /dev/null | sed -e 's/refs\/heads\///g'` | |
if [ ! -z ${branch} ]; then | |
if [ ${branch} == "master" ]; then | |
branch=`echo " ${CYAN}${branch}"` | |
else | |
branch=`echo " ${PURPLE}${branch}"` | |
fi | |
fi | |
changes=`git status -s 2> /dev/null | \ | |
wc -l | sed -e 's/ *//'` | |
if [ ${changes} -eq 0 ]; then | |
dirty="" | |
else | |
dirty="${RED}*${OFF}" | |
fi | |
prompt="\u@\h ${CYAN}${time}${OFF} \ | |
${BLUE}\w${OFF}${PURPLE}${branch}${OFF}${dirty}" | |
if [ ${exitstatus} -eq 0 ]; then | |
PS1="${prompt} ${GREEN}\\$ ${OFF}" | |
else | |
PS1="${prompt} ${RED}\\$ ${OFF}" | |
fi | |
PS2="${BOLD}>${OFF} " | |
dir=`pwd` | |
title=`basename ${dir}` | |
echo -n -e "\033]0;${title}\007" | |
} | |
PROMPT_COMMAND=prompt_command |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment