env: add set_preferred function
This commit is contained in:
parent
91d650025a
commit
eaaeb9fb86
1 changed files with 33 additions and 26 deletions
|
@ -3,30 +3,37 @@
|
||||||
# This file should be compatible with standard sh
|
# This file should be compatible with standard sh
|
||||||
# shellcheck disable=SC2034
|
# 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
|
# checks if a path is already in the PATH and if it exists
|
||||||
path_append() {
|
path_append() {
|
||||||
if [ -d "$1" ]; then
|
if [ -d "$1" ]; then
|
||||||
# posix SH doesn't have [[, so we skip testing if the path is already in the PATH for simplicity
|
case ":$PATH:" in
|
||||||
# shellcheck disable=SC3010
|
(:"$1":) ;;
|
||||||
if command '[[' > /dev/null 2>&1 && [[ ":$PATH:" != *":$1:"* ]]; then
|
(*)
|
||||||
PATH="${PATH:+"$PATH:"}$1"
|
PATH="${PATH:+"$PATH:"}$1" ;;
|
||||||
else
|
esac
|
||||||
PATH="$PATH:$1"
|
|
||||||
fi
|
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
path_prepend() {
|
path_prepend() {
|
||||||
if [ -d "$1" ]; then
|
if [ -d "$1" ]; then
|
||||||
# posix SH doesn't have [[, so we skip testing if the path is in the PATH already for simplicity
|
case ":$PATH:" in
|
||||||
# shellcheck disable=SC3010
|
(:"$1":) ;;
|
||||||
if command '[[' > /dev/null 2>&1 && [[ ":$PATH:" != *":$1:"* ]]; then
|
(*)
|
||||||
PATH="$1${PATH:+":$PATH"}"
|
PATH="$1${PATH:+":$PATH"}" ;;
|
||||||
else
|
esac
|
||||||
PATH="$1:$PATH"
|
|
||||||
fi
|
|
||||||
fi
|
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
|
# 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}"
|
export CONFIG_HOME="${XDG_DATA_HOME:-$HOME/.config}"
|
||||||
|
@ -59,21 +66,21 @@ if [ "${EDITOR:-}" = "" ] ||
|
||||||
[ "${EDITOR##*/}" = "nano" ] ||
|
[ "${EDITOR##*/}" = "nano" ] ||
|
||||||
! command -v "$EDITOR" > /dev/null
|
! command -v "$EDITOR" > /dev/null
|
||||||
then
|
then
|
||||||
if command -v nvim > /dev/null;then
|
set_preferred EDITOR nvim vim vi nano
|
||||||
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
|
|
||||||
fi
|
fi
|
||||||
export SYSTEMD_EDITOR="$EDITOR"
|
export SYSTEMD_EDITOR="$EDITOR"
|
||||||
export VISUAL="$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
|
# Tell zellij to auto attach by default instead of opening a new session
|
||||||
export ZELLIJ_AUTO_ATTACH=true
|
export ZELLIJ_AUTO_ATTACH=true
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue