Files
Bashrc/dotfiles/ps1/ps1.sh
2026-01-24 07:38:48 +00:00

140 lines
3.5 KiB
Bash

#!/usr/bin/env bash
# PS1 module with Nerd Font fallback + disable switch
# Designed for bash 3.2+ (macOS) and bash 4/5 (Linux)
# Load this in interactive shells.
case "$-" in
*i*) ;;
*) return 0 ;;
esac
# Disable switch (per-user/per-shell)
# 1) env: DISABLE_GLOBAL_PS1=1
# 2) file: ~/.config/ps1/disable
if [[ "${DISABLE_GLOBAL_PS1:-0}" == "1" ]]; then
return 0
fi
if [[ -f "$HOME/.config/ps1/disable" ]]; then
return 0
fi
# Nerd Font detection (best-effort) + overrides
# PS1_FORCE_ASCII=1 -> always fallback separators
# PS1_FORCE_NF=1 -> always Nerd Font separators
_ps1_has_nf() {
if [[ "${PS1_FORCE_ASCII:-0}" == "1" ]]; then return 1; fi
if [[ "${PS1_FORCE_NF:-0}" == "1" ]]; then return 0; fi
# Linux/WSL: fontconfig often available
if command -v fc-list >/dev/null 2>&1; then
fc-list 2>/dev/null | grep -qi "Nerd Font" && return 0
fi
# macOS: fontconfig often not present; default to fallback unless forced
return 1
}
# Time-based emoji (Europe/Oslo)
_ps1_symbol() {
local hh mm h m
hh=$(TZ=Europe/Oslo date +%H); mm=$(TZ=Europe/Oslo date +%M)
h=$((10#$hh)); m=$((10#$mm))
if (( h >= 5 && h <= 8 )); then echo "🌅"
elif (( h >= 9 && h <= 10 )); then echo "☕"
elif (( h == 11 && m < 30 )); then echo "🥪"
elif (( (h == 11 && m >= 30) || (h >= 12 && h <= 15) )); then echo "💻"
elif (( h == 16 )); then echo "🍲"
elif (( h >= 17 && h <= 22 )); then echo "🌆"
else echo "🌙"
fi
}
# Path shortening (keeps /home/user or /Users/user visible)
# bash 3.2 safe
_ps1_path() {
local p="$PWD"
local parts=()
IFS='/' read -ra parts <<< "$p"
if ((${#parts[@]} < 6)); then
echo "$p"; return
fi
local n=${#parts[@]}
echo "/${parts[1]}/${parts[2]}/…/${parts[$((n-2))]}/${parts[$((n-1))]}"
}
# Dynamic vars updated before each prompt
__PS1_SYM=""
__PS1_PATH=""
__PS1_STATUS=0
__PS1_USE_NF=0
__PS1_TEMPLATE=""
_ps1_build_template() {
local RST="\[\e[0m\]"
# Two-zone palette
# Zone 1: gray-blue pastel (date/time/user)
local Z1_BG="\[\e[48;5;61m\]"
local Z1_FG="\[\e[38;5;255m\]"
# Zone 2: turquoise / cool green (host/path)
local Z2_BG="\[\e[48;5;37m\]"
local Z2_FG="\[\e[38;5;255m\]"
local PATH_FG="\[\e[38;5;194m\]"
# Frame
local FRAME="\[\e[38;5;60m\]"
# Status colors
local OK="\[\e[38;5;76m\]"
local BAD="\[\e[38;5;203m\]"
# Separators with fallback
local SEP_EXPR='\$( [ "$__PS1_USE_NF" -eq 1 ] && printf "" || printf "▶" )'
local LEFT_EXPR='\$( [ "$__PS1_USE_NF" -eq 1 ] && printf "" || printf "[" )'
local RIGHT_EXPR='\$( [ "$__PS1_USE_NF" -eq 1 ] && printf "" || printf "]" )'
local PROMPT_SYM="\$( [ \$__PS1_STATUS -eq 0 ] && printf '${OK}' || printf '${BAD}' )➜${RST}"
__PS1_TEMPLATE="\
${FRAME}╭─${RST}\
${Z1_BG}${Z1_FG}${LEFT_EXPR} \d \A \u ${RST}${Z1_BG}${Z2_BG}${Z2_FG}${SEP_EXPR}${RST}\
${Z2_BG}${Z2_FG} @\h ${PATH_FG}\${__PS1_PATH} ${RST}${Z2_BG}${Z2_FG}${RIGHT_EXPR}${RST}\
\n${FRAME}╰── ${RST}${PROMPT_SYM} \${__PS1_SYM} "
}
_ps1_set_prompt() {
if [[ -z "${__PS1_TEMPLATE}" ]]; then
_ps1_build_template
fi
PS1="${__PS1_TEMPLATE}"
}
_ps1_update() {
__PS1_STATUS=$?
__PS1_SYM="$(_ps1_symbol)"
__PS1_PATH="$(_ps1_path)"
if _ps1_has_nf; then __PS1_USE_NF=1; else __PS1_USE_NF=0; fi
_ps1_set_prompt
}
ps1_on() {
if [[ "${PROMPT_COMMAND:-}" != *"_ps1_update"* ]]; then
if [[ -n "${PROMPT_COMMAND:-}" ]]; then
PROMPT_COMMAND="${PROMPT_COMMAND}; _ps1_update"
else
PROMPT_COMMAND="_ps1_update"
fi
fi
_ps1_set_prompt
}
ps1_off() { :; }
# Enable by default when loaded (global standard)
ps1_on