This commit is contained in:
2026-01-24 04:41:56 +00:00
parent 9e808d8981
commit d590a82645
10 changed files with 416 additions and 287 deletions

View File

@@ -0,0 +1,179 @@
#!/usr/bin/env bash
set -euo pipefail
if [[ "${EUID}" -ne 0 ]]; then
echo "Kjør som root: sudo bash $0"
exit 1
fi
PS1_FILE="/etc/profile.d/ps1.sh"
BASH_BASHRC="/etc/bash.bashrc"
MARKER_START="# >>> global ps1 (managed) >>>"
MARKER_END="# <<< global ps1 (managed) <<<"
echo "Skriver global PS1 til: $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
}
# -------------------------------------------------
# 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() {
PROMPT_COMMAND="_ps1_update"
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 w/ fallback (NF vs non-NF)
local SEP_EXPR='\$( [ "$__PS1_USE_NF" -eq 1 ] && printf "" || printf "▶" )'
local LEFT_EXPR='\$( [ "$__PS1_USE_NF" -eq 1 ] && printf "" || printf "[" )'
local RIGHT_EXPR='\$( [ "$__PS1_USE_NF" -eq 1 ] && printf "" || printf "]" )'
local PROMPT_SYM="\$( [ \$__PS1_STATUS -eq 0 ] && printf '${OK}' || printf '${BAD}' )➜${RST}"
PS1="\
${FRAME}╭─${RST}\
${Z1_BG}${Z1_FG}${LEFT_EXPR} \d \A \u ${RST}${Z1_BG}${Z2_BG}${Z2_FG}${SEP_EXPR}${RST}\
${Z2_BG}${Z2_FG} @\h ${PATH_FG}\${__PS1_PATH} ${RST}${Z2_BG}${Z2_FG}${RIGHT_EXPR}${RST}\
\n${FRAME}╰── ${RST}${PROMPT_SYM} \${__PS1_SYM} "
}
ps1_off() {
# no-op helper; users can override if needed
:
}
# Enable by default (global standard)
ps1_on
EOF
chmod 0644 "$PS1_FILE"
echo "Oppdaterer $BASH_BASHRC for å source $PS1_FILE (idempotent)"
# Remove old managed block if present, then add
tmp="$(mktemp)"
awk -v s="$MARKER_START" -v e="$MARKER_END" '
$0==s {inside=1; next}
$0==e {inside=0; next}
!inside {print}
' "$BASH_BASHRC" > "$tmp"
cat "$tmp" > "$BASH_BASHRC"
rm -f "$tmp"
cat >> "$BASH_BASHRC" <<EOF
$MARKER_START
# Load global PS1 (managed)
if [ -r "$PS1_FILE" ]; then
. "$PS1_FILE"
fi
$MARKER_END
EOF
echo "✅ Ferdig."
echo "Logg ut/inn (eller åpne ny shell) for å se global PS1."
echo
echo "Tips:"
echo " - Tving NerdFont glyphs: export PS1_FORCE_NF=1"
echo " - Tving fallback: export PS1_FORCE_ASCII=1"

View File

@@ -0,0 +1,53 @@
#!/usr/bin/env bash
set -euo pipefail
if [[ "${EUID}" -ne 0 ]]; then
echo "Kjør som root: sudo bash $0"
exit 1
fi
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
SRC_PS1="$REPO_ROOT/ps1/ps1.sh"
DST_PS1="/etc/profile.d/ps1.sh"
BASH_BASHRC="/etc/bash.bashrc"
MARKER_START="# >>> global ps1 (managed) >>>"
MARKER_END="# <<< global ps1 (managed) <<<"
if [[ ! -r "$SRC_PS1" ]]; then
echo "Fant ikke $SRC_PS1"
exit 1
fi
cp -f "$SRC_PS1" "$DST_PS1"
chmod 0644 "$DST_PS1"
# Ensure /etc/bash.bashrc sources it (idempotent)
tmp="$(mktemp)"
awk -v s="$MARKER_START" -v e="$MARKER_END" '
$0==s {inside=1; next}
$0==e {inside=0; next}
!inside {print}
' "$BASH_BASHRC" > "$tmp"
cat "$tmp" > "$BASH_BASHRC"
rm -f "$tmp"
cat >> "$BASH_BASHRC" <<EOF
$MARKER_START
# Load global PS1 (managed)
if [ -r "$DST_PS1" ]; then
. "$DST_PS1"
fi
$MARKER_END
EOF
echo "✅ Linux/WSL global PS1 installert:"
echo " $DST_PS1"
echo " og sourcet fra $BASH_BASHRC"
echo
echo "Disable per bruker:"
echo " export DISABLE_GLOBAL_PS1=1"
echo " eller touch ~/.config/ps1/disable"

