Files
Bashrc/ps1.sh
2026-01-24 02:44:38 +00:00

101 lines
2.6 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
# -------------------------------------------------
# ps1.sh custom PS1 (must be sourced)
# -------------------------------------------------
# Must be sourced, not executed
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
echo "Dette scriptet må kjøres med:"
echo " source ps1.sh"
echo "eller:"
echo " . ps1.sh"
exit 1
fi
# Only for interactive shells
case "$-" in
*i*) ;;
*) return 0 ;;
esac
# -------------------------------------------------
# Emoji based on time (Norway / Europe/Oslo)
# Ranges:
# 05:00-08:59 tidlig
# 09:00-10:59 formiddag
# 11:00-11:29 lunsj
# 11:30-15:59 dag
# 16:00-16:59 middag
# 17:00-22:59 kveld
# 23:00-04:59 natt
# -------------------------------------------------
_ps1_emoji() {
local hh mm
hh="$(TZ=Europe/Oslo date +%H)"
mm="$(TZ=Europe/Oslo date +%M)"
# Strip leading zeros safely
local h m
h=$((10#$hh))
m=$((10#$mm))
if (( h >= 5 && h <= 8 )); then
printf '%s' "🌅" # tidlig
elif (( h >= 9 && h <= 10 )); then
printf '%s' "☕" # formiddag
elif (( h == 11 && m < 30 )); then
printf '%s' "🥪" # lunsj (11:00-11:29)
elif (( (h == 11 && m >= 30) || (h >= 12 && h <= 15) )); then
printf '%s' "💻" # dag
elif (( h == 16 )); then
printf '%s' "🍲" # middag
elif (( h >= 17 && h <= 22 )); then
printf '%s' "🌆" # kveld
else
printf '%s' "🌙" # natt (23-04)
fi
}
# -------------------------------------------------
# Build PS1 (boxy + pastel contrast + full path)
# Uses $(pwd) so you see /home/user instead of ~
# -------------------------------------------------
_ps1_build() {
local emoji path
emoji="$(_ps1_emoji)"
path="$(pwd)"
# Pastel-ish 256 colors:
# - box: 183 (light purple)
# - value: 117 (light cyan)
# - accent 229 (light yellow)
local BOX="\[\e[38;5;183m\]"
local VAL="\[\e[38;5;117m\]"
local ACC="\[\e[38;5;229m\]"
local RST="\[\e[0m\]"
# Layout:
# ╭─(date time)-(user@host)-(fullpath)
# ╰──➜ $ + emoji
printf '%b' \
"${BOX}╭─(${ACC}\d${BOX} ${ACC}\A${BOX})-(${VAL}\u${BOX}@${VAL}\h${BOX})-(${VAL}${path}${BOX})${RST}\n${BOX}╰──➜ ${RST}\\$${emoji} "
}
# -------------------------------------------------
# PROMPT_COMMAND handling
# -------------------------------------------------
__PS1_PREV_PROMPT_COMMAND="${PROMPT_COMMAND-}"
ps1_on() {
PROMPT_COMMAND='_PS1="$(_ps1_build)"; export PS1="$_PS1"'
_PS1="$(_ps1_build)"; export PS1="$_PS1"
}
ps1_off() {
PROMPT_COMMAND="$__PS1_PREV_PROMPT_COMMAND"
}
# Optional: turn it on immediately when sourced (comment out if you don't want auto-enable)
# ps1_on