144 lines
4 KiB
Bash
144 lines
4 KiB
Bash
# ---- logging ---------------------------------------------------------------
|
|
|
|
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
|
|
}
|
|
|
|
# ---- helpers ---------------------------------------------------------------
|
|
|
|
have() { command -v "$1" >/dev/null 2>&1; }
|
|
|
|
is_interactive() {
|
|
[[ -t 1 || -n ${PS1:-} ]]
|
|
}
|
|
|
|
append_unique() {
|
|
local var="$1" val="$2"
|
|
[[ ";${!var:-};" == *";$val;"* ]] && return
|
|
printf -v "$var" '%s' "${!var:+${!var%;};}$val"
|
|
}
|
|
|
|
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 ----------------------------------------------------
|
|
|
|
if [[ -z ${TERM:-} || $TERM == dumb ]]; then
|
|
export TERM=xterm-256color
|
|
fi
|
|
|
|
INTERACTIVE=0
|
|
is_interactive && INTERACTIVE=1
|
|
|
|
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
|
|
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"
|
|
fi
|
|
|
|
# ---- starship --------------------------------------------------------------
|
|
|
|
if have @STARSHIP@; then
|
|
log_shell_init "starship init"
|
|
case $SHELL in
|
|
*/bash) eval "$(@STARSHIP@ init bash)" ;;
|
|
*/zsh) eval "$(@STARSHIP@ init zsh)" ;;
|
|
esac
|
|
append_unique PROMPT_COMMAND starship_precmd
|
|
else
|
|
log_shell_init "starship skipped"
|
|
fi
|
|
|
|
# ---- atuin -----------------------------------------------------------------
|
|
|
|
if have @ATUIN@; then
|
|
log_shell_init "atuin init"
|
|
|
|
if [[ -z ${ATUIN_SESSION:-} ]]; then
|
|
export ATUIN_SESSION="$(@ATUIN@ uuid)"
|
|
ATUIN_HISTORY_ID=""
|
|
fi
|
|
|
|
case $SHELL in
|
|
*/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
|
|
else
|
|
log_shell_init "atuin skipped"
|
|
fi
|
|
|
|
# ---- zoxide ---------------------------------------------------------------
|
|
|
|
if have zoxide; then
|
|
log_shell_init "zoxide init"
|
|
case $SHELL in
|
|
*/bash) eval "$(zoxide init bash)" ;;
|
|
*/zsh) eval "$(zoxide init zsh)" ;;
|
|
esac
|
|
have __zoxide_hook && append_unique PROMPT_COMMAND __zoxide_hook
|
|
else
|
|
log_shell_init "zoxide skipped"
|
|
fi
|
|
|
|
# ---- bash-preexec PROMPT_COMMAND safety -----------------------------------
|
|
|
|
have __bp_precmd_invoke_cmd && append_unique PROMPT_COMMAND __bp_precmd_invoke_cmd
|
|
have __bp_interactive_mode && append_unique PROMPT_COMMAND __bp_interactive_mode
|
|
|
|
# ---- aliases --------------------------------------------------------------
|
|
|
|
if have eza; then
|
|
alias ls='eza'
|
|
alias ll='eza -l --icons=auto --group-directories-first'
|
|
alias l1='eza -1'
|
|
alias l.='eza -d .*'
|
|
fi
|
|
|
|
if have ug; then
|
|
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
|
|
alias cat='bat --style=plain --pager=never' 2>/dev/null
|
|
fi
|