From eaaeb9fb8602e57a98271811c2ef64d317a058af Mon Sep 17 00:00:00 2001 From: PowerUser64 Date: Fri, 30 May 2025 14:53:52 -0700 Subject: [PATCH] env: add set_preferred function --- .config/shell/env | 59 ++++++++++++++++++++++++++--------------------- 1 file changed, 33 insertions(+), 26 deletions(-) diff --git a/.config/shell/env b/.config/shell/env index 9a8930e..9917338 100644 --- a/.config/shell/env +++ b/.config/shell/env @@ -3,30 +3,37 @@ # This file should be compatible with standard sh # shellcheck disable=SC2034 -# path_append from https://superuser.com/questions/39751/add-directory-to-path-if-its-not-already-there # checks if a path is already in the PATH and if it exists path_append() { if [ -d "$1" ]; then - # posix SH doesn't have [[, so we skip testing if the path is already in the PATH for simplicity - # shellcheck disable=SC3010 - if command '[[' > /dev/null 2>&1 && [[ ":$PATH:" != *":$1:"* ]]; then - PATH="${PATH:+"$PATH:"}$1" - else - PATH="$PATH:$1" - fi + case ":$PATH:" in + (:"$1":) ;; + (*) + PATH="${PATH:+"$PATH:"}$1" ;; + esac fi } path_prepend() { if [ -d "$1" ]; then - # posix SH doesn't have [[, so we skip testing if the path is in the PATH already for simplicity - # shellcheck disable=SC3010 - if command '[[' > /dev/null 2>&1 && [[ ":$PATH:" != *":$1:"* ]]; then - PATH="$1${PATH:+":$PATH"}" - else - PATH="$1:$PATH" - fi + case ":$PATH:" in + (:"$1":) ;; + (*) + PATH="$1${PATH:+":$PATH"}" ;; + esac fi } +set_preferred() { + var="$1" + shift + for p; do + if command -v "$p" > /dev/null; then + eval "$var="'"$p"' + return + fi + done + echo "Error (env): couldn't find a valid candidate for \$$var" + unset var +} # these would be temp vars, but ZSH does something weird where it makes them evaluate at runtime instead of export CONFIG_HOME="${XDG_DATA_HOME:-$HOME/.config}" @@ -59,21 +66,21 @@ if [ "${EDITOR:-}" = "" ] || [ "${EDITOR##*/}" = "nano" ] || ! command -v "$EDITOR" > /dev/null then - if command -v nvim > /dev/null;then - export EDITOR=nvim - elif command -v vim > /dev/null;then - export EDITOR=vim - elif command -v vi > /dev/null;then - export EDITOR=vi - elif command -v nano > /dev/null; then - export EDITOR=nano - else - echo "Error: couldn't find a valid candidate for \$EDITOR" >&2 - fi + set_preferred EDITOR nvim vim vi nano fi export SYSTEMD_EDITOR="$EDITOR" export VISUAL="$EDITOR" +# Set a preferred terminal emulator +if [ "${TERM_EMU:-}" = "" ]; then + set_preferred TERM_EMU kitty alacritty +fi + +# Set a preferred terminal emulator +if [ "${PAGER:-}" = "" ]; then + set_preferred PAGER less more +fi + # Tell zellij to auto attach by default instead of opening a new session export ZELLIJ_AUTO_ATTACH=true