sourcing flow improved, faster startups yeaaa

This commit is contained in:
tosu 2024-06-23 12:34:39 +02:00
parent f77a77b034
commit d33265970a
2 changed files with 246 additions and 229 deletions

26
.bashrc
View File

@ -2,6 +2,7 @@
# ex: set ts=4 sw=4 ft=sh # ex: set ts=4 sw=4 ft=sh
# TODO: Test on solaris (and other unices). Many tools will fail since we're not considering /usr/xpg4/bin # TODO: Test on solaris (and other unices). Many tools will fail since we're not considering /usr/xpg4/bin
# TODO: Make more general purpose, currently quite bashy
# Exit when noninteractive. This is more portable than checking PS1. # Exit when noninteractive. This is more portable than checking PS1.
[ "${-#*i}" = "${-}" ] && return [ "${-#*i}" = "${-}" ] && return
@ -12,29 +13,9 @@ _have_all () { while [ "${#}" -gt "0" ] ; do [ -x "$(_path_lookup "${1-}")" ] ||
_have () { _have_all "${1-}" ; } _have () { _have_all "${1-}" ; }
_source_if () { [ -f "${1-}" ] && [ -r "${1-}" ] && . "${1-}" ; } _source_if () { [ -f "${1-}" ] && [ -r "${1-}" ] && . "${1-}" ; }
# TODO: Create ~/.local/bin directory and add ~/.local/bin to PATH
# Add some basic scripts, like log.log, etc.
############################ logging utils (posix) #############################
log.log () {
case "${1-}" in
red) __ansi='41;30' ;;
orange) __ansi='43;30' ;;
blue) __ansi='44;30' ;;
green) __ansi='42;30' ;;
*) __ansi='45;30' ;;
esac
shift
1>&2 printf "\033[${__ansi}m%s\033[m\n" "${*}"
unset __ansi
}
log.err () { log.log red "${@}" ; }
log.warn () { log.log orange "${@}" ; }
log.info () { log.log blue "${@}" ; }
log.good () { log.log green "${@}" ; }
###################### exit when already sourced (posix) ####################### ###################### exit when already sourced (posix) #######################
[ -n "${BASHRC_SOURCED}" ] && { log.err ".bashrc already sourced. Reset shell with 'exec bash [-l]' or start a new terminal." ; return 1 ; } [ -n "${BASHRC_SOURCED}" ] && { log.err ".bashrc already sourced. Reset shell with 'exec bash [-l]' or start a new terminal." ; return 1 ; }
BASHRC_SOURCED='1' BASHRC_SOURCED='1' # don't use export here, otherwise you can't start shells within shells
################################## bash reset ################################## ################################## bash reset ##################################
# Set IFS to the default value, <space><tab><newline> # Set IFS to the default value, <space><tab><newline>
@ -55,7 +36,10 @@ unalias -a
unset -v POSIXLY_CORRECT unset -v POSIXLY_CORRECT
####################### per session environment (posix) ######################## ####################### per session environment (posix) ########################
set | grep -sq '^\(TMUX_PANE\|SSH_CONNECTION\)' && exec 2>/dev/null
[ -n "${DISPLAY-}" ] || log.warn 'DISPLAY not set' [ -n "${DISPLAY-}" ] || log.warn 'DISPLAY not set'
set | grep -sq '^\(TMUX_PANE\|SSH_CONNECTION\)' && exec 2>/dev/tty
BAT_THEME='gruvbox-dark' # BAT_THEME='gruvbox-light' BAT_THEME='gruvbox-dark' # BAT_THEME='gruvbox-light'
GPG_TTY="$(tty)" GPG_TTY="$(tty)"

189
.profile
View File

