Update skel, common functions.

This commit is contained in:
David Tomaschik
2026-05-26 14:12:32 -07:00
parent ecbc25e5ac
commit 5a7d9cf060
4 changed files with 137 additions and 51 deletions

63
dotfiles/commonrc.sh Normal file
View File

@@ -0,0 +1,63 @@
#!/bin/sh
# shellcheck disable=SC1090
# common functions for use in shell scripts
find_first() {
while [ "$#" -gt 0 ]; do
if test -e "${1}" ; then
echo "${1}"
return 0
fi
shift
done
return 1
}
have_command() {
command -v "$1" >/dev/null 2>&1
}
# Helper function: Returns 0 if the directory is in PATH, 1 otherwise
path_contains() {
case ":${PATH}:" in
*:"$1":*) return 0 ;;
*) return 1 ;;
esac
}
# Prepend a directory to PATH if it's not already there
path_prepend() {
if [ -d "$1" ] && ! path_contains "$1"; then
PATH="$1:${PATH}"
fi
}
# Append a directory to PATH if it's not already there
path_append() {
if [ -d "$1" ] && ! path_contains "$1"; then
PATH="${PATH}:$1"
fi
}
source_first_existing() {
_sfe_script="$(find_first "$@")"
_sfe_status=$?
if [ "$_sfe_status" -eq 0 ]; then
. "$_sfe_script"
# Clean up our temporary variables before returning success
unset -v _sfe_script _sfe_status
return 0
fi
# Clean up our temporary variables before returning failure
unset -v _sfe_script _sfe_status
return 1
}
source_if_existing() {
if [ -f "$1" ]; then
. "$1"
fi
}

View File

