dotfiles/fschl/files/bash-prompt

77 lines
1.9 KiB
Bash

# -*- mode: sh; -*-
# Adapted from https://jdhao.github.io/2021/03/31/bash_prompt_config/
function fromhex() {
hex=$1
if [[ $hex == "#"* ]]; then
hex=$(echo $1 | awk '{print substr($0,2)}')
fi
r=$(printf '0x%0.2s' "$hex")
g=$(printf '0x%0.2s' ${hex#??})
b=$(printf '0x%0.2s' ${hex#????})
echo -e `printf "%03d" "$(((r<75?0:(r-35)/40)*6*6+(g<75?0:(g-35)/40)*6+(b<75?0:(b-35)/40)+16))"`
}
GRAY="\e[1;30m"
BOLD="\033[1m"
LT_CYAN="\e[1;36m"
LT_GREEN="\e[1;32m"
LT_RED="\e[1;31m"
GREEN="\e[0;32m"
PURPLE="\e[1;35m"
COLOR_NONE="\e[0m"
# Detect whether the current directory is a git repository.
function is_git_repository() {
git branch > /dev/null 2>&1
}
function set_git_branch () {
# Note that for new repo without commit, git rev-parse --abbrev-ref HEAD
# will error out.
if git rev-parse --abbrev-ref HEAD > /dev/null 2>&1; then
BRANCH="$(git rev-parse --abbrev-ref HEAD)"
else
BRANCH="<bare repo>"
fi
}
function set_bash_prompt () {
PS1="\n"
# Host and directory
PS1+="${LT_CYAN}\u@\h${COLOR_NONE} ${LT_RED}${BOLD}${COLOR_NONE} "
PS1+="${GREEN}\w${COLOR_NONE}"
# Add git branch
if is_git_repository; then
set_git_branch
PS1+="${PURPLE}${BRANCH}${COLOR_NONE}"
fi
# Current time in 12 hour format
PS1+="${GREEN}$(date +'%r')${COLOR_NONE}\n"
# Prompt character with color based on exit code
if [ "$?" == "0" ]; then
PS1+="${LT_GREEN}"
else
PS1+="${LT_RED}"
fi
PS1+="λ${COLOR_NONE}"
# If inside vterm, add prompt metadata
if [[ "$INSIDE_EMACS" = 'vterm' ]]; then
PS1=$PS1' \[$(vterm_prompt_end)\]'
fi
}
# Include common vterm functions
if [[ "$INSIDE_EMACS" = 'vterm' ]] \
&& [[ -n ${EMACS_VTERM_PATH} ]] \
&& [[ -f ${EMACS_VTERM_PATH}/etc/emacs-vterm-bash.sh ]]; then
source ${EMACS_VTERM_PATH}/etc/emacs-vterm-bash.sh
fi
export PROMPT_COMMAND=set_bash_prompt