View File

@@ -0,0 +1,55 @@
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
SRC_PS1="$REPO_ROOT/ps1/ps1.sh"
DST_DIR="$HOME/.config/ps1"
DST_PS1="$DST_DIR/ps1.sh"
MARKER_START="# >>> user ps1 (managed) >>>"
MARKER_END="# <<< user ps1 (managed) <<<"
mkdir -p "$DST_DIR"
cp -f "$SRC_PS1" "$DST_PS1"
chmod 0644 "$DST_PS1"
add_source_block() {
local file="$1"
[[ -f "$file" ]] || touch "$file"
# remove old block
local tmp
tmp="$(mktemp)"
awk -v s="$MARKER_START" -v e="$MARKER_END" '
$0==s {inside=1; next}
$0==e {inside=0; next}
!inside {print}
' "$file" > "$tmp"
cat "$tmp" > "$file"
rm -f "$tmp"
cat >> "$file" <<EOF
$MARKER_START
# Load user PS1 (managed)
if [ -r "$DST_PS1" ]; then
. "$DST_PS1"
fi
$MARKER_END
EOF
}
# Bash on macOS may read .bash_profile for login shells; interactive shells read .bashrc
add_source_block "$HOME/.bashrc"
add_source_block "$HOME/.bash_profile"
echo "✅ macOS user PS1 installert:"
echo " $DST_PS1"
echo " sourcet fra ~/.bashrc og ~/.bash_profile"
echo
echo "Tips:"
echo " - Tving NerdFont glyphs: export PS1_FORCE_NF=1"
echo " - Tving fallback: export PS1_FORCE_ASCII=1"
echo " - Disable: export DISABLE_GLOBAL_PS1=1 eller touch ~/.config/ps1/disable"

View File

@@ -0,0 +1,50 @@
#!/usr/bin/env bash
set -euo pipefail
FONT_ZIP_URL="https://github.com/ryanoasis/nerd-fonts/releases/latest/download/JetBrainsMono.zip"
is_macos() { [[ "$(uname -s)" == "Darwin" ]]; }
echo "🔎 Installerer JetBrainsMono Nerd Font (best effort)"
if is_macos; then
# Try Homebrew first (best UX on mac)
if command -v brew >/dev/null 2>&1; then
echo "🍺 Homebrew funnet prøver cask..."
brew tap homebrew/cask-fonts >/dev/null 2>&1 || true
if brew install --cask font-jetbrains-mono-nerd-font; then
echo "✅ Installert via Homebrew cask."
echo "Velg fonten i terminal: JetBrainsMono Nerd Font"
exit 0
fi
echo "⚠️ Cask feilet, faller tilbake til manuell install."
fi
FONT_DIR="$HOME/Library/Fonts"
mkdir -p "$FONT_DIR"
tmpzip="$(mktemp -t jbmono.XXXXXX.zip)"
curl -fsSL "$FONT_ZIP_URL" -o "$tmpzip"
tmpdir="$(mktemp -d -t jbmono.XXXXXX)"
unzip -o "$tmpzip" -d "$tmpdir" >/dev/null
rm -f "$tmpzip"
# Copy only TTF/OTF
find "$tmpdir" -type f \( -iname "*.ttf" -o -iname "*.otf" \) -maxdepth 2 -print0 \
| xargs -0 -I{} cp -f "{}" "$FONT_DIR"/
rm -rf "$tmpdir"
echo "✅ Font kopiert til $FONT_DIR"
echo "Velg fonten i terminal: JetBrainsMono Nerd Font"
exit 0
fi
# Linux
FONT_DIR="$HOME/.local/share/fonts"
mkdir -p "$FONT_DIR"
tmpzip="$(mktemp --suffix=.zip)"
curl -fsSL "$FONT_ZIP_URL" -o "$tmpzip"
unzip -o "$tmpzip" -d "$FONT_DIR" >/dev/null
rm -f "$tmpzip"
fc-cache -f >/dev/null 2>&1 || true
echo "✅ Font installert i $FONT_DIR"
echo "Velg fonten i terminal: JetBrainsMono Nerd Font"