Unify global installer with shared PS1
- Use dotfiles/ps1/ps1.sh for global installs - Force NF caps and bold prompt arrow in shared PS1
This commit is contained in:
@@ -6,148 +6,19 @@ if [[ "${EUID}" -ne 0 ]]; then
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
|
||||||
|
REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
|
||||||
|
|
||||||
|
SRC_PS1="$REPO_ROOT/ps1/ps1.sh"
|
||||||
PS1_FILE="/etc/profile.d/ps1.sh"
|
PS1_FILE="/etc/profile.d/ps1.sh"
|
||||||
BASH_BASHRC="/etc/bash.bashrc"
|
BASH_BASHRC="/etc/bash.bashrc"
|
||||||
|
|
||||||
MARKER_START="# >>> global ps1 (managed) >>>"
|
MARKER_START="# >>> global ps1 (managed) >>>"
|
||||||
MARKER_END="# <<< global ps1 (managed) <<<"
|
MARKER_END="# <<< global ps1 (managed) <<<"
|
||||||
|
|
||||||
echo "Skriver global PS1 til: $PS1_FILE"
|
echo "Installerer global PS1: $SRC_PS1 -> $PS1_FILE"
|
||||||
|
|
||||||
# Write the PS1 module (global)
|
|
||||||
cat > "$PS1_FILE" <<'EOF'
|
|
||||||
#!/usr/bin/env bash
|
|
||||||
|
|
||||||
# This file is loaded globally. It may be sourced from /etc/bash.bashrc.
|
|
||||||
# It must not exit the shell; only return when not interactive.
|
|
||||||
|
|
||||||
case "$-" in
|
|
||||||
*i*) ;;
|
|
||||||
*) return 0 ;;
|
|
||||||
esac
|
|
||||||
|
|
||||||
# -------------------------------------------------
|
|
||||||
# Nerd Font detection + fallback controls
|
|
||||||
# Env overrides:
|
|
||||||
# PS1_FORCE_ASCII=1 -> always fallback separators
|
|
||||||
# PS1_FORCE_NF=1 -> always use 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
|
|
||||||
|
|
||||||
if command -v fc-list >/dev/null 2>&1; then
|
|
||||||
if fc-list 2>/dev/null | grep -qi "Nerd Font"; then
|
|
||||||
return 0
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
|
|
||||||
# Force NF separators globally (rounded caps) even when detection fails.
|
|
||||||
export PS1_FORCE_NF=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 visible)
|
|
||||||
# bash 3.2+ compatible (no negative array indexes)
|
|
||||||
# -------------------------------------------------
|
|
||||||
_ps1_path() {
|
|
||||||
local p="$PWD"
|
|
||||||
local parts=()
|
|
||||||
IFS='/' read -ra parts <<< "$p"
|
|
||||||
|
|
||||||
if ((${#parts[@]} < 6)); then
|
|
||||||
echo "$p"
|
|
||||||
return
|
|
||||||
fi
|
|
||||||
|
|
||||||
local n=${#parts[@]}
|
|
||||||
local a="${parts[1]}"
|
|
||||||
local b="${parts[2]}"
|
|
||||||
local c="${parts[$((n-2))]}"
|
|
||||||
local d="${parts[$((n-1))]}"
|
|
||||||
|
|
||||||
echo "/${a}/${b}/…/${c}/${d}"
|
|
||||||
}
|
|
||||||
|
|
||||||
# -------------------------------------------------
|
|
||||||
# Dynamic vars updated before each prompt
|
|
||||||
# -------------------------------------------------
|
|
||||||
__PS1_SYM=""
|
|
||||||
__PS1_PATH=""
|
|
||||||
__PS1_STATUS=0
|
|
||||||
__PS1_USE_NF=0
|
|
||||||
|
|
||||||
_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
|
|
||||||
}
|
|
||||||
|
|
||||||
# -------------------------------------------------
|
|
||||||
# Public functions
|
|
||||||
# -------------------------------------------------
|
|
||||||
ps1_on() {
|
|
||||||
local RST="\[\e[0m\]"
|
|
||||||
|
|
||||||
# Zone 1 (date/time/user): gray-blue pastel
|
|
||||||
local Z1_BG="\[\e[48;5;61m\]"
|
|
||||||
local Z1_FG="\[\e[38;5;255m\]"
|
|
||||||
|
|
||||||
# Zone 2 (host/path): turquoise / cool green
|
|
||||||
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 (NF)
|
|
||||||
local LEFT=""
|
|
||||||
local SEP=""
|
|
||||||
local RIGHT=""
|
|
||||||
|
|
||||||
PS1="\
|
|
||||||
${FRAME}╭─${RST}\
|
|
||||||
${Z1_BG}${Z1_FG}${LEFT} \d \A \u ${RST}\
|
|
||||||
${Z1_BG}${Z2_BG}${Z2_FG}${SEP}${RST}\
|
|
||||||
${Z2_BG}${Z2_FG} @\h ${PATH_FG}\w ${RST}${Z2_BG}${Z2_FG}${RIGHT}${RST}\
|
|
||||||
\n${FRAME}╰── ${RST}\
|
|
||||||
\$( [ \$? -eq 0 ] && printf '${OK}' || printf '${BAD}' )➜${RST} 🌙 "
|
|
||||||
}
|
|
||||||
|
|
||||||
ps1_off() {
|
|
||||||
# no-op helper; users can override if needed
|
|
||||||
:
|
|
||||||
}
|
|
||||||
|
|
||||||
# Enable by default (global standard)
|
|
||||||
ps1_on
|
|
||||||
EOF
|
|
||||||
|
|
||||||
|
cp -f "$SRC_PS1" "$PS1_FILE"
|
||||||
chmod 0644 "$PS1_FILE"
|
chmod 0644 "$PS1_FILE"
|
||||||
|
|
||||||
echo "Oppdaterer $BASH_BASHRC for å source $PS1_FILE (idempotent)"
|
echo "Oppdaterer $BASH_BASHRC for å source $PS1_FILE (idempotent)"
|
||||||
@@ -162,7 +33,7 @@ awk -v s="$MARKER_START" -v e="$MARKER_END" '
|
|||||||
cat "$tmp" > "$BASH_BASHRC"
|
cat "$tmp" > "$BASH_BASHRC"
|
||||||
rm -f "$tmp"
|
rm -f "$tmp"
|
||||||
|
|
||||||
cat >> "$BASH_BASHRC" <<EOF
|
cat >> "$BASH_BASHRC" <<EOF2
|
||||||
|
|
||||||
$MARKER_START
|
$MARKER_START
|
||||||
# Load global PS1 (managed)
|
# Load global PS1 (managed)
|
||||||
@@ -170,7 +41,7 @@ if [ -r "$PS1_FILE" ]; then
|
|||||||
. "$PS1_FILE"
|
. "$PS1_FILE"
|
||||||
fi
|
fi
|
||||||
$MARKER_END
|
$MARKER_END
|
||||||
EOF
|
EOF2
|
||||||
|
|
||||||
echo "✅ Ferdig."
|
echo "✅ Ferdig."
|
||||||
echo "Logg ut/inn (eller åpne ny shell) for å se global PS1."
|
echo "Logg ut/inn (eller åpne ny shell) for å se global PS1."
|
||||||
|
|||||||
@@ -18,6 +18,9 @@ if [[ -f "$HOME/.config/ps1/disable" ]]; then
|
|||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Force NF separators globally (rounded caps) unless ASCII is forced.
|
||||||
|
export PS1_FORCE_NF=1
|
||||||
|
|
||||||
# Nerd Font detection (best-effort) + overrides
|
# Nerd Font detection (best-effort) + overrides
|
||||||
# PS1_FORCE_ASCII=1 -> always fallback separators
|
# PS1_FORCE_ASCII=1 -> always fallback separators
|
||||||
# PS1_FORCE_NF=1 -> always Nerd Font separators
|
# PS1_FORCE_NF=1 -> always Nerd Font separators
|
||||||
@@ -90,6 +93,8 @@ _ps1_set_prompt() {
|
|||||||
# Status colors
|
# Status colors
|
||||||
local OK="\[\e[38;5;76m\]"
|
local OK="\[\e[38;5;76m\]"
|
||||||
local BAD="\[\e[38;5;203m\]"
|
local BAD="\[\e[38;5;203m\]"
|
||||||
|
local BOLD="\[\e[1m\]"
|
||||||
|
local NOBOLD="\[\e[22m\]"
|
||||||
|
|
||||||
local left right sep
|
local left right sep
|
||||||
if [[ "$__PS1_USE_NF" -eq 1 ]]; then
|
if [[ "$__PS1_USE_NF" -eq 1 ]]; then
|
||||||
@@ -100,16 +105,16 @@ _ps1_set_prompt() {
|
|||||||
|
|
||||||
local prompt_sym
|
local prompt_sym
|
||||||
if [[ "$__PS1_STATUS" -eq 0 ]]; then
|
if [[ "$__PS1_STATUS" -eq 0 ]]; then
|
||||||
prompt_sym="${OK}➜${RST}"
|
prompt_sym="${OK}${BOLD}➜${NOBOLD}${RST}"
|
||||||
else
|
else
|
||||||
prompt_sym="${BAD}➜${RST}"
|
prompt_sym="${BAD}${BOLD}➜${NOBOLD}${RST}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Keep order: date time user | host path, newline, then prompt
|
# Keep order: date time user | host path, newline, then prompt
|
||||||
PS1="\
|
PS1="\
|
||||||
${FRAME}╭─${RST}\
|
${FRAME}╭─${RST}\
|
||||||
${Z1_BG}${Z1_FG}${left} \\d \\A \\u ${RST}${Z1_BG}${Z2_BG}${Z2_FG}${sep}${RST}\
|
${Z1_BG}${Z1_FG}${left} \\d \\A \\u ${RST}${Z1_BG}${Z2_BG}${Z2_FG}${sep}${RST}\
|
||||||
${Z2_BG}${Z2_FG} @\\h ${PATH_FG}${__PS1_PATH} ${RST}${Z2_BG}${Z2_FG}${right}${RST}\
|
${Z2_BG}${Z2_FG} @\\h ${PATH_FG}${__PS1_PATH}${RST}${Z2_BG}${Z2_FG}${right}${RST}\
|
||||||
\n${FRAME}╰── ${RST}${prompt_sym} ${__PS1_SYM} "
|
\n${FRAME}╰── ${RST}${prompt_sym} ${__PS1_SYM} "
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user