This commit is contained in:
2026-01-24 03:35:30 +00:00
parent ca3a0a7c93
commit e61b41bbbf

120
ps1.sh
View File

@@ -13,116 +13,58 @@ esac
# ------------------------------------------------- # -------------------------------------------------
# Time-based symbol (Europe/Oslo) # Time-based symbol (Europe/Oslo)
# Updates naturally when time passes (no session-lock).
# ------------------------------------------------- # -------------------------------------------------
_ps1_symbol() { _ps1_symbol() {
local hh mm h m local h m
hh="$(TZ=Europe/Oslo date +%H)" h=$(TZ=Europe/Oslo date +%H)
mm="$(TZ=Europe/Oslo date +%M)" m=$(TZ=Europe/Oslo date +%M)
h=$((10#$hh)) h=$((10#$h))
m=$((10#$mm)) m=$((10#$m))
if (( h >= 5 && h <= 8 )); then if (( h >= 5 && h <= 8 )); then
printf '%s' "🌅" # tidlig echo "🌅"
elif (( h >= 9 && h <= 10 )); then elif (( h >= 9 && h <= 10 )); then
printf '%s' "☕" # formiddag echo "☕"
elif (( h == 11 && m < 30 )); then elif (( h == 11 && m < 30 )); then
printf '%s' "🥪" # lunsj echo "🥪"
elif (( (h == 11 && m >= 30) || (h >= 12 && h <= 15) )); then elif (( (h == 11 && m >= 30) || (h >= 12 && h <= 15) )); then
printf '%s' "💻" # dag echo "💻"
elif (( h == 16 )); then elif (( h == 16 )); then
printf '%s' "🍲" # middag echo "🍲"
elif (( h >= 17 && h <= 22 )); then elif (( h >= 17 && h <= 22 )); then
printf '%s' "🌆" # kveld echo "🌆"
else else
printf '%s' "🌙" # natt echo "🌙"
fi fi
} }
# ------------------------------------------------- # -------------------------------------------------
# Path shortening (keeps /home/user visible; shortens deeper paths) # Path shortening (keeps /home/user)
# Example:
# /home/steffen/projects/very/long/path -> /home/steffen/…/long/path
# ------------------------------------------------- # -------------------------------------------------
_ps1_path() { _ps1_path() {
local p="$PWD" local p="$PWD"
local max_tail=2 # keep last 2 path components
local parts=() local parts=()
IFS='/' read -ra parts <<< "$p"
# Split path into array
IFS='/' read -r -a parts <<< "$p"
# If path is short enough, return it unchanged
# Heuristic: fewer than 6 components -> keep full path
if ((${#parts[@]} < 6)); then if ((${#parts[@]} < 6)); then
printf '%s' "$p" echo "$p"
return return
fi fi
# Build prefix: / + first 2 non-empty components (e.g. home/steffen) echo "/${parts[1]}/${parts[2]}/…/${parts[-2]}/${parts[-1]}"
local prefix="/"
local taken=0
local i
for ((i=0; i<${#parts[@]}; i++)); do
[[ -z "${parts[i]}" ]] && continue
prefix+="${parts[i]}"
taken=$((taken+1))
if (( taken >= 2 )); then
break
fi
prefix+="/"
done
# Build tail: last N components
local tail=""
local count=0
for ((i=${#parts[@]}-1; i>=0; i--)); do
[[ -z "${parts[i]}" ]] && continue
tail="/${parts[i]}${tail}"
count=$((count+1))
if (( count >= max_tail )); then
break
fi
done
printf '%s' "${prefix}/…${tail}"
} }
# ------------------------------------------------- # -------------------------------------------------
# Build PS1 # Variables updated before each prompt
# Adds:
# 2) Exit-status feedback: arrow + $ turns red when last cmd failed
# 3) Shorter path (but keeps /home/user visible)
# ------------------------------------------------- # -------------------------------------------------
_ps1_build() { __PS1_SYM=""
local sym path __PS1_PATH=""
sym="$(_ps1_symbol)" __PS1_STATUS=0
path="$(_ps1_path)"
# Pastel-ish 256 colors _ps1_update() {
local BOX="\[\e[38;5;183m\]" __PS1_STATUS=$?
local VAL="\[\e[38;5;117m\]" __PS1_SYM="$(_ps1_symbol)"
local ACC="\[\e[38;5;229m\]" __PS1_PATH="$(_ps1_path)"
local RST="\[\e[0m\]"
# Status colors
local OK="\[\e[38;5;76m\]" # green-ish
local BAD="\[\e[38;5;203m\]" # red-ish
# Arrow + $ changes color based on last exit code.
# NOTE: PROMPT_COMMAND runs after the command; $? is last command status here.
local status_color
if [[ "${__PS1_LAST_STATUS:-0}" -eq 0 ]]; then
status_color="$OK"
else
status_color="$BAD"
fi
local ps1
ps1="${BOX}╭─(${ACC}\\d${BOX} ${ACC}\\A${BOX})-(${VAL}\\u${BOX}@${VAL}\\h${BOX})-(${VAL}${path}${BOX})${RST}\\n${BOX}╰──${status_color}➜ \\$${RST} ${sym} "
printf '%s' "$ps1"
} }
# ------------------------------------------------- # -------------------------------------------------
@@ -131,10 +73,16 @@ _ps1_build() {
__PS1_PREV_PROMPT_COMMAND="${PROMPT_COMMAND-}" __PS1_PREV_PROMPT_COMMAND="${PROMPT_COMMAND-}"
ps1_on() { ps1_on() {
# Capture last command status each time before building PS1 PROMPT_COMMAND="_ps1_update"
PROMPT_COMMAND='__PS1_LAST_STATUS=$?; _PS1="$(_ps1_build)"; PS1="$_PS1"'
__PS1_LAST_STATUS=$? local BOX="\[\e[38;5;183m\]"
PS1="$(_ps1_build)" local VAL="\[\e[38;5;117m\]"
local ACC="\[\e[38;5;229m\]"
local OK="\[\e[38;5;76m\]"
local BAD="\[\e[38;5;203m\]"
local RST="\[\e[0m\]"
PS1="${BOX}╭─(${ACC}\d${BOX} ${ACC}\A${BOX})-(${VAL}\u${BOX}@${VAL}\h${BOX})-(${VAL}\${__PS1_PATH}${BOX})${RST}\n${BOX}╰──\$( [ \$__PS1_STATUS -eq 0 ] && printf '${OK}' || printf '${BAD}' )➜ ${RST}\$ \${__PS1_SYM} "
} }
ps1_off() { ps1_off() {