From 0670c4910a615244900af884a7919509d99e2176 Mon Sep 17 00:00:00 2001 From: ben Date: Sat, 3 May 2025 14:17:11 -0700 Subject: bash --- config/bash/ps1.sh | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 config/bash/ps1.sh (limited to 'config/bash/ps1.sh') diff --git a/config/bash/ps1.sh b/config/bash/ps1.sh new file mode 100644 index 0000000..35a182a --- /dev/null +++ b/config/bash/ps1.sh @@ -0,0 +1,62 @@ +function parse_git_branch { + git rev-parse --is-inside-work-tree &>/dev/null || return + local branch=$(git symbolic-ref --short HEAD 2>/dev/null || git describe --tags --exact-match 2>/dev/null) + + echo "(${branch})" +} + + +## ✗ ✓ ↑ ↓ ↲ ↲ ↳ ↴ ↵ ↺ ➔ ➙ ➜ ➥ +function parse_git_status { + local symbol="✓" # clean state (up to date) + + if git rev-parse --is-inside-work-tree &>/dev/null; then + local branch + branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null) + + # Ahead / behind remote + if [[ "$branch" != "HEAD" ]]; then + local behind=$(git rev-list --count HEAD..origin/$branch 2>/dev/null || echo 0) + local ahead=$(git rev-list --count origin/$branch..HEAD 2>/dev/null || echo 0) + if [[ "$ahead" -gt 0 ]]; then + symbol="↑" + elif [[ "$behind" -gt 0 ]]; then + symbol="↓" + fi + fi + + # Unstaged changes + if ! git diff --quiet 2>/dev/null; then + symbol="✗" + fi + if ! git diff --cached --quiet 2>/dev/null; then + symbol="✗" + fi + + # untracked files + if git ls-files --others --exclude-standard -z | grep -q .; then + symbol="?" + fi + fi + + echo "${symbol}" +} + +function set_PS1 { + WHITE="\[\033[01;015m\]" + YELLOW="\[\033[01;33m\]" + BLUE="\[\033[01;34m\]" + RED="\[\033[01;31m\]" + PINK="\[\033[01;35m\]" + RESET="\[\033[0m\]" + + if git rev-parse --is-inside-work-tree &>/dev/null; then + PS1="${YELLOW}\u@\h ${BLUE}[$(basename "$PWD")]${RED}$(parse_git_branch) ${RESET}$(parse_git_status) ${PINK}\$:${RESET} " + else + PS1="${WHITE}\u@\h \$: ${RESET}" + #PS1="\[\033[01;015m\]\u@\h $: \[\033[0m\]" + fi +} + +set_PS1 +PROMPT_COMMAND=set_PS1 -- cgit v1.2.3