1
0
mirror of https://github.com/tw93/Mole.git synced 2026-03-22 20:15:07 +00:00

🐛 Update animation and update logic

This commit is contained in:
Tw93
2025-10-03 21:36:05 +08:00
parent d01405085d
commit 3f66c658be

89
mole
View File

@@ -25,54 +25,29 @@ MOLE_TAGLINE="can dig deep to clean your Mac."
# Check for updates (non-blocking, cached) # Check for updates (non-blocking, cached)
check_for_updates() { check_for_updates() {
local cache_dir="$HOME/.cache/mole" local cache="$HOME/.cache/mole/version_check"
local version_cache="$cache_dir/version_check"
local check_interval=86400 # Check once per day (24 hours)
mkdir -p "$cache_dir" 2>/dev/null mkdir -p "$(dirname "$cache")" 2>/dev/null
# Check if we should run version check (based on cache age) # Show cached message if less than 24 hours old
if [[ -f "$version_cache" ]]; then if [[ -f "$cache" ]]; then
local cache_age=$(($(date +%s) - $(stat -f%m "$version_cache" 2>/dev/null || echo 0))) local age=$(($(date +%s) - $(stat -f%m "$cache" 2>/dev/null || echo 0)))
if [[ $cache_age -lt $check_interval ]]; then [[ $age -lt 86400 ]] && [[ -s "$cache" ]] && cat "$cache" && return
# Cache is still fresh, show cached message if exists
if [[ -s "$version_cache" ]]; then
cat "$version_cache"
fi
return 0
fi
fi fi
# Run version check in background (non-blocking) # Background version check
( (
local latest_version="" local latest=$(curl -fsSL --connect-timeout 2 --max-time 3 \
local timeout=3 # 3 second timeout for version check "https://raw.githubusercontent.com/tw93/mole/main/mole" 2>/dev/null | \
grep '^VERSION=' | head -1 | sed 's/VERSION="\(.*\)"/\1/')
# Try to fetch latest version from GitHub with timeout if [[ -n "$latest" && "$VERSION" != "$latest" && "$(printf '%s\n' "$VERSION" "$latest" | sort -V | head -1)" == "$VERSION" ]]; then
if command -v curl >/dev/null 2>&1; then echo -e "${YELLOW}📢 New version ${GREEN}${latest}${YELLOW} available (current: ${VERSION})\n Run ${GREEN}mole update${YELLOW} to upgrade${NC}" | tee "$cache"
latest_version=$(curl -fsSL --connect-timeout 2 --max-time $timeout \
"https://api.github.com/repos/tw93/mole/releases/latest" 2>/dev/null | \
grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/' | sed 's/^[Vv]//')
elif command -v wget >/dev/null 2>&1; then
latest_version=$(wget -qO- --timeout=$timeout --tries=1 \
"https://api.github.com/repos/tw93/mole/releases/latest" 2>/dev/null | \
grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/' | sed 's/^[Vv]//')
fi
# Compare versions if fetch succeeded - only notify if latest is newer
if [[ -n "$latest_version" ]] && [[ "$(printf '%s\n' "$VERSION" "$latest_version" | sort -V | head -n1)" == "$VERSION" ]] && [[ "$VERSION" != "$latest_version" ]]; then
local msg="${YELLOW}📢 New version available: ${GREEN}${latest_version}${YELLOW} (current: ${VERSION})${NC}\n Run ${GREEN}mole update${YELLOW} to upgrade${NC}"
echo -e "$msg" > "$version_cache"
echo -e "$msg"
else else
echo "" > "$version_cache" echo -n > "$cache"
fi fi
touch "$cache" 2>/dev/null
# Touch cache file to update timestamp
touch "$version_cache" 2>/dev/null
) & ) &
# Don't wait for background check
disown 2>/dev/null || true disown 2>/dev/null || true
} }
@@ -124,7 +99,6 @@ EOF
done done
printf '\n' printf '\n'
printf "%s\n" "${BLUE}Ready to dig deep...${NC}"
sleep 0.5 sleep 0.5
printf '\033[2J\033[H' printf '\033[2J\033[H'
@@ -237,33 +211,16 @@ show_main_menu() {
# Interactive main menu loop # Interactive main menu loop
interactive_main_menu() { interactive_main_menu() {
maybe_show_intro() { # Show intro animation only once per terminal tab
if [[ ! -t 1 ]]; then
return
fi
local tty_name
tty_name=$(tty 2>/dev/null || echo "")
if [[ -n "$tty_name" ]]; then
local sanitized
sanitized=$(echo "$tty_name" | tr -c '[:alnum:]_' '_')
local flag_file="/tmp/mole_intro_${sanitized}"
if [[ -f "$flag_file" ]]; then
return
fi
animate_mole_intro
touch "$flag_file" 2>/dev/null || true
else
if [[ -z "${MOLE_INTRO_SHOWN:-}" ]]; then
animate_mole_intro
export MOLE_INTRO_SHOWN=1
fi
fi
}
if [[ -t 1 ]]; then if [[ -t 1 ]]; then
animate_mole_intro local tty_name=$(tty 2>/dev/null || echo "")
if [[ -n "$tty_name" ]]; then
local flag_file="/tmp/mole_intro_$(echo "$tty_name" | tr -c '[:alnum:]_' '_')"
if [[ ! -f "$flag_file" ]]; then
animate_mole_intro
touch "$flag_file" 2>/dev/null || true
fi
fi
fi fi
local current_option=1 local current_option=1
local first_draw=true local first_draw=true