This commit is contained in:
tavo 2025-08-03 19:23:30 -06:00
parent 60d707a8fe
commit 20abab508a

View file

@ -21,30 +21,34 @@ _error() {
printf "[YAZI MANAGER] \033[31merror: %s\033[0m\n" "$1" printf "[YAZI MANAGER] \033[31merror: %s\033[0m\n" "$1"
} }
# Cleans $YAZI_DIR
_clean_yazi_versions() {
if [ -d "$YAZI_DIR" ]; then
rm -rf "$YAZI_DIR"/*
fi
}
# Check if $YAZI_DIR exists, if it doesn't, create it. # Check if $YAZI_DIR exists, if it doesn't, create it.
# If there is an error, return 1. # If there is an error, return 1.
_yazi_dir_check() { _yazi_dir_check() {
if ! [ -d "$YAZI_DIR" ]; then if ! [ -d "$YAZI_DIR" ]; then
_prompt "Installation directory '$YAZI_DIR' does not exist, create?"
if [ "$?" -eq 1 ]; then
return 1 return 1
fi fi
mkdir -p "$YAZI_DIR" }
# Cleans $YAZI_DIR
_clean_yazi_versions() {
if [ -d "$YAZI_DIR" ]; then
rm -rf "${YAZI_DIR:?}"/*
fi fi
} }
# Echoes list of yazi versions. # Echoes list of yazi versions.
_list_yazi_versions() { _list_yazi_versions() {
preferred="$1"
for v in "$YAZI_DIR"/*; do for v in "$YAZI_DIR"/*; do
if [ -d "$v" ]; then if [ -d "$v" ]; then
echo "${v##*yazi-}" version="${v##*yazi-}"
if [ "$preferred" = "$version" ]; then
echo "$version*"
else
echo "$version"
fi
fi fi
done done
} }
@ -60,11 +64,39 @@ _latest_local() {
echo "$latest_local" echo "$latest_local"
} }
# Sets the preferred yazi version
# If there is an error, return 1.
_set_preferred_version() {
version="$1"
if [ -z "$version" ]; then
_error "provide a version to set. e.g. 25.5.31"
return 1
fi
echo "$version" > "$YAZI_DIR"/yazi_version
}
# Echoes current set yazi version. e.g. 25.5.31
# If there is an error, return 1.
_current_local() {
if [ -f "$YAZI_DIR"/yazi_version ]; then
cat "$YAZI_DIR"/yazi_version
else
return 1
fi
}
# Get the latest installed yazi binary path. # Get the latest installed yazi binary path.
# If there is a problem with the binary, return 1. # If there is a problem with the binary, return 1.
_yazi_bin() { _yazi_bin() {
latest_local="$(_latest_local)" preferred="$1"
bin="$YAZI_DIR/yazi-$latest_local/yazi-x86_64-unknown-linux-gnu/yazi"
if [ -z "$preferred" ]; then
preferred="$(_latest_local)"
fi
bin="$YAZI_DIR/yazi-$preferred/yazi-x86_64-unknown-linux-gnu/yazi"
if [ -x "$bin" ]; then if [ -x "$bin" ]; then
echo "$bin" echo "$bin"
@ -76,6 +108,7 @@ _yazi_bin() {
# Echoes the latest remote available yazi version. e.g. 25.5.31 # Echoes the latest remote available yazi version. e.g. 25.5.31
# If there is an error, return 1. # If there is an error, return 1.
_latest_remote() { _latest_remote() {
# shellcheck disable=SC1083
release_url="$(curl -Ls -o /dev/null -w %{url_effective} "$YAZI_GITHUB"/releases/latest)" release_url="$(curl -Ls -o /dev/null -w %{url_effective} "$YAZI_GITHUB"/releases/latest)"
if [ "$?" -eq 1 ]; then if [ "$?" -eq 1 ]; then
return 1 return 1
@ -87,14 +120,24 @@ _latest_remote() {
# If there is an error, return 1. # If there is an error, return 1.
_download_yazi() { _download_yazi() {
version="$1" version="$1"
target="$YAZI_DIR/yazi-$version.zip"
download_url="$YAZI_GITHUB/releases/download/v$version/yazi-x86_64-unknown-linux-gnu.zip"
if [ -z "$version" ]; then if [ -z "$version" ]; then
_error "provide a version to download. e.g. 25.5.31" _error "provide a version to download. e.g. 25.5.31 or latest"
return 1 return 1
fi fi
case "$version" in
latest) version="$(_latest_remote)"
if [ "$?" -eq 1 ]; then
_error "failed get latest"
return 1
fi
;;
*) ;;
esac
target="$YAZI_DIR/yazi-$version.zip"
download_url="$YAZI_GITHUB/releases/download/v$version/yazi-x86_64-unknown-linux-gnu.zip"
curl -sLo "$target" "$download_url" curl -sLo "$target" "$download_url"
if [ "$?" -eq 1 ]; then if [ "$?" -eq 1 ]; then
_error "failed to download" _error "failed to download"
@ -112,9 +155,12 @@ _download_yazi() {
return 1 return 1
fi fi
_set_preferred_version "$version"
rm -f "$target" rm -f "$target"
} }
# Updates to latest remote available version.
# If there is an error, return 1.
_update_yazi() { _update_yazi() {
latest_remote="$(_latest_remote)" latest_remote="$(_latest_remote)"
if [ "$?" -eq 1 ]; then if [ "$?" -eq 1 ]; then
@ -129,35 +175,56 @@ _update_yazi() {
fi fi
if [ "$latest_local" != "$latest_remote" ]; then if [ "$latest_local" != "$latest_remote" ]; then
_msg "installing yazi ${latest_local:-none} -> $latest_remote..." _msg "updating yazi ${latest_local:-none} -> $latest_remote..."
_download_yazi "$latest_remote" _download_yazi "$latest_remote"
if [ "$?" -eq 1 ]; then if [ "$?" -eq 1 ]; then
_error "failed installation" _error "failed installation"
return 1 return 1
fi fi
_set_preferred_version "$latest_remote"
else else
_msg "already up-to-date" _msg "already up-to-date"
fi fi
} }
YAZI_BIN="$(_yazi_bin)" if [ -d "$YAZI_DIR" ]; then
YAZI_BIN="$(_yazi_bin "$(_current_local)")"
if [ "$?" -eq 1 ] && [ "$1" != "--manager-download" ] ; then
_prompt "no version of yazi installed, download latest?"
if [ "$?" -eq 1 ]; then
_error "no version of yazi installed, download via '--manager-download x.x.x'"
return 1
fi
_download_yazi "latest"
if [ "$?" -eq 1 ]; then
_error "error downloading latest version"
return 1
fi
fi
if [ -x "$YAZI_BIN" ]; then
# shellcheck disable=SC2068 # shellcheck disable=SC2068
case "$1" in case "$1" in
--manager-update) _update_yazi ;; --manager-update) _update_yazi ;;
--manager-download) _download_yazi $2 ;; --manager-download) _download_yazi "$2" ;;
--manager-list) _list_yazi_versions $2 ;; --manager-list) _list_yazi_versions "$(_current_local)" ;;
--manager-set) _set_preferred_version "$2" ;;
--manager-clean) _clean_yazi_versions ;; --manager-clean) _clean_yazi_versions ;;
*) $YAZI_BIN $@ ;; *) $YAZI_BIN $@ ;;
esac esac
else else
_yazi_dir_check _yazi_dir_check
if [ "$?" -eq 1 ]; then
_prompt "yazi not installed, download?" _prompt "Installation directory '$YAZI_DIR' does not exist, create?"
if [ "$?" -eq 1 ]; then if [ "$?" -eq 1 ]; then
return 1 return 1
fi fi
mkdir -p "$YAZI_DIR"
fi
_prompt "yazi not installed, download latest?"
if ! [ "$?" -eq 1 ]; then
_update_yazi _update_yazi
fi fi
fi