diff --git a/.config/shell/profile b/.config/shell/profile index 5084b6d..8f834b6 100644 --- a/.config/shell/profile +++ b/.config/shell/profile @@ -34,20 +34,74 @@ export LESS_TERMCAP_us=$'\E[01;36m' # Other program settings: export DISPLAY_DPI=96 -PATHS=( +# Remove an entry from PATH +rm_path_entry() { + local shell="${SHELL##*/}" rm_entry="${1:?Entry not set}" new_path path_arr + + if [[ "$shell" == "zsh" ]]; then + IFS=" " read -A path_arr <<<"${PATH//:/ }" + else + IFS=" " read -a path_arr <<<"${PATH//:/ }" + fi + + for path_entry in "${path_arr[@]}"; do + grep -Eq "^$rm_entry$" <<<"$path_entry" && continue + new_path="$new_path:$path_entry" + done + + PATH="${new_path#:}" +} + +# Remove one or more entries from PATH +rm_path_entries() { + local rm_entry + + for rm_entry in "$@"; do + rm_path_entry "$rm_entry" + done +} + +# Prepend one or more entries to PATH. +# The order of the supplied args will be kept, e.g. PATH="$1:$2:$PATH" +prepend_path_with_entries() { + local addl_paths entry + + for entry in "$@"; do + ! [ -d "$entry" ] && continue + addl_paths="$addl_paths:$entry" + done + + PATH="${addl_paths#:}:$PATH" +} + +# Append one or more entries to PATH. +# The order of the supplied args will be kept, e.g. PATH="$PATH:$1:$2" +append_path_with_entries() { + local addl_paths entry + + for entry in "$@"; do + ! [ -d "$entry" ] && continue + addl_paths="$addl_paths:$entry" + done + + PATH="$PATH:${addl_paths#:}" +} + +prepend_paths=( "$HOME/bin" "$HOME/.local/bin" "$HOME/.cargo/bin" "${KREW_ROOT:-$HOME/.krew}/bin" ) +append_paths=() -for DIR in "${PATHS[@]}"; do - ! [ -d "$DIR" ] && continue - export PATH="$PATH:$DIR" -done +[ -f "$HOME/homebrew/bin/brew" ] && eval "$("$HOME/homebrew/bin/brew" shellenv)" +[ -n "$HOMEBREW_PREFIX" ] && append_paths+=("$HOMEBREW_PREFIX/bin" "$HOMEBREW_PREFIX/sbin") + +rm_path_entries "${prepend_paths[@]}" "${append_paths[@]}" # Ensure we clean up first +prepend_path_with_entries "${prepend_paths[@]}" +append_path_with_entries "${append_paths[@]}" # Add the profile-extras file yourself if you wish to override anything: PROFILE_EXTRAS="${XDG_CONFIG_HOME:-$HOME/.config}/shell/profile-extras" [ -f "$PROFILE_EXTRAS" ] && . "$PROFILE_EXTRAS" - -[ -f "$HOME/homebrew/bin/brew" ] && eval "$("$HOME/homebrew/bin/brew" shellenv)"