This commit is contained in:
2026-01-24 03:21:23 +00:00
parent eea3b702de
commit ec5405d82c

102
ps1.sh
View File

@@ -1,89 +1,86 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# -------------------------------------------------
# ps1.sh custom PS1 (must be sourced)
# -------------------------------------------------
# Must be sourced, not executed # Must be sourced, not executed
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
echo "Dette scriptet må kjøres med:" echo "Bruk: source ps1.sh"
echo " source ps1.sh"
echo "eller:"
echo " . ps1.sh"
exit 1 exit 1
fi fi
# Only for interactive shells
case "$-" in case "$-" in
*i*) ;; *i*) ;;
*) return 0 ;; *) return 0 ;;
esac esac
# ------------------------------------------------- # Returns 0 if current shell locale is UTF-8 capable
# Emoji based on time (Norway / Europe/Oslo) _ps1_is_utf8() {
# Ranges: # locale charmap outputs e.g. UTF-8
# 05:00-08:59 tidlig local cm
# 09:00-10:59 formiddag cm="$(LC_ALL=${LC_ALL-} LANG=${LANG-} locale charmap 2>/dev/null || true)"
# 11:00-11:29 lunsj [[ "$cm" == "UTF-8" ]]
# 11:30-15:59 dag }
# 16:00-16:59 middag
# 17:00-22:59 kveld # Emoji based on time (Europe/Oslo) with ASCII fallback if not UTF-8
# 23:00-04:59 natt _ps1_symbol() {
# -------------------------------------------------
_ps1_emoji() {
local hh mm h m local hh mm h m
hh="$(TZ=Europe/Oslo date +%H)" hh="$(TZ=Europe/Oslo date +%H)"
mm="$(TZ=Europe/Oslo date +%M)" mm="$(TZ=Europe/Oslo date +%M)"
h=$((10#$hh)) h=$((10#$hh))
m=$((10#$mm)) m=$((10#$mm))
if (( h >= 5 && h <= 8 )); then if _ps1_is_utf8; then
printf '%s' "🌅" # tidlig if (( h >= 5 && h <= 8 )); then
elif (( h >= 9 && h <= 10 )); then printf '%s' "🌅"
printf '%s' "☕" # formiddag elif (( h >= 9 && h <= 10 )); then
elif (( h == 11 && m < 30 )); then printf '%s' "☕"
printf '%s' "🥪" # lunsj elif (( h == 11 && m < 30 )); then
elif (( (h == 11 && m >= 30) || (h >= 12 && h <= 15) )); then printf '%s' "🥪"
printf '%s' "💻" # dag elif (( (h == 11 && m >= 30) || (h >= 12 && h <= 15) )); then
elif (( h == 16 )); then printf '%s' "💻"
printf '%s' "🍲" # middag elif (( h == 16 )); then
elif (( h >= 17 && h <= 22 )); then printf '%s' "🍲"
printf '%s' "🌆" # kveld elif (( h >= 17 && h <= 22 )); then
printf '%s' "🌆"
else
printf '%s' "🌙"
fi
else else
printf '%s' "🌙" # natt # ASCII fallbacks
if (( h >= 5 && h <= 8 )); then
printf '%s' "^" # early
elif (( h >= 9 && h <= 10 )); then
printf '%s' "o" # morning
elif (( h == 11 && m < 30 )); then
printf '%s' "=" # lunch
elif (( (h == 11 && m >= 30) || (h >= 12 && h <= 15) )); then
printf '%s' "*" # day
elif (( h == 16 )); then
printf '%s' "#" # dinner
elif (( h >= 17 && h <= 22 )); then
printf '%s' "~" # evening
else
printf '%s' "." # night
fi
fi fi
} }
# -------------------------------------------------
# Build PS1 (boxy + pastel contrast + full path)
# NOTE: We DO NOT use printf %b, because \u etc are PS1 escapes (not printf escapes)
# We keep PS1 escapes as \\u, \\h, \\d, \\A, \\n so bash expands them in the prompt.
# -------------------------------------------------
_ps1_build() { _ps1_build() {
local emoji path local sym path
emoji="$(_ps1_emoji)" sym="$(_ps1_symbol)"
path="$(pwd)" path="$(pwd)"
# Pastel-ish 256 colors: # Pastel-ish 256 colors
# - box: 183 (light purple)
# - value: 117 (light cyan)
# - accent 229 (light yellow)
local BOX="\[\e[38;5;183m\]" local BOX="\[\e[38;5;183m\]"
local VAL="\[\e[38;5;117m\]" local VAL="\[\e[38;5;117m\]"
local ACC="\[\e[38;5;229m\]" local ACC="\[\e[38;5;229m\]"
local RST="\[\e[0m\]" local RST="\[\e[0m\]"
# Use PS1 escapes for date/time/user/host and a PS1 newline (\\n) # IMPORTANT: keep PS1 escapes as \\d \\A \\u \\h \\n
# Use full path from $(pwd) so /home/user is shown (no ~ shortening)
local ps1 local ps1
ps1="${BOX}╭─(${ACC}\\d${BOX} ${ACC}\\A${BOX})-(${VAL}\\u${BOX}@${VAL}\\h${BOX})-(${VAL}${path}${BOX})${RST}\\n${BOX}╰──➜ ${RST}\\$${emoji} " ps1="${BOX}╭─(${ACC}\\d${BOX} ${ACC}\\A${BOX})-(${VAL}\\u${BOX}@${VAL}\\h${BOX})-(${VAL}${path}${BOX})${RST}\\n${BOX}╰──➜ ${RST}\\$${sym} "
printf '%s' "$ps1" printf '%s' "$ps1"
} }
# -------------------------------------------------
# PROMPT_COMMAND handling
# -------------------------------------------------
__PS1_PREV_PROMPT_COMMAND="${PROMPT_COMMAND-}" __PS1_PREV_PROMPT_COMMAND="${PROMPT_COMMAND-}"
ps1_on() { ps1_on() {
@@ -94,6 +91,3 @@ ps1_on() {
ps1_off() { ps1_off() {
PROMPT_COMMAND="$__PS1_PREV_PROMPT_COMMAND" PROMPT_COMMAND="$__PS1_PREV_PROMPT_COMMAND"
} }
# Optional auto-enable (leave commented)
# ps1_on