dotfiles/.functions/ps1.sh

96 lines
3.8 KiB
Bash
Raw Permalink Normal View History

2021-11-11 15:27:24 +00:00
#!/bin/bash
2021-11-14 12:45:49 +00:00
function fetch_bash_ps1(){
2023-09-25 06:58:47 +00:00
local BASH_PS1_URL="${BASH_PS1_URL:-https://git.rre.nu/jonas/dotfiles/raw/branch/main/.functions/ps1.sh}"
2021-11-14 12:45:49 +00:00
if ! command -v curl &> /dev/null;then
echo "curl not found"
return 1
fi
curl -o "${HOME}/.functions/ps1.sh" "$BASH_PS1_URL"
# shellcheck disable=1090
source "${HOME}/.functions/ps1.sh"
}
2021-11-12 11:54:38 +00:00
function bash_ps1 () {
2021-11-11 15:27:24 +00:00
local LAST_EXIT_STATUS=$?
local EXIT_STATUS=""
2021-11-12 10:36:09 +00:00
local GIT_BRANCH=""
local PYTHON_ENV=""
local K8S_CURRENT_CONTEXT=""
2021-11-12 11:21:38 +00:00
local STATUS=""
2021-11-11 15:27:24 +00:00
local PS1_COLOR_OFF='\[\033[0m\]'
local PS1_COLOR_DIVIDER="${PS1_COLOR_DIVIDER:-\[\033[0;32m\]}"
local PS1_COLOR_USERNAME="${PS1_COLOR_USERNAME:-\[\e[34;1m\]}"
local PS1_COLOR_USERHOSTAT="${PS1_COLOR_USERHOSTAT:-\[\e[34;1m\]}"
2022-06-20 07:35:19 +00:00
local PS1_COLOR_HOSTNAME="${PS1_COLOR_HOSTNAME:-\[\e[34;1m\]}"
2021-11-14 12:30:19 +00:00
local PS1_COLOR_GITBRANCH="${PS1_COLOR_GITBRANCH:-\[\e[33;1m\]}"
local PS1_COLOR_VENV="${PS1_COLOR_VENV:-\[\033[0;36m\]}"
local PS1_COLOR_PATH="${PS1_COLOR_PATH:-\[\033[0;37m\]}"
local PS1_COLOR_EXIT_STATUS="${PS1_COLOR_EXIT_STATUS:-\[\033[1;37m\]\[\033[41m\]}"
local PS1_COLOR_KUBERNETES="${PS1_COLOR_KUBERNETES:-\[\033[1;37m\]\[\033[44m\]}"
local PS1_COLOR_TODO="${PS1_COLOR_TODO:-\[\033[1;30m\]\[\033[43m\]}"
local PS1_COLOR_DOTF="${PS1_COLOR_DOTF:-\[\033[1;37m\]\[\033[45m\]}"
2021-11-19 09:41:43 +00:00
local PS1_COLOR_PASS="${PS1_COLOR_PASS:-\[\033[0;30m\]\[\033[47m\]}"
2021-12-14 07:55:35 +00:00
local PS1_COLOR_SSH="${PS1_COLOR_SSH:-\[\033[0;37m\]\[\033[44m\]}"
2021-11-11 15:27:24 +00:00
2021-11-12 10:36:09 +00:00
local CURRENT_CONTEXT=""
2021-11-11 15:27:24 +00:00
2021-11-13 14:42:10 +00:00
export GIT_PS1_SHOWDIRTYSTATE=1
export GIT_PS1_SHOWUNTRACKEDFILES=1
[[ $LAST_EXIT_STATUS != 0 ]] && EXIT_STATUS="${PS1_COLOR_DIVIDER}[${PS1_COLOR_EXIT_STATUS}${LAST_EXIT_STATUS}${PS1_COLOR_DIVIDER}]"
2021-11-14 12:45:49 +00:00
if [[ $(type -t __git_ps1) == function ]];then
GIT_BRANCH="$(__git_ps1 '%s')"
if [[ -n "$GIT_BRANCH" ]];then
GIT_BRANCH="${PS1_COLOR_DIVIDER}─[${PS1_COLOR_GITBRANCH}${GIT_BRANCH}${PS1_COLOR_DIVIDER}]"
fi
fi
2021-11-14 12:30:19 +00:00
[[ -n "$VIRTUAL_ENV" ]] && PYTHON_ENV="${PS1_COLOR_DIVIDER}─[${PS1_COLOR_VENV}${VIRTUAL_ENV##*/}${PS1_COLOR_DIVIDER}]"
2022-07-08 07:32:49 +00:00
if command -v kubectl > /dev/null; then
CURRENT_CONTEXT="$(kubectl config current-context 2>/dev/null)"
2021-11-12 10:36:09 +00:00
if [[ "$CURRENT_CONTEXT" != "default" ]];then
K8S_CURRENT_CONTEXT="${PS1_COLOR_DIVIDER}[${PS1_COLOR_KUBERNETES}${CURRENT_CONTEXT}${PS1_COLOR_DIVIDER}]"
2021-11-12 10:36:09 +00:00
fi
2021-11-11 15:27:24 +00:00
fi
2021-11-12 10:36:09 +00:00
2021-11-12 12:46:32 +00:00
if [[ $(type -t status_dotfiles) == function ]];then
if [[ $(status_dotfiles check_error) == YES ]];then
2021-11-26 09:16:18 +00:00
STATUS="${PS1_COLOR_DOTF}E"
2021-11-12 12:46:32 +00:00
elif [[ $(status_dotfiles check_status) ]];then
2021-11-26 09:16:18 +00:00
STATUS="${PS1_COLOR_DOTF}+"
2021-11-12 12:46:32 +00:00
elif [[ $(status_dotfiles check_push) ]];then
2021-11-26 09:16:18 +00:00
STATUS="${PS1_COLOR_DOTF}p"
2021-11-12 11:21:38 +00:00
fi
fi
2021-11-19 09:41:43 +00:00
if type -t pass > /dev/null; then
if [[ -n "$(pass git status --short 2> /dev/null)" ]];then
2021-11-26 09:16:18 +00:00
STATUS="${STATUS}${PS1_COLOR_PASS}%"
2021-11-19 09:41:43 +00:00
elif [[ -n "$(pass git cherry -v)" ]];then
2021-11-26 09:16:18 +00:00
STATUS="${STATUS}${PS1_COLOR_PASS}p"
2021-11-19 09:41:43 +00:00
fi
fi
2021-12-14 07:55:35 +00:00
if type -t ssh-add > /dev/null;then
if [[ "$(ssh-add -L 2>/dev/null | grep -E -c '^ssh')" == "0" ]];then
2021-12-14 07:55:35 +00:00
STATUS="${STATUS}${PS1_COLOR_SSH}s"
fi
fi
2021-11-19 09:41:43 +00:00
2021-11-12 11:47:28 +00:00
if [[ $(type -t todo_status) == function ]] && [[ "$(todo_status)" == "YES" ]];then
2021-11-26 09:16:18 +00:00
STATUS="${STATUS}${PS1_COLOR_TODO}T"
2021-11-12 11:47:28 +00:00
fi
2021-11-26 09:16:18 +00:00
if [[ -n $STATUS ]];then
STATUS="${PS1_COLOR_DIVIDER}─[${STATUS}${PS1_COLOR_DIVIDER}]"
2021-11-12 11:21:38 +00:00
fi
PS1="\n${PS1_COLOR_DIVIDER}┌──(${PS1_COLOR_USERNAME}\u${PS1_COLOR_USERHOSTAT}@${PS1_COLOR_HOSTNAME}\h${PS1_COLOR_DIVIDER})─[${PS1_COLOR_PATH}\w${PS1_COLOR_DIVIDER}]${EXIT_STATUS}${STATUS}${PYTHON_ENV}${K8S_CURRENT_CONTEXT}${GIT_BRANCH}"
PS1="${PS1}\n${PS1_COLOR_DIVIDER}└─\$${PS1_COLOR_OFF} "
2021-11-11 15:27:24 +00:00
}
2021-11-12 10:36:09 +00:00
2021-11-12 11:54:38 +00:00
export PROMPT_COMMAND=bash_ps1