#!/bin/sh YAZI_GITHUB="https://github.com/sxyazi/yazi" YAZI_DIR="${YAZI_DIR:=$HOME/.local/share/yazi}" _prompt() { printf "[YAZI MANAGER] \033[34m%s\033[0m \033[1m[y/n]:\033[0m " "$1" read -r opt case "$opt" in [Yy]) return 0;; [Nn]) return 1;; *) return 1;; esac } _msg() { printf "[YAZI MANAGER] \033[0m%s\033[0m\n" "$1" } _error() { printf "[YAZI MANAGER] \033[31merror: %s\033[0m\n" "$1" } # Check if $YAZI_DIR exists, if it doesn't, create it. # If there is an error, return 1. _yazi_dir_check() { if ! [ -d "$YAZI_DIR" ]; then return 1 fi } # Cleans $YAZI_DIR _clean_yazi_versions() { if [ -d "$YAZI_DIR" ]; then rm -rf "${YAZI_DIR:?}"/* fi } # Echoes list of yazi versions. _list_yazi_versions() { preferred="$1" for v in "$YAZI_DIR"/*; do if [ -d "$v" ]; then version="${v##*yazi-}" if [ "$preferred" = "$version" ]; then echo "$version*" else echo "$version" fi fi done } # Echoes latest installed yazi version. e.g. 25.5.31 # If there is an error, return 1. _latest_local() { versions="$(_list_yazi_versions)" for v in $versions ; do latest_local="$v" done 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. # If there is a problem with the binary, return 1. _yazi_bin() { preferred="$1" if [ -z "$preferred" ]; then preferred="$(_latest_local)" fi bin="$YAZI_DIR/yazi-$preferred/yazi-x86_64-unknown-linux-gnu/yazi" if [ -x "$bin" ]; then echo "$bin" else return 1 fi } # Echoes the latest remote available yazi version. e.g. 25.5.31 # If there is an error, return 1. _latest_remote() { # shellcheck disable=SC1083 release_url="$(curl -Ls -o /dev/null -w %{url_effective} "$YAZI_GITHUB"/releases/latest)" if [ "$?" -eq 1 ]; then return 1 fi echo "${release_url##*/v}" } # Downloads a given yazi version. e.g. _download_yazi 25.5.31 # If there is an error, return 1. _download_yazi() { version="$1" if [ -z "$version" ]; then _error "provide a version to download. e.g. 25.5.31 or latest" return 1 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" if [ "$?" -eq 1 ]; then _error "failed to download" return 1 fi if ! [ -f "$target" ]; then _error "failed to download" return 1 fi unzip -o "$target" -d "${target%.zip}" >/dev/null if [ "$?" -eq 1 ]; then _error "failed to extract" return 1 fi _set_preferred_version "$version" rm -f "$target" } # Updates to latest remote available version. # If there is an error, return 1. _update_yazi() { latest_remote="$(_latest_remote)" if [ "$?" -eq 1 ]; then _error "failed to look up latest version" return 1 fi latest_local="$(_latest_local)" if [ "$?" -eq 1 ]; then _error "failed to look up local version" return 1 fi if [ "$latest_local" != "$latest_remote" ]; then _msg "updating yazi ${latest_local:-none} -> $latest_remote..." _download_yazi "$latest_remote" if [ "$?" -eq 1 ]; then _error "failed installation" return 1 fi _set_preferred_version "$latest_remote" else _msg "already up-to-date" fi } 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 # shellcheck disable=SC2068 case "$1" in --manager-update) _update_yazi ;; --manager-download) _download_yazi "$2" ;; --manager-list) _list_yazi_versions "$(_current_local)" ;; --manager-set) _set_preferred_version "$2" ;; --manager-clean) _clean_yazi_versions ;; *) $YAZI_BIN $@ ;; esac else _yazi_dir_check if [ "$?" -eq 1 ]; then _prompt "Installation directory '$YAZI_DIR' does not exist, create?" if [ "$?" -eq 1 ]; then return 1 fi mkdir -p "$YAZI_DIR" fi _prompt "yazi not installed, download latest?" if ! [ "$?" -eq 1 ]; then _update_yazi fi fi