This commit is contained in:
120
ps1.sh
120
ps1.sh
@@ -13,116 +13,58 @@ esac
|
||||
|
||||
# -------------------------------------------------
|
||||
# Time-based symbol (Europe/Oslo)
|
||||
# Updates naturally when time passes (no session-lock).
|
||||
# -------------------------------------------------
|
||||
_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))
|
||||
local h m
|
||||
h=$(TZ=Europe/Oslo date +%H)
|
||||
m=$(TZ=Europe/Oslo date +%M)
|
||||
h=$((10#$h))
|
||||
m=$((10#$m))
|
||||
|
||||
if (( h >= 5 && h <= 8 )); then
|
||||
printf '%s' "🌅" # tidlig
|
||||
echo "🌅"
|
||||
elif (( h >= 9 && h <= 10 )); then
|
||||
printf '%s' "☕" # formiddag
|
||||
echo "☕"
|
||||
elif (( h == 11 && m < 30 )); then
|
||||
printf '%s' "🥪" # lunsj
|
||||
echo "🥪"
|
||||
elif (( (h == 11 && m >= 30) || (h >= 12 && h <= 15) )); then
|
||||
printf '%s' "💻" # dag
|
||||
echo "💻"
|
||||
elif (( h == 16 )); then
|
||||
printf '%s' "🍲" # middag
|
||||
echo "🍲"
|
||||
elif (( h >= 17 && h <= 22 )); then
|
||||
printf '%s' "🌆" # kveld
|
||||
echo "🌆"
|
||||
else
|
||||
printf '%s' "🌙" # natt
|
||||
echo "🌙"
|
||||
fi
|
||||
}
|
||||
|
||||
# -------------------------------------------------
|
||||
# Path shortening (keeps /home/user visible; shortens deeper paths)
|
||||
# Example:
|
||||
# /home/steffen/projects/very/long/path -> /home/steffen/…/long/path
|
||||
# Path shortening (keeps /home/user)
|
||||
# -------------------------------------------------
|
||||
_ps1_path() {
|
||||
local p="$PWD"
|
||||
local max_tail=2 # keep last 2 path components
|
||||
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
|
||||
printf '%s' "$p"
|
||||
echo "$p"
|
||||
return
|
||||
fi
|
||||
|
||||
# Build prefix: / + first 2 non-empty components (e.g. home/steffen)
|
||||
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}"
|
||||
echo "/${parts[1]}/${parts[2]}/…/${parts[-2]}/${parts[-1]}"
|
||||
}
|
||||
|
||||
# -------------------------------------------------
|
||||
# Build PS1
|
||||
# Adds:
|
||||
# 2) Exit-status feedback: arrow + $ turns red when last cmd failed
|
||||
# 3) Shorter path (but keeps /home/user visible)
|
||||
# Variables updated before each prompt
|
||||
# -------------------------------------------------
|
||||
_ps1_build() {
|
||||
local sym path
|
||||
sym="$(_ps1_symbol)"
|
||||
path="$(_ps1_path)"
|
||||
__PS1_SYM=""
|
||||
__PS1_PATH=""
|
||||
__PS1_STATUS=0
|
||||
|
||||
# Pastel-ish 256 colors
|
||||
local BOX="\[\e[38;5;183m\]"
|
||||
local VAL="\[\e[38;5;117m\]"
|
||||
local ACC="\[\e[38;5;229m\]"
|
||||
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"
|
||||
_ps1_update() {
|
||||
__PS1_STATUS=$?
|
||||
__PS1_SYM="$(_ps1_symbol)"
|
||||
__PS1_PATH="$(_ps1_path)"
|
||||
}
|
||||
|
||||
# -------------------------------------------------
|
||||
@@ -131,10 +73,16 @@ _ps1_build() {
|
||||
__PS1_PREV_PROMPT_COMMAND="${PROMPT_COMMAND-}"
|
||||
|
||||
ps1_on() {
|
||||
# Capture last command status each time before building PS1
|
||||
PROMPT_COMMAND='__PS1_LAST_STATUS=$?; _PS1="$(_ps1_build)"; PS1="$_PS1"'
|
||||
__PS1_LAST_STATUS=$?
|
||||
PS1="$(_ps1_build)"
|
||||
PROMPT_COMMAND="_ps1_update"
|
||||
|
||||
local BOX="\[\e[38;5;183m\]"
|
||||
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() {
|
||||
|
||||
Reference in New Issue
Block a user