Skip to content

Instantly share code, notes, and snippets.

@balupton
Last active November 11, 2021 19:23
Show Gist options
  • Save balupton/8e86fbe8a97fb8def4e60143e62efbdb to your computer and use it in GitHub Desktop.
Save balupton/8e86fbe8a97fb8def4e60143e62efbdb to your computer and use it in GitHub Desktop.
dump of eval-segment attempt, not worth it
#!/usr/bin/env bash
source "$DOROTHY/sources/strict.bash"
# {
# # do a bunch of things
# } 2>&1 | echo-segment --collapse --piped 'do something' || echo-segment --elsed 'do something'
if test "$#" -eq 0; then
{
echo 'one'
sleep 2
echo 'two'
exit 1
} | echo-segment --collapse --pipe -- 'header'
echo-segment --exit="$?" -- 'header'
exit "$?"
fi
# options
mapfile -t options < <(echo-before-separator "$@")
option_pipe="$(get-flag-value pipe -- "${options[@]}")"
option_exit="$(get-flag-value exit -- "${options[@]}")"
mapfile -t args < <(echo-after-separator "$@")
if test "$option_pipe" = 'yes'; then
echo-color --oh2 --h2="${args[*]}" --ch2
echo-revolving-door </dev/stdin
# log="$(mktemp)"
# cat </dev/stdin | tee "$log" | echo-revolving-door
elif test -n "$option_exit"; then
if test "$option_exit" -eq 0; then
echo-color --og2 --g2="${args[*]}" --cg2
else
echo-color --oe2 --e2="${args[*]}" --ce2
return "$option_exit"
fi
else
exit 1
fi
#!/usr/bin/env bash
# shellcheck disable=SC2164,SC1091
source "$DOROTHY/sources/strict.bash"
# helper
function eval_collapse() {
# prepare
local ec=0 status='' log options QUIET option_collapse option_pending option_success option_failure option_h1 option_h2 option_h3 args
log="$(mktemp)" header=''
# options
mapfile -t options < <(echo-before-separator "$@")
QUIET="$(get-flag-value quiet --missing="${QUIET-}" -- "${options[@]}" | echo-affirmative || get-flag-value verbose --missing="${VERBOSE-}" -- "$@" | echo-non-affirmative || :)"
option_collapse="$(get-flag-value collapse --missing=yes -- "${options[@]}" | echo-affirmative)"
option_pending="$(get-flag-value pending -- "${options[@]}")"
option_success="$(get-flag-value success -- "${options[@]}")"
option_failure="$(get-flag-value failure -- "${options[@]}")"
option_h1="$(get-flag-value h1 -- "${options[@]}")"
option_h2="$(get-flag-value h2 -- "${options[@]}")"
option_h3="$(get-flag-value h3 -- "${options[@]}")"
mapfile -t args < <(echo-after-separator "$@")
# helpers
function eval_collapse_header() {
if test -n "$option_pending"; then
echo "$option_pending"
elif test -n "$option_h1"; then
echo
echo-color --oh1 --h1="$option_h1" --ch1
elif test -n "$option_h2"; then
echo-color --oh2 --h2="$option_h2" --ch2
elif test -n "$option_h3"; then
echo-color --oh3 --h3="$option_h3" --ch3
fi
echo-color --oe --h3="${args[*]}" --ce
}
function eval_collapse_footer() {
if test "$ec" -eq 0; then
if test -n "$option_success"; then
echo "$option_success"
elif test -n "$option_h1"; then
echo-color --og1 --g1="$option_h1" --cg1
elif test -n "$option_h2"; then
echo-color --og2 --g2="$option_h2" --cg2
elif test -n "$option_h3"; then
echo-color --og3 --g3="$option_h3" --cg3
fi
else
if test -n "$option_failure"; then
echo "$option_failure"
elif test -n "$option_h1"; then
echo-color --oe1 --e1="$option_h1" --ce1
elif test -n "$option_h2"; then
echo-color --oe2 --e2="$option_h2" --ce2
elif test -n "$option_h3"; then
echo-color --oe3 --e3="$option_h3" --ce3
fi
fi
trap - RETURN
}
# header and footer
header="$(eval_collapse_header)"
trap eval_collapse_footer RETURN
# run
if test "$option_collapse" = 'no'; then
ec=0 && ("${args[@]}") || ec="$?"
else
ec=0 && ("${args[@]}" 2>&1 | tee "$log" | echo-revolving-door) || ec="$?"
fi
# determine
if test "$ec" -eq 0; then
# unless QUIET=no (verbose mode) then
# hide header (the revolving door already hid the command output)
# and return
if test "$QUIET" != 'no'; then
if test "$option_collapse" = 'yes'; then
# hide headers, only possible if revolving door was used
echo-clear-lines <<<"$header"
fi
# return
return "$ec"
fi
# success status
status="$(echo-color -n --g3="[$ec]")"
else
# failure status
status="$(echo-color -n --e3="[$ec]")"
fi
# did the command output anything?
if test -z "$(cat "$log")"; then
# there was no output
if test "$option_collapse" = 'yes'; then
# hide headers, only possible if revolving door was used
echo-clear-lines <<<"$header"
fi
# and output header footer combo
echo-color --oe --h3="${args[*]}" --ces "$status"
else
# if it did
# then output its results (if error)
if test "$ec" -ne 0; then
cat "$log"
fi
# then output footer
echo-color --oes --h3="${args[*]}" --ce "$status"
fi
# done
return "$ec"
}
# fire if invoked standalone
if test "$0" = "${BASH_SOURCE[0]}"; then
eval_collapse "$@"
fi
#!/usr/bin/env bash
# shellcheck disable=SC2164,SC1091,SC1090
source "$DOROTHY/sources/strict.bash"
source "$(which eval-collapse)"
# ✓✔☑
# todo: this is newer than commands/pipp and setup-util pip and pipx, so consider what to do with them
# =====================================
# Configuration
source "$DOROTHY/sources/config.sh"
load_dorothy_config 'setup.bash'
# ^ provides: PYTHON_INSTALL, PIP_INSTALL, PIPX_INSTALL
# =====================================
# Start
function install_python() {
# python2
env CLI=python \
APK=python \
APT=python \
BREW=python \
PACMAN=python \
YUM=python \
setup-util
# python3
env CLI=python3 \
APK=python3 \
APT=python3 \
BREW=python3 \
PACMAN=python3 \
YUM=python3 \
setup-util
# pipx dependencies for linux
if ! is-mac; then
env OPTIONAL=yes NAME='python3-venv' \
APT='python3-venv' \
PACMAN='python-virtualenv' \
setup-util
fi
# refresh environment
source "$DOROTHY/sources/environment.sh"
}
function install_pyenv() {
true
# PYENV_ROOT="${PYENV_ROOT:-"$HOME/.pyenv"}"
# echo -e "\nCleaning pyenv..."
# rm -Rf "$PYENV_ROOT"
# if test -n "${PYENV_VERSION-}"; then
# echo -e "\nEnsuring pyenv is latest..."
# fetch https://pyenv.run | bash
# source "$DOROTHY/sources/environment.sh"
# pyenv install "$PYENV_VERSION"
# fi
# source "$DOROTHY/sources/environment.sh"
}
function install_each_pip_package() {
local bin="$1"
shift
for item in "$@"; do
eval_collapse --success="$(echo-color --dim+green=" pip installed: $item")" \
-- "$bin" -m \
pip install --user --upgrade --force-reinstall --no-warn-script-location "$item" || :
done
}
function install_each_pipx_package() {
local bin="$1"
shift
for item in "$@"; do
eval_collapse --success="$(echo-color --dim+green=" pipx installed: $item")" \
-- "$bin" -m \
pipx install --include-deps --force "$item" || :
done
}
function install_python_packages() {
local bin="$1" pip_installer_url pip_installer python_version pip_install=() pipx_install=()
# prepare
pip_installer="$(mktemp)" # --suffix='-get-pip.py' not supported by macos
if test -v 'PYTHON_INSTALL'; then
pip_install+=("${PYTHON_INSTALL[@]}")
fi
if test -v 'PIP_INSTALL'; then
pip_install+=("${PIP_INSTALL[@]}")
fi
if get-python-version "$bin" '2.'; then
python_version=2
if test -v 'PYTHON2_PIP_INSTALL'; then
pip_install+=("${PYTHON2_PIP_INSTALL[@]}")
fi
pip_installer_url='https://bootstrap.pypa.io/pip/2.7/get-pip.py'
else
python_version=3
if test -v 'PYTHON3_PIP_INSTALL'; then
pip_install+=("${PYTHON3_PIP_INSTALL[@]}")
fi
if test -v 'PIPX_INSTALL'; then
pipx_install+=("${PIPX_INSTALL[@]}")
fi
pip_installer_url='https://bootstrap.pypa.io/get-pip.py'
fi
# download pip
eval_collapse --h2="download pip" \
-- down "$pip_installer_url" --destination="$pip_installer"
# install pip
eval_collapse --h2="install pip" \
-- "$bin" "$pip_installer" --user
source "$DOROTHY/sources/environment.sh"
# echo -e "\nUpgrading pip for $bin..."
# "$bin" -m pip install \
# --user --upgrade --force-reinstall --no-warn-script-location \
# pip setuptools
# source "$DOROTHY/sources/environment.sh"
# install pip packages
eval_collapse --no-collapse --h3="install ${#pip_install[@]} pip packages" \
-- install_each_pip_package s"$bin" "${pip_install[@]}"
# pipx
if test "$python_version" -eq 3; then
# install pipx
eval_collapse -h3="install pipx" \
-- "$bin" -m pip install --user --upgrade --force-reinstall --no-warn-script-location pipx || :
source "$DOROTHY/sources/environment.sh"
# install pipx packages
eval_collapse --no-collapse --h3="install ${#pipx_install[@]} pipx packages" \
-- install_each_pipx_packages "$bin" "${pipx_install[@]}"
fi
}
function setup_python() {
eval_collapse --h2='install python' -- install_python
return 0
for bin in python python2 python3; do
if command-exists "$bin"; then
eval_collapse --h2="install $bin packages" \
-- install_python_packages "$bin"
fi
done
}
eval_collapse --no-collapse --h1='setup python' -- setup_python
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment