# Check for required commands
check_dependencies() {
local missing_deps=()
if ! command -v curl >/dev/null 2>&1 && ! command -v wget >/dev/null 2>&1; then
missing_deps+=("curl or wget")
fi
if ! command -v unzip >/dev/null 2>&1; then
missing_deps+=("unzip")
fi
# Homebrew 本身最好存在(优先用 brew 安装)
if ! command -v brew >/dev/null 2>&1; then
echo "Note: Homebrew not found. The script will fallback to downloading the binary from GitHub."
fi
if [ ${#missing_deps[@]} -gt 0 ]; then
echo "Error: Missing required dependencies: ${missing_deps[*]}"
echo ""
echo "To install on macOS (Homebrew):"
echo " brew install curl unzip"
exit 1
fi
}
check_dependencies
# Simple menu
print "Please select an option:"
print "1) Install all (yazi + theme + function)"
print "2) Install yazi and configure yy wrapper function"
print "3) Only install yazi"
print "q) Quit"
# macOS: yazi 与 ya 的最终路径(优先 brew;若用 GitHub zip 则指向解压目录)
YAZI_BIN=""
YA_BIN=""
USE_BREW=0
if command -v brew >/dev/null 2>&1; then
echo "Detected Homebrew. Using Homebrew to install yazi..."
# 如你希望同时安装常用可选依赖,可启用下两行(可选):
# brew install ffmpeg-full sevenzip jq poppler fd ripgrep fzf zoxide resvg imagemagick-full font-symbols-only-nerd-font 2>/dev/null || true
# brew link ffmpeg-full imagemagick-full -f --overwrite 2>/dev/null || true
if brew install yazi; then
USE_BREW=1
# brew 安装后通常会把 bin 加入 PATH;确保能找到 ya
YAZI_BIN=$(command -v yazi 2>/dev/null || true)
YA_BIN=$(command -v ya 2>/dev/null || true)
# 若当前 shell 未刷新 PATH,尝试用 brew 前缀猜测路径
if [[ -z "$YAZI_BIN" || -z "$YA_BIN" ]]; then
local_brew_prefix=$(brew --prefix 2>/dev/null || true)
if [[ -n "$local_brew_prefix" && -x "${local_brew_prefix}/bin/yazi" ]]; then
YAZI_BIN="${local_brew_prefix}/bin/yazi"
fi
if [[ -n "$local_brew_prefix" && -x "${local_brew_prefix}/bin/ya" ]]; then
YA_BIN="${local_brew_prefix}/bin/ya"
fi
fi
if [[ -z "$YAZI_BIN" || -z "$YA_BIN" ]]; then
error_exit "Homebrew installed yazi but cannot locate yazi or ya binaries."
fi
echo "Homebrew install success: yazi=${YAZI_BIN}, ya=${YA_BIN}"
else
error_exit "Homebrew install failed."
fi
else
# ---- 若没有 Homebrew:从 GitHub 下载 macOS 预编译包 ----
echo "Homebrew not found. Falling back to downloading the binary from GitHub."
CURRENT_DIR=$(pwd)
# 自动识别 Apple Silicon (arm64) 或 Intel (x86_64)
ARCH=$(uname -m)
case "$ARCH" in
arm64)
ARCH_SUFFIX="aarch64-apple-darwin"
;;
x86_64)
ARCH_SUFFIX="x86_64-apple-darwin"
;;
*)
error_exit "Unsupported macOS architecture: $ARCH"
;;
esac
if [ -f "$TARGET_FILE" ]; then
echo "File already exists: $TARGET_FILE"
echo "Skipping download."
else
echo "Downloading $FILENAME ..."
if command -v curl >/dev/null 2>&1; then
if ! curl -L -o "$TARGET_FILE" "$URL"; then
error_exit "Download failed with curl."
fi
elif command -v wget >/dev/null 2>&1; then
if ! wget -O "$TARGET_FILE" "$URL"; then
error_exit "Download failed with wget."
fi
else
error_exit "No curl or wget available."
fi
echo "Download successful."
fi
if [ -d "$TARGET_UNZIPPED_DIR" ]; then
echo "Target directory already exists: $TARGET_UNZIPPED_DIR"
else
echo "Unzipping: $FILENAME"
if ! unzip -q "$TARGET_FILE" -d "$CURRENT_DIR/"; then
error_exit "Unzip failed."
fi
fi
add_path_line() {
local rcfile="$1"
if [ -f "$rcfile" ]; then
if grep -qF "$PATH_LINE" "$rcfile"; then
echo "PATH already configured in $rcfile"
else
echo "" >> "$rcfile"
echo "$PATH_LINE" >> "$rcfile"
echo "PATH added to $rcfile"
fi
fi
}
# 若使用 ~/bin 且 ~/.bin 不在 PATH,则自动加入 .zshrc
if [[ "$USE_BREW" -eq 0 ]]; then
case "$USER_DEFAULT_SHELL" in
*zsh) add_path_line "$HOME/.zshrc" ;;
# 不建议在 macOS 上改动 .bash_profile;若你用 bash 请自行调整
esac
fi
# ---- YY 函数(保持原逻辑)----
if [[ $CHOICE -eq 1 || $CHOICE -eq 2 ]]; then
YY_FUNC='function yy() {
local tmp="$(mktemp -t "yazi-cwd.XXXXXX")"
yazi "$@" --cwd-file="$tmp"
if cwd="$(cat -- "$tmp")" && [ -n "$cwd" ] && [ "$cwd" != "$PWD" ]; then
cd -- "$cwd"
fi
rm -f -- "$tmp"
}'
add_func() {
local rcfile="$1"
if [ -f "$rcfile" ]; then
if grep -F "function yy()" "$rcfile" >/dev/null 2>&1; then
echo "yy function already exists"
else
echo "" >> "$rcfile"
echo "$YY_FUNC" >> "$rcfile"
echo "yy function added to $rcfile"
fi
fi
}
case "$USER_DEFAULT_SHELL" in
*zsh) add_func "$HOME/.zshrc" ;;
*) echo "Unsupported shell for adding function; please add yy manually." ;;
esac
fi
# ---- 主题与示例配置(仅选择 1)----
if [ "$CHOICE" -eq 1 ]; then
CONFIG_DIR="${HOME}/.config/yazi"
mkdir -p "${CONFIG_DIR}"
cat >"${CONFIG_DIR}/init.lua" <<'EOF'
function Linemode:size_and_mtime()
local time = math.floor(self._file.cha.mtime or 0)
if time == 0 then time = ""
elseif os.date("%Y", time) == os.date("%Y") then time = os.date("%b %d %H:%M", time)
else time = os.date("%b %d %Y", time) end
local size = self._file:size()
return string.format("%s %s", size and ya.readable_size(size) or "-", time)
end
EOF
cat >"${CONFIG_DIR}/theme.toml" <<'EOF'
[flavor]
dark = "kanagawa"
EOF
echo "Installing kanagawa theme..."
# 使用找到的 ya 路径调用
if "$YA_BIN" pkg add dangooddd/kanagawa; then
echo "Theme installed successfully!"
else
echo "Warning: Theme installation failed."
fi
fi
echo ""
echo "✓ Installation complete!"
if [[ "$USER_DEFAULT_SHELL" = *zsh* ]]; then
echo "Please run: source ~/.zshrc"
fi