nanuqsaurus/scripts/shell-init.sh

145 lines
4 KiB
Bash
Raw Normal View History

# ---- logging ---------------------------------------------------------------
2026-02-02 22:07:05 +02:00
log_shell_init() {
# Disabled by default to avoid noisy permission errors in login shells.
# Enable with:
# export NANUQSAURUS_SHELL_INIT_LOG=1
# Optionally override the file path:
# export NANUQSAURUS_SHELL_INIT_LOG_FILE="$HOME/.local/state/shell-init.log"
[[ -n "${NANUQSAURUS_SHELL_INIT_LOG:-}" ]] || return 0
local uid="${UID:-}"
[[ -n "$uid" ]] || uid="$(id -u 2>/dev/null || echo unknown)"
local log_file="${NANUQSAURUS_SHELL_INIT_LOG_FILE:-${XDG_STATE_HOME:-$HOME/.local/state}/nanuqsaurus/shell-init-${uid}.log}"
local log_dir="${log_file%/*}"
mkdir -p "$log_dir" 2>/dev/null || return 0
# Redirect stderr first so a failing stdout redirection stays silent.
printf '%s\n' "shell-init: $*" 2>/dev/null >>"$log_file" || true
2026-02-02 22:07:05 +02:00
}
# ---- helpers ---------------------------------------------------------------
have() { command -v "$1" >/dev/null 2>&1; }
is_interactive() {
[[ -t 1 || -n ${PS1:-} ]]
2026-02-02 22:07:05 +02:00
}
append_unique() {
local var="$1" val="$2"
[[ ";${!var:-};" == *";$val;"* ]] && return
printf -v "$var" '%s' "${!var:+${!var%;};}$val"
2026-02-02 22:07:05 +02:00
}
prepend_unique_array() {
local name="$1" val="$2"
local -n arr="$name"
local out=("$val")
for f in "${arr[@]}"; do [[ $f != "$val" ]] && out+=("$f"); done
arr=("${out[@]}")
}
# ---- environment sanity ----------------------------------------------------
2026-02-02 22:07:05 +02:00
if [[ -z ${TERM:-} || $TERM == dumb ]]; then
2026-02-02 22:07:05 +02:00
export TERM=xterm-256color
fi
INTERACTIVE=0
is_interactive && INTERACTIVE=1
2026-02-02 22:07:05 +02:00
log_shell_init "TERM=$TERM PS1=${PS1:-<empty>} SHELL=$SHELL interactive=$INTERACTIVE"
# Ensure PS1 exists so downstream init blocks run
if (( INTERACTIVE )) && [[ -z ${PS1:-} ]]; then
2026-02-02 22:07:05 +02:00
PS1='\\s-\\v\\$ '
fi
(( INTERACTIVE )) || return 0
# ---- bash-preexec ----------------------------------------------------------
if [[ -n ${BASH_VERSION:-} && -z ${bash_preexec_imported:-} && -r "@BASHPREEXEC@" ]]; then
# shellcheck disable=SC1090
. "@BASHPREEXEC@"
have __bp_install && __bp_install
log_shell_init "bash-preexec loaded"
2026-02-02 22:07:05 +02:00
fi
# ---- starship --------------------------------------------------------------
if have @STARSHIP@; then
2026-02-02 22:07:05 +02:00
log_shell_init "starship init"
case $SHELL in
2026-02-02 22:07:05 +02:00
*/bash) eval "$(@STARSHIP@ init bash)" ;;
*/zsh) eval "$(@STARSHIP@ init zsh)" ;;
esac
append_unique PROMPT_COMMAND starship_precmd
2026-02-02 22:07:05 +02:00
else
log_shell_init "starship skipped"
2026-02-02 22:07:05 +02:00
fi
# ---- atuin -----------------------------------------------------------------
if have @ATUIN@; then
log_shell_init "atuin init"
if [[ -z ${ATUIN_SESSION:-} ]]; then
export ATUIN_SESSION="$(@ATUIN@ uuid)"
2026-02-02 22:07:05 +02:00
ATUIN_HISTORY_ID=""
fi
case $SHELL in
2026-02-02 22:07:05 +02:00
*/bash) eval "$(@ATUIN@ init bash)" ;;
*/zsh) eval "$(@ATUIN@ init zsh)" ;;
esac
declare -a preexec_functions precmd_functions
prepend_unique_array preexec_functions __atuin_preexec
prepend_unique_array precmd_functions __atuin_precmd
2026-02-02 22:07:05 +02:00
else
log_shell_init "atuin skipped"
fi
# ---- zoxide ---------------------------------------------------------------
if have zoxide; then
2026-02-02 22:07:05 +02:00
log_shell_init "zoxide init"
case $SHELL in
2026-02-02 22:07:05 +02:00
*/bash) eval "$(zoxide init bash)" ;;
*/zsh) eval "$(zoxide init zsh)" ;;
esac
have __zoxide_hook && append_unique PROMPT_COMMAND __zoxide_hook
2026-02-02 22:07:05 +02:00
else
log_shell_init "zoxide skipped"
fi
# ---- bash-preexec PROMPT_COMMAND safety -----------------------------------
2026-02-02 22:07:05 +02:00
have __bp_precmd_invoke_cmd && append_unique PROMPT_COMMAND __bp_precmd_invoke_cmd
have __bp_interactive_mode && append_unique PROMPT_COMMAND __bp_interactive_mode
2026-02-02 22:07:05 +02:00
# ---- aliases --------------------------------------------------------------
if have eza; then
2026-02-02 22:07:05 +02:00
alias ls='eza'
alias ll='eza -l --icons=auto --group-directories-first'
2026-02-02 22:07:05 +02:00
alias l1='eza -1'
alias l.='eza -d .*'
2026-02-02 22:07:05 +02:00
fi
if have ug; then
2026-02-02 22:07:05 +02:00
alias grep='ug'
alias egrep='ug -E'
alias fgrep='ug -F'
alias xzgrep='ug -z'
alias xzegrep='ug -zE'
alias xzfgrep='ug -zF'
fi
if have bat; then
2026-02-02 22:07:05 +02:00
alias cat='bat --style=plain --pager=never' 2>/dev/null
fi