@ -3,29 +3,53 @@
# TODO: Make more general purpose, currently quite bashy # TODO: Make more general purpose, currently quite bashy
############################## global environment ############################## # TODO: Create ~/.local/bin directory and add ~/.local/bin to PATH
if ! [ -n "${USER-}" ] ; then # Add some basic scripts, like log.log, etc.
if ! USER="$(2>/dev/null ps -o user= -p "${$}" | awk '{print $1}')" ; then ############################ logging utils (posix) #############################
if ! USER="$(2>/dev/null whoami)" ; then log.log () {
if ! USER="$(2>/dev/null id -u -n)"; then case "${1-}" in
if ! USER="$(basename -- "$({HOME=~ && printf %s "${HOME}")")" ; then red) __ansi='41;30' ;;
if ! USER="$(2>/dev/null logname)" ; then orange) __ansi='43;30' ;;
if USER="${LOGNAME-}" ; [ -z "${USER}" ] ; then blue) __ansi='44;30' ;;
unset USER green) __ansi='42;30' ;;
fi; fi; fi; fi; fi; fi; fi *) __ansi='45;30' ;;
esac
shift
1>&2 printf "\033[${__ansi}m%s\033[m\n" "${*}"
unset __ansi
}
log.err () { log.log red "${@}" ; }
log.warn () { log.log orange "${@}" ; }
log.info () { log.log blue "${@}" ; }
log.good () { log.log green "${@}" ; }
if ! [ -n "${HOME-}" ] ; then if [ -n "${PROFILE_SOURCED}" ] ; then
if ! HOME="$(getent passwd "$(id -u "${USER}")" | cut -d: -f6)" ; then ###################### exit when already sourced (posix) #######################
if ! HOME="$(getent passwd "${UID}" | cut -d: -f6)" ; then log.err ".profile already sourced. unset PROFILE_SOURCED to force reloading, or don't start a login session (e.g. leave the -l flag from bash)."
if ! HOME="$(awk -v FS=':' -v user="${USER}" '($1==user) {print $6}' "/etc/passwd")" ; then else
unset HOME ########################## global environment (posix) ##########################
HOME=~ if ! [ -n "${USER-}" ] ; then
if [ "${HOME}" = "~" ] ; then if ! USER="$(2>/dev/null ps -o user= -p "${$}" | awk '{print $1}')" ; then
if ! mkdir "/tmp/${USER}" && HOME="/tmp/${USER}" ; then if ! USER="$(2>/dev/null whoami)" ; then
unset HOME if ! USER="$(2>/dev/null id -u -n)"; then
fi; fi ; fi ; fi ; fi; fi if ! USER="$(basename -- "$({HOME=~ && printf %s "${HOME}")")" ; then
if ! USER="$(2>/dev/null logname)" ; then
if USER="${LOGNAME-}" ; [ -z "${USER}" ] ; then
unset USER
fi; fi; fi; fi; fi; fi; fi
export EDITOR="$({ type -P nvim || if ! [ -n "${HOME-}" ] ; then
if ! HOME="$(getent passwd "$(id -u "${USER}")" | cut -d: -f6)" ; then
if ! HOME="$(getent passwd "${UID}" | cut -d: -f6)" ; then
if ! HOME="$(awk -v FS=':' -v user="${USER}" '($1==user) {print $6}' "/etc/passwd")" ; then
unset HOME
HOME=~
if [ "${HOME}" = "~" ] ; then
if ! mkdir "/tmp/${USER}" && HOME="/tmp/${USER}" ; then
unset HOME
fi; fi ; fi ; fi ; fi; fi
export EDITOR="$({ type -P nvim ||
type -P vim || type -P vim ||
type -P vi || type -P vi ||
type -P nvi || type -P nvi ||
@ -33,42 +57,43 @@ export EDITOR="$({ type -P nvim ||
type -P nano || type -P nano ||
type -P ex || type -P ex ||
type -P ed ; } 2>/dev/null)" type -P ed ; } 2>/dev/null)"
export _JAVA_AWT_WM_NONREPARENTING="1" export _JAVA_AWT_WM_NONREPARENTING="1"
export GIT_SSH_COMMAND="ssh -oIdentitiesOnly=yes -F"${HOME}"/.ssh/config" export GIT_SSH_COMMAND="ssh -oIdentitiesOnly=yes -F"${HOME}"/.ssh/config"
export LANG="en_US.UTF-8" export LANG="en_US.UTF-8"
export VISUAL="${EDITOR}" export VISUAL="${EDITOR}"
export SUDO_EDITOR="${EDITOR}" export SUDO_EDITOR="${EDITOR}"
export XDG_RUNTIME_DIR="/run/user/$(id -u)" export XDG_RUNTIME_DIR="/run/user/$(id -u)"
export SSH_AUTH_SOCK="${XDG_RUNTIME_DIR}/ssh-agent.socket" export SSH_AUTH_SOCK="${XDG_RUNTIME_DIR}/ssh-agent.socket"
export GIT_CONFIG_GLOBAL="${HOME}/.gitconfig" export GIT_CONFIG_GLOBAL="${HOME}/.gitconfig"
export USER42="tischmid" export USER42="tischmid"
export EMAIL42="timo42@proton.me" export EMAIL42="timo42@proton.me"
export MAIL="timo42@proton.me" export MAIL="timo42@proton.me"
export GOPATH="${HOME}/go" export GOPATH="${HOME}/go"
MANPAGER="nvim +Man!" # MANPAGER="less -X" MANPAGER="nvim +Man!" # MANPAGER="less -X"
export MANPAGER export MANPAGER
export PATH export PATH
export LD_LIBRARY_PATH export LD_LIBRARY_PATH
pathvarprepend () { ####################### path append & prepend functions ########################
pathvarprepend () {
# prepending paths to pathvar denoted by the expansion of the PATHVAR parameter # prepending paths to pathvar denoted by the expansion of the PATHVAR parameter
# if it's already in the PATH, move it to the end # if it's already in the PATH, move it to the end
# POSIX compliant version # POSIX compliant version
test -n "${2}" || test -n "${2}" ||
{ echo "Usage: pathvarprepend PATHVAR PATH_TO_ADD [PATH_TO_ADD...]"; { log.info "Usage: pathvarprepend PATHVAR PATH_TO_ADD [PATH_TO_ADD...]";
echo "Example: pathvarprepend LD_LIBRARY_PATH '${HOME}/.local/lib' '/usr/local/lib'"; log.info "Example: pathvarprepend LD_LIBRARY_PATH '${HOME}/.local/lib' '/usr/local/lib'";
return 2; } return 2; }
pathvar="${1}" pathvar="${1}"
shift shift
case $pathvar in (*[!abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_]*|""|[0123456789]*) false;; esac || case $pathvar in (*[!abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_]*|""|[0123456789]*) false;; esac ||
{ echo 'Expanded pathvar is not a valid name/variable identifier'; return 3; } { log.err 'Expanded pathvar is not a valid name/variable identifier'; return 3; }
if [ "$pathvar" = "PATH" ]; then if [ "$pathvar" = "PATH" ]; then
test "${-#*r}" = "${-}" || test "${-#*r}" = "${-}" ||
{ echo 'Restricted shell, cannot change PATH'; return 4; } { log.err 'Restricted shell, cannot change PATH'; return 4; }
fi fi
path_prepend_error=0 path_prepend_error=0
@ -86,10 +111,10 @@ pathvarprepend () {
norm_path_to_add="${1}" norm_path_to_add="${1}"
test "${norm_path_to_add#*:}" = "${norm_path_to_add}" || test "${norm_path_to_add#*:}" = "${norm_path_to_add}" ||
{ echo 'Cannot add path with colon'; path_prepend_error="1"; continue; } { log.warn 'Cannot add path with colon'; path_prepend_error="1"; continue; }
test -d "${norm_path_to_add}" || test -d "${norm_path_to_add}" ||
{ echo "path_to_add ('${norm_path_to_add}') not a directory"; path_prepend_error="1"; continue; } { log.warn "path_to_add ('${norm_path_to_add}') not a directory"; path_prepend_error="1"; continue; }
norm_path=$(printf %s ":$(eval "printf %s "'"'"\$$pathvar"'"'):" | head -n 1 | sed 's|/\+|/|g; s/\/$//; s/:/::/g') # fence with colons, ensure one line, deduplicate slashes, trim trailing, duplicate colons norm_path=$(printf %s ":$(eval "printf %s "'"'"\$$pathvar"'"'):" | head -n 1 | sed 's|/\+|/|g; s/\/$//; s/:/::/g') # fence with colons, ensure one line, deduplicate slashes, trim trailing, duplicate colons
norm_path_to_add=$(printf %s "$norm_path_to_add" | head -n 1 | sed 's|/\+|/|g; s/\/$//') # ensure one line, deduplicate slashes, trim trailing norm_path_to_add=$(printf %s "$norm_path_to_add" | head -n 1 | sed 's|/\+|/|g; s/\/$//') # ensure one line, deduplicate slashes, trim trailing
@ -109,26 +134,26 @@ pathvarprepend () {
eval "${pathvar}=\$norm_path_to_add\${norm_path:+:\$norm_path}" # prepend with colon eval "${pathvar}=\$norm_path_to_add\${norm_path:+:\$norm_path}" # prepend with colon
done done
return "${path_prepend_error}" return "${path_prepend_error}"
} && export -f pathvarprepend } && export -f pathvarprepend
pathvarappend () { pathvarappend () {
# appending paths to pathvar denoted by the expansion of the PATHVAR parameter # appending paths to pathvar denoted by the expansion of the PATHVAR parameter
# if it's already in the PATH, move it to the end # if it's already in the PATH, move it to the end
# POSIX compliant version # POSIX compliant version
test -n "${2}" || test -n "${2}" ||
{ echo "Usage: pathappend PATHVAR PATH_TO_ADD [PATH_TO_ADD...]"; { log.info "Usage: pathappend PATHVAR PATH_TO_ADD [PATH_TO_ADD...]";
echo "Example: pathappend LD_LIBRARY_PATH '${HOME}/.local/lib' '/usr/local/lib'"; log.info "Example: pathappend LD_LIBRARY_PATH '${HOME}/.local/lib' '/usr/local/lib'";
return 2; } return 2; }
pathvar="${1}" pathvar="${1}"
case "${pathvar}" in (*[!abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_]*|""|[0123456789]*) false;; esac || case "${pathvar}" in (*[!abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_]*|""|[0123456789]*) false;; esac ||
{ echo 'Expanded pathvar is not a valid name/variable identifier'; return 3; } { log.err 'Expanded pathvar is not a valid name/variable identifier'; return 3; }
if [ "${pathvar}" = "PATH" ]; then if [ "${pathvar}" = "PATH" ]; then
test "${-#*r}" = "${-}" || test "${-#*r}" = "${-}" ||
{ echo 'Restricted shell, cannot change PATH'; return 4; } { log.err 'Restricted shell, cannot change PATH'; return 4; }
fi fi
path_append_error="0" path_append_error="0"
@ -137,10 +162,10 @@ pathvarappend () {
norm_path_to_add="${1}" norm_path_to_add="${1}"
test "${norm_path_to_add#*:}" = "${norm_path_to_add}" || test "${norm_path_to_add#*:}" = "${norm_path_to_add}" ||
{ echo 'Cannot add path with colon'; path_append_error="1"; continue; } { log.warn 'Cannot add path with colon'; path_append_error="1"; continue; }
test -d "${norm_path_to_add}" || test -d "${norm_path_to_add}" ||
{ echo "path_to_add ('${norm_path_to_add}') not a directory"; path_append_error="1"; continue; } { log.warn "path_to_add ('${norm_path_to_add}') not a directory"; path_append_error="1"; continue; }
norm_path="$(printf %s ":$(eval "printf %s "'"'"\$${pathvar}"'"'):" | head -n 1 | sed 's|/\+|/|g; s/\/$//; s/:/::/g')" # fence with colons, ensure one line, deduplicate slashes, trim trailing, duplicate colons norm_path="$(printf %s ":$(eval "printf %s "'"'"\$${pathvar}"'"'):" | head -n 1 | sed 's|/\+|/|g; s/\/$//; s/:/::/g')" # fence with colons, ensure one line, deduplicate slashes, trim trailing, duplicate colons
norm_path_to_add="$(printf %s "${norm_path_to_add}" | head -n 1 | sed 's|/\+|/|g; s/\/$//')" # ensure one line, deduplicate slashes, trim trailing norm_path_to_add="$(printf %s "${norm_path_to_add}" | head -n 1 | sed 's|/\+|/|g; s/\/$//')" # ensure one line, deduplicate slashes, trim trailing
@ -160,36 +185,37 @@ pathvarappend () {
eval "${pathvar}=\${norm_path:+\$norm_path:}\$norm_path_to_add" # append with colon eval "${pathvar}=\${norm_path:+\$norm_path:}\$norm_path_to_add" # append with colon
done done
return "${path_append_error}" return "${path_append_error}"
} && export -f pathvarappend } && export -f pathvarappend
path_append () { path_append () {
pathvarappend PATH "$@" pathvarappend PATH "$@"
} && export -f path_append } && export -f path_append
ld_lib_path_append () { ld_lib_path_append () {
pathvarappend LD_LIBRARY_PATH "$@" pathvarappend LD_LIBRARY_PATH "$@"
} && export -f ld_lib_path_append } && export -f ld_lib_path_append
cdpath_append () { cdpath_append () {
pathvarappend CDPATH "$@" pathvarappend CDPATH "$@"
} && export -f cdpath_append } && export -f cdpath_append
path_prepend () { path_prepend () {
pathvarprepend PATH "$@" pathvarprepend PATH "$@"
} && export -f path_prepend } && export -f path_prepend
ld_lib_path_prepend () { ld_lib_path_prepend () {
pathvarprepend LD_LIBRARY_PATH "$@" pathvarprepend LD_LIBRARY_PATH "$@"
} && export -f ld_lib_path_prepend } && export -f ld_lib_path_prepend
cdpath_prepend () { cdpath_prepend () {
pathvarprepend CDPATH "$@" pathvarprepend CDPATH "$@"
} && export -f cdpath_prepend } && export -f cdpath_prepend
# ld_lib_path_append \ # ld_lib_path_append \
# "${HOME}/.local/lib" # "${HOME}/.local/lib"
path_prepend \ set | grep -sq '^\(TMUX_PANE\|SSH_CONNECTION\)' && exec 2>/dev/null
path_prepend \
"/bin" \ "/bin" \
"/sbin" \ "/sbin" \
"/usr/bin" \ "/usr/bin" \
@ -200,24 +226,31 @@ path_prepend \
"/usr/local/games" \ "/usr/local/games" \
"/snap/bin" \ "/snap/bin" \
path_append \ path_append \
"${HOME}/bin" \ "${HOME}/bin" \
"${HOME}/.bin" \ "${HOME}/.bin" \
"${HOME}/.local/bin" \ "${HOME}/.local/bin" \
"${HOME}/.local/sbin" \ "${HOME}/.local/sbin" \
"${HOME}/.brew/bin" \ "${HOME}/.brew/bin" \
"${GOPATH}/bin" "${GOPATH}/bin"
set | grep -sq '^\(TMUX_PANE\|SSH_CONNECTION\)' && exec 2>/dev/tty
# add cargo bin to path
if [ -f "${HOME}/.cargo/env" ] && [ -r "${HOME}/.cargo/env" ]; then # add cargo bin to path
if [ -f "${HOME}/.cargo/env" ] && [ -r "${HOME}/.cargo/env" ]; then
. "${HOME}/.cargo/env" . "${HOME}/.cargo/env"
fi
export NVM_DIR="${HOME}/.nvm"
[ -s "${NVM_DIR}/nvm.sh" ] && . "${NVM_DIR}/nvm.sh"
[ -s "${NVM_DIR}/bash_completion" ] && . "${NVM_DIR}/bash_completion"
if ! set | grep -sq '^\(TMUX_PANE\|SSH_CONNECTION\)' ; then
pidof -q startx && 1>/home/tosuman/.startx.log 2>&1 startx || log.warn "Not starting X again"
fi
log.info ".profile sourced"
export PROFILE_SOURCED='1'
fi fi
export NVM_DIR="${HOME}/.nvm"
[ -s "${NVM_DIR}/nvm.sh" ] && . "${NVM_DIR}/nvm.sh"
[ -s "${NVM_DIR}/bash_completion" ] && . "${NVM_DIR}/bash_completion"
1>/dev/null pidof -q startx && 1>/home/tosuman/.startx.log 2>&1 startx
# if running bash # if running bash
[ -n "${BASH_VERSINFO}" ] && if [ -f "${HOME}/.bashrc" ] && [ -r "${HOME}/.bashrc" ] && . "${HOME}/.bashrc" [ -n "${BASH_VERSINFO}" ] && [ -f "${HOME}/.bashrc" ] && [ -r "${HOME}/.bashrc" ] && . "${HOME}/.bashrc"