@@ -59,6 +59,8 @@ DIRSTACKSIZE=16
export OS="$(uname 2>/dev/null || echo "Unknown")" export OS="$(uname 2>/dev/null || echo "Unknown")"
source ~/.commonrc.sh
# Set terminal title # Set terminal title
case $TERM in case $TERM in
# Only set the title for terminals that are likely to support it # Only set the title for terminals that are likely to support it
@@ -163,25 +165,6 @@ bindkey '^r' history-incremental-search-backward
# delete really deletes # delete really deletes
bindkey "^[[3~" delete-char bindkey "^[[3~" delete-char
source_if_existing() {
[[ -f "${1}" ]] && source "${1}"
}
source_first_existing() {
while (($#)); do
if test -e "${1}" ; then
source "${1}"
return
fi
shift
done
return 1
}
have_command() {
command -v "${1}" &>/dev/null
}
if test -d ${HOME}/.local/bin ; then if test -d ${HOME}/.local/bin ; then
export PATH="${HOME}/.local/bin:${PATH}" export PATH="${HOME}/.local/bin:${PATH}"
fi fi

View File

@@ -2,31 +2,18 @@ function dumpenv {
if [ "$(uname)" = "Linux" ]; then if [ "$(uname)" = "Linux" ]; then
tr '\0' '\n' < /proc/${1}/environ tr '\0' '\n' < /proc/${1}/environ
elif [ "$(uname)" = "Darwin" ]; then elif [ "$(uname)" = "Darwin" ]; then
# macOS doesn't have /proc, use ps instead. # macOS doesn't have /proc, use ps instead.
# Note: this may truncate if environment is very large. # Note: this may truncate if environment is very large.
ps -p ${1} -wwwe -o command= | tr ' ' '\n' | grep '=' ps -p ${1} -wwwe -o command= | tr ' ' '\n' | grep '='
fi fi
} }
if test -x "/sbin/starship" ; then _STARSHIP_PATH="$(find_first "$(command -v starship)" /sbin/starship "${HOME}/tools/starship/starship" "${HOME}/.local/bin/starship" /usr/local/bin/starship)"
_STARSHIP_PATH="/sbin/starship" if test -n "$_STARSHIP_PATH" ; then
function starship_prompt { function starship_prompt {
eval "$(/sbin/starship init zsh)" eval "$($_STARSHIP_PATH init zsh)"
}
elif test -x "${HOME}/tools/starship/starship" ; then
_STARSHIP_PATH="${HOME}/tools/starship/starship"
function starship_prompt {
eval "$($HOME/tools/starship/starship init zsh)"
} }
fi fi
if test -f ${HOME}/.zprompt ; then
if test "$(cat ${HOME}/.zprompt)" = "starship" ; then
if test -n "${_STARSHIP_PATH:-}" ; then
eval "$(${_STARSHIP_PATH} init zsh)"
fi
fi
fi
unset _STARSHIP_PATH
function hashall { function hashall {
tee >(md5sum) | tee >(sha1sum) | sha256sum tee >(md5sum) | tee >(sha1sum) | sha256sum

View File

@@ -7,14 +7,30 @@ set -o errexit
set -o shwordsplit 2>/dev/null || true # Make zsh behave like bash set -o shwordsplit 2>/dev/null || true # Make zsh behave like bash
HOME=${HOME:-$(cd ~ && pwd)} HOME=${HOME:-$(cd ~ && pwd)}
LOCAL_BIN="${HOME}/.local/bin"
STARSHIP_INSTALL_HASH="52c64f14a558034ebeb1907ea9364e802b32474576fd3e68265f73bc33cc8fbb"
have_command() { have_command() {
command -v "${1}" >/dev/null 2>&1 command -v "${1}" >/dev/null 2>&1
} }
sudo_group() {
if [[ "$(id -u)" -eq 0 ]] ; then
return 0
fi
have_command sudo && ( id -Gn | grep -q '\bsudo\b' )
}
maybe_sudo() {
if [[ "$(id -u)" -eq 0 ]] ; then
"$@"
return
fi
if ! have_command sudo ; then
return 1
fi
sudo "$@"
}
link_directory_contents() { link_directory_contents() {
local SRCDIR="${1}" local SRCDIR="${1}"
@@ -176,6 +192,41 @@ install_dotfiles() {
fi fi
} }
install_starship() {
if have_command starship ; then return 0 ; fi
if have_command apt-get && sudo_group ; then
if maybe_sudo apt-get install -qy starship ; then
return 0
fi
echo "apt-get install starship failed, installing locally" >&2
fi
local tmpd
tmpd="$(mktemp -d tmp.starship.XXXXXX)"
local install_path="${tmpd}/install.sh"
if have_command curl ; then
curl -sSL --show-error -o "${install_path}" https://starship.rs/install.sh
elif have_command wget ; then
wget -q -O "${install_path}" --https-only https://starship.rs/install.sh
else
echo "No curl or wget available!!" >&2
return 1
fi
local dl_hash
dl_hash="$(sha256sum "${install_path}" | awk '{print $1}')"
if [[ "$dl_hash" != "${STARSHIP_INSTALL_HASH}" ]] ; then
echo "Hash check failed!!" >&2
echo "Expected: ${STARSHIP_INSTALL_HASH}, got ${dl_hash} on ${install_path}" >&2
return 1
fi
if sudo_group ; then
if maybe_sudo sh "${install_path}" ; then
return 0
fi
echo "root installation failed, falling back to user-local" >&2
fi
sh "${install_path}" -b "${LOCAL_BIN}"
}
install_main() { install_main() {
if [[ -d "${BASEDIR}/.git" ]] && have_command git ; then if [[ -d "${BASEDIR}/.git" ]] && have_command git ; then
if [[ -z "$(git -C "${BASEDIR}" status --porcelain)" ]]; then if [[ -z "$(git -C "${BASEDIR}" status --porcelain)" ]]; then
@@ -185,6 +236,8 @@ install_main() {
fi fi
fi fi
[[ "$MINIMAL" = 1 ]] || { [[ "$MINIMAL" = 1 ]] || {
mkdir -p "${LOCAL_BIN}"
install_starship
# Install vim-plug if not already present # Install vim-plug if not already present
local VIM_PLUG_URL="https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim" local VIM_PLUG_URL="https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim"
@@ -199,22 +252,22 @@ install_main() {
else else
echo "Error: curl not found. Cannot install vim-plug." >&2 echo "Error: curl not found. Cannot install vim-plug." >&2
fi fi
fi fi
# Install TPM (Tmux Plugin Manager) if not already present # Install TPM (Tmux Plugin Manager) if not already present
local TPM_DIR="${HOME}/.tmux/plugins/tpm" local TPM_DIR="${HOME}/.tmux/plugins/tpm"
local TPM_REPO="https://github.com/tmux-plugins/tpm" local TPM_REPO="https://github.com/tmux-plugins/tpm"
if [[ ! -d "${TPM_DIR}" ]]; then if [[ ! -d "${TPM_DIR}" ]]; then
verbose "Installing TPM (Tmux Plugin Manager)..." verbose "Installing TPM (Tmux Plugin Manager)..."
if have_command git; then if have_command git; then
git clone --depth 1 "${TPM_REPO}" "${TPM_DIR}" git clone --depth 1 "${TPM_REPO}" "${TPM_DIR}"
else else
echo "Error: git not found. Cannot install TPM." >&2 echo "Error: git not found. Cannot install TPM." >&2
fi fi
fi fi
# try to update dotfile overlays # try to update dotfile overlays
if [[ -d "${BASEDIR}/dotfile_overlays" ]] ; then if [[ -d "${BASEDIR}/dotfile_overlays" ]] ; then
for dotfiledir in "${BASEDIR}/dotfile_overlays/"* ; do for dotfiledir in "${BASEDIR}/dotfile_overlays/"* ; do
if [[ -d "${dotfiledir}/.git" ]] ; then if [[ -d "${dotfiledir}/.git" ]] ; then