1
0
dotfiles/.config/zsh/lib/starship.zsh.sample
2021-04-19 01:00:58 +03:00

206 lines
5.9 KiB
Bash

#!/bin/zsh
## inspired by 'https://starship.rs/install.sh' as of 2021-03-07
ZSHU[starship_baseurl]='https://github.com/starship/starship/releases'
## ZSHU[starship_target] is auto-detected
## ZSHU[starship_path] defaults to ZSHU[d_bin] which is in PATH already
# export STARSHIP_CONFIG="$HOME/.config/starship.toml"
# export STARSHIP_CACHE="$HOME/.cache/starship"
__z_starship_auto_path() {
echo "${ZSHU[starship_path]:-${ZSHU[d_bin]}}"
}
__z_starship() {
local x=$(__z_starship_auto_path)
x="$x/starship"
[ -x "$x" ] || x=starship
[ -x "$x" ] || return 127
"$x" "$@"
}
__z_starship_test() { __z_starship -V &>/dev/null ; }
## NB: supply TARGET environment variable to call
__z_starship_url_latest() {
printf '%s/latest/download/starship-%s.tar.gz' \
"${ZSHU[starship_baseurl]}" "${TARGET}"
}
## NB: supply TARGET environment variable to call
## $1 - version (semver like '0.50.0')
__z_starship_url_versioned() {
printf '%s/download/v%s/starship-%s.tar.gz' \
"${ZSHU[starship_baseurl]}" "$1" "${TARGET}"
}
## NB: install starship somewhere in PATH ;)
__z_starship_ver_installed() {
__z_starship -V 2>/dev/null \
| sed -En '/^starship v?(\S.+)$/{s//\1/;p;}'
}
## NB: supply TARGET environment variable to call
__z_starship_ver_latest() {
local x=$(__z_starship_url_latest)
local y=$(__z_curl_location "$x")
## hackish strip, e.g.:
## from: https://github.com/starship/starship/releases/download/v0.50.0/starship-x86_64-unknown-linux-musl.tar.gz
## to: v0.50.0
y=${y:h:t}
[ "${y:0:1}" = 'v' ] && y=${y:1}
echo "$y"
}
__z_starship_detect_arch() {
local arch=${ZSHU[mach]}
case "${arch}" in
x86_64) [ "$(getconf LONG_BIT)" -eq 32 ] && arch=i686 ;;
aarch64) [ "$(getconf LONG_BIT)" -eq 32 ] && arch=arm ;;
esac
echo "${arch}"
}
__z_starship_detect_platform() {
local platform=${ZSHU[uname]}
case "${ZSHU[uname]}" in
msys_nt*) platform=pc-windows-msvc ;;
cygwin_nt*) platform=pc-windows-msvc ;;
mingw*) platform=pc-windows-msvc ;;
linux) platform=unknown-linux-musl ;; ## static builds
darwin) platform=apple-darwin ;;
freebsd) platform=unknown-freebsd ;;
esac
echo "${platform}"
}
## $1 - arch
## $2 - platform
__z_starship_detect_target() {
local target="$1-$2"
case "${target}" in
arm-unknown-linux-musl) target="${target}eabihf" ;;
esac
echo "${target}"
}
__z_starship_auto_target() {
[ -n "${ZSHU[starship_target]}" ] && echo "${ZSHU[starship_target]}" && return
local arch=$(__z_starship_detect_arch)
local platform=$(__z_starship_detect_platform)
local target=$(__z_starship_detect_target "${arch}" "${platform}")
echo "${target}"
}
__z_starship_install() {
local ver=${1:-latest}
local target url resp
target=$(__z_starship_auto_target)
if [ "${ver}" = 'latest' ] ; then
url=$(TARGET=${target} __z_starship_url_latest)
resp=$(__z_curl_response "${url}")
resp=${resp:-400}
[ ${resp} -ge 400 ] && return 1
else
url=$(TARGET=${target} __z_starship_url_versioned "${ver}")
resp=$(__z_curl_response "${url}")
resp=${resp:-400}
if [ ${resp} -ge 400 ] ; then
## last resort: try messing with version ;D
if [ "${ver:0:1}" = 'v' ] ; then
ver=${ver:1}
else
ver="v${ver}"
fi
url=$(TARGET=${target} __z_starship_url_versioned "${ver}")
resp=$(__z_curl_response "${url}")
resp=${resp:-400}
[ ${resp} -ge 400 ] && return 1
fi
fi
local t=$(mktemp -d)
local f="$t/starship.tar.gz"
command curl -sqL "${url}" > "$f"
command tar -C "$t" -xf "$f" starship &>/dev/null
if [ $? -ne 0 ] ; then
## last resort
command tar -C "$t" --strip-components=1 --wildcards -xf "$f" '*/starship' &>/dev/null
if [ $? -ne 0 ] ; then
rm -rf "$t"
return 1
fi
fi
local d=$(__z_starship_auto_path)
mv "$t/starship" "$d/"
local r=$?
if [ $r -eq 0 ] ; then
[ "${ver:0:1}" = 'v' ] && ver=${ver:1}
echo "starship: installed ${ver} version in $d/" 1>&2
fi
rm -rf "$t"
return $r
}
z-starship-target-available() {
local target url resp
target=$(__z_starship_auto_target)
url=$(TARGET=${target} __z_starship_url_latest)
resp=$(__z_curl_response "${url}")
resp=${resp:-400}
if [ ${resp} -lt 400 ] ; then
echo "starship: available for ${target}" 1>&2
return 0
else
echo "starship: NOT available for ${target}" 1>&2
return 1
fi
}
z-starship-update-available() {
local target=$(__z_starship_auto_target)
local installed=$(__z_starship_ver_installed)
local latest=$(TARGET=${target} __z_starship_ver_latest)
if [ -z "${latest}" ] ; then
echo "starship: update is NOT available" 1>&2
return 1
fi
if [ -z "${installed}" ] ; then
echo "starship: NOT installed, install it 1st" 1>&2
return 0
fi
local tailver=$(printf '%s\n' "${installed}" "${latest}" | sort -Vu | tail -n 1)
if [ "${installed}" = "${tailver}" ] ; then
if [ "${installed}" = "${latest}" ] ; then
echo "starship: local version is up to date" 1>&2
else
echo "starship: local version is newer! o_O" 1>&2
fi
return 1
else
echo "starship: update is available (${installed} -> ${latest})" 1>&2
return 0
fi
}
z-starship-init() {
[ -n "${ZSHU[starship_init]}" ] && return
__z_starship_test || return
eval "$(__z_starship init zsh)"
ZSHU[starship_init]=1
}
z-starship-install() {
z-starship-target-available || return
__z_starship_install || \
echo "starship: unable to install" 1>&2
}
z-starship-update() {
z-starship-update-available || return 0
__z_starship_install || \
echo "starship: unable to update" 1>&2
}