mirror of
https://github.com/tw93/Mole.git
synced 2026-02-04 11:31:46 +00:00
Apply scan cache to simplify
This commit is contained in:
954
bin/optimize.sh
954
bin/optimize.sh
File diff suppressed because it is too large
Load Diff
@@ -100,42 +100,18 @@ format_last_used_summary() {
|
||||
|
||||
# Scan applications and collect information
|
||||
scan_applications() {
|
||||
# Cache configuration
|
||||
# Simplified cache: only check timestamp (24h TTL)
|
||||
local cache_dir="$HOME/.cache/mole"
|
||||
local cache_file="$cache_dir/app_scan_cache"
|
||||
local cache_meta="$cache_dir/app_scan_meta"
|
||||
local cache_dir_mtime="$cache_dir/app_dir_mtime"
|
||||
local cache_ttl=86400 # 24 hours cache validity (app count change will trigger refresh)
|
||||
local cache_ttl=86400 # 24 hours
|
||||
|
||||
mkdir -p "$cache_dir" 2> /dev/null
|
||||
|
||||
# Get modification time of app directories to detect new installations
|
||||
local sys_app_mtime
|
||||
sys_app_mtime=$(stat -f%m "/Applications" 2> /dev/null || echo "0")
|
||||
local user_app_mtime
|
||||
user_app_mtime=$(stat -f%m "$HOME/Applications" 2> /dev/null || echo "0")
|
||||
local combined_mtime=$((sys_app_mtime + user_app_mtime))
|
||||
|
||||
# Quick count of current apps (system + user directories)
|
||||
local current_app_count
|
||||
current_app_count=$(
|
||||
(
|
||||
find /Applications -name "*.app" -maxdepth 1 2> /dev/null
|
||||
find ~/Applications -name "*.app" -maxdepth 1 2> /dev/null
|
||||
) | wc -l | tr -d ' '
|
||||
)
|
||||
|
||||
# Check if cache is valid unless explicitly disabled
|
||||
if [[ -f "$cache_file" && -f "$cache_meta" && -f "$cache_dir_mtime" ]]; then
|
||||
local cache_age=$(($(date +%s) - $(stat -f%m "$cache_file" 2> /dev/null || echo 0)))
|
||||
local cached_app_count
|
||||
cached_app_count=$(cat "$cache_meta" 2> /dev/null || echo "0")
|
||||
local cached_dir_mtime
|
||||
cached_dir_mtime=$(cat "$cache_dir_mtime" 2> /dev/null || echo "0")
|
||||
|
||||
# Cache is valid if: age < TTL AND app count matches AND directory not modified
|
||||
if [[ $cache_age -lt $cache_ttl && "$cached_app_count" == "$current_app_count" && "$cached_dir_mtime" == "$combined_mtime" ]]; then
|
||||
# Silent - cache hit, return immediately without any output
|
||||
# Check if cache exists and is fresh
|
||||
if [[ -f "$cache_file" ]]; then
|
||||
local cache_age=$(($(date +%s) - $(stat -f%m "$cache_file" 2> /dev/null || echo 86401)))
|
||||
if [[ $cache_age -lt $cache_ttl ]]; then
|
||||
# Cache hit - return immediately
|
||||
echo "$cache_file"
|
||||
return 0
|
||||
fi
|
||||
@@ -381,12 +357,10 @@ scan_applications() {
|
||||
}
|
||||
rm -f "$temp_file"
|
||||
|
||||
# Update cache with app count metadata and directory modification time
|
||||
# Save to cache (simplified - no metadata)
|
||||
cp "${temp_file}.sorted" "$cache_file" 2> /dev/null || true
|
||||
echo "$current_app_count" > "$cache_meta" 2> /dev/null || true
|
||||
echo "$combined_mtime" > "$cache_dir_mtime" 2> /dev/null || true
|
||||
|
||||
# Verify sorted file exists before returning
|
||||
# Return sorted file
|
||||
if [[ -f "${temp_file}.sorted" ]]; then
|
||||
echo "${temp_file}.sorted"
|
||||
else
|
||||
@@ -613,20 +587,13 @@ main() {
|
||||
# Hide cursor during operation
|
||||
hide_cursor
|
||||
|
||||
# Quick cache validity check first (minimal I/O)
|
||||
local cache_dir="$HOME/.cache/mole"
|
||||
local cache_file="$cache_dir/app_scan_cache"
|
||||
local cache_meta="$cache_dir/app_scan_meta"
|
||||
local cache_ttl=86400
|
||||
# Simplified: always check if we need alt screen for scanning
|
||||
# (scan_applications handles cache internally)
|
||||
local needs_scanning=true
|
||||
|
||||
# Fast preliminary check: cache exists and not expired
|
||||
if [[ -f "$cache_file" && -f "$cache_meta" ]]; then
|
||||
local cache_file="$HOME/.cache/mole/app_scan_cache"
|
||||
if [[ -f "$cache_file" ]]; then
|
||||
local cache_age=$(($(date +%s) - $(stat -f%m "$cache_file" 2> /dev/null || echo 86401)))
|
||||
if [[ $cache_age -lt $cache_ttl ]]; then
|
||||
# Cache age is OK, now check app count (delegate to scan_applications)
|
||||
needs_scanning=false
|
||||
fi
|
||||
[[ $cache_age -lt 86400 ]] && needs_scanning=false
|
||||
fi
|
||||
|
||||
# Only enter alt screen if we need scanning (shows progress)
|
||||
|
||||
@@ -9,12 +9,12 @@ get_memory_info() {
|
||||
local total_bytes used_gb total_gb
|
||||
|
||||
# Total memory
|
||||
total_bytes=$(sysctl -n hw.memsize 2>/dev/null || echo "0")
|
||||
total_bytes=$(sysctl -n hw.memsize 2> /dev/null || echo "0")
|
||||
total_gb=$(awk "BEGIN {printf \"%.2f\", $total_bytes / (1024*1024*1024)}")
|
||||
|
||||
# Used memory from vm_stat
|
||||
local vm_output active wired compressed page_size
|
||||
vm_output=$(vm_stat 2>/dev/null || echo "")
|
||||
vm_output=$(vm_stat 2> /dev/null || echo "")
|
||||
page_size=4096
|
||||
|
||||
active=$(echo "$vm_output" | awk '/Pages active:/ {print $NF}' | tr -d '.')
|
||||
@@ -25,7 +25,7 @@ get_memory_info() {
|
||||
wired=${wired:-0}
|
||||
compressed=${compressed:-0}
|
||||
|
||||
local used_bytes=$(( (active + wired + compressed) * page_size ))
|
||||
local used_bytes=$(((active + wired + compressed) * page_size))
|
||||
used_gb=$(awk "BEGIN {printf \"%.2f\", $used_bytes / (1024*1024*1024)}")
|
||||
|
||||
echo "$used_gb $total_gb"
|
||||
@@ -36,7 +36,7 @@ get_disk_info() {
|
||||
local home="${HOME:-/}"
|
||||
local df_output total_gb used_gb used_percent
|
||||
|
||||
df_output=$(df -k "$home" 2>/dev/null | tail -1)
|
||||
df_output=$(df -k "$home" 2> /dev/null | tail -1)
|
||||
|
||||
local total_kb used_kb
|
||||
total_kb=$(echo "$df_output" | awk '{print $2}')
|
||||
@@ -53,7 +53,7 @@ get_disk_info() {
|
||||
get_uptime_days() {
|
||||
local boot_output boot_time uptime_days
|
||||
|
||||
boot_output=$(sysctl -n kern.boottime 2>/dev/null || echo "")
|
||||
boot_output=$(sysctl -n kern.boottime 2> /dev/null || echo "")
|
||||
boot_time=$(echo "$boot_output" | sed -n 's/.*sec = \([0-9]*\).*/\1/p')
|
||||
|
||||
if [[ -n "$boot_time" ]]; then
|
||||
@@ -71,7 +71,7 @@ get_uptime_days() {
|
||||
dir_size_kb() {
|
||||
local path="$1"
|
||||
[[ ! -e "$path" ]] && echo "0" && return
|
||||
du -sk "$path" 2>/dev/null | awk '{print $1}' || echo "0"
|
||||
du -sk "$path" 2> /dev/null | awk '{print $1}' || echo "0"
|
||||
}
|
||||
|
||||
# Format size from KB
|
||||
@@ -101,7 +101,7 @@ check_startup_items() {
|
||||
)
|
||||
|
||||
for dir in "${dirs[@]}"; do
|
||||
[[ -d "$dir" ]] && count=$((count + $(ls -1 "$dir" 2>/dev/null | wc -l)))
|
||||
[[ -d "$dir" ]] && count=$((count + $(ls -1 "$dir" 2> /dev/null | wc -l)))
|
||||
done
|
||||
|
||||
if [[ $count -gt 5 ]]; then
|
||||
@@ -160,7 +160,7 @@ check_swap_cleanup() {
|
||||
local file
|
||||
|
||||
for file in /private/var/vm/swapfile*; do
|
||||
[[ -f "$file" ]] && total_kb=$((total_kb + $(stat -f%z "$file" 2>/dev/null || echo 0) / 1024))
|
||||
[[ -f "$file" ]] && total_kb=$((total_kb + $(stat -f%z "$file" 2> /dev/null || echo 0) / 1024))
|
||||
done
|
||||
|
||||
if [[ $total_kb -gt 0 ]]; then
|
||||
@@ -171,13 +171,13 @@ check_swap_cleanup() {
|
||||
|
||||
# Check local snapshots
|
||||
check_local_snapshots() {
|
||||
command -v tmutil >/dev/null 2>&1 || return
|
||||
command -v tmutil > /dev/null 2>&1 || return
|
||||
|
||||
local snapshots
|
||||
snapshots=$(tmutil listlocalsnapshots / 2>/dev/null || echo "")
|
||||
snapshots=$(tmutil listlocalsnapshots / 2> /dev/null || echo "")
|
||||
|
||||
local count
|
||||
count=$(echo "$snapshots" | grep -c "com.apple.TimeMachine" 2>/dev/null)
|
||||
count=$(echo "$snapshots" | grep -c "com.apple.TimeMachine" 2> /dev/null)
|
||||
count=$(echo "$count" | tr -d ' \n')
|
||||
count=${count:-0}
|
||||
[[ "$count" =~ ^[0-9]+$ ]] && [[ $count -gt 0 ]] && echo "local_snapshots|Local Snapshots|${count} APFS local snapshots detected|true"
|
||||
@@ -237,13 +237,20 @@ EOF
|
||||
|
||||
# Conditional items
|
||||
local item
|
||||
item=$(check_startup_items || true); [[ -n "$item" ]] && items+=("$item")
|
||||
item=$(check_cache_refresh || true); [[ -n "$item" ]] && items+=("$item")
|
||||
item=$(check_mail_downloads || true); [[ -n "$item" ]] && items+=("$item")
|
||||
item=$(check_saved_state || true); [[ -n "$item" ]] && items+=("$item")
|
||||
item=$(check_swap_cleanup || true); [[ -n "$item" ]] && items+=("$item")
|
||||
item=$(check_local_snapshots || true); [[ -n "$item" ]] && items+=("$item")
|
||||
item=$(check_developer_cleanup || true); [[ -n "$item" ]] && items+=("$item")
|
||||
item=$(check_startup_items || true)
|
||||
[[ -n "$item" ]] && items+=("$item")
|
||||
item=$(check_cache_refresh || true)
|
||||
[[ -n "$item" ]] && items+=("$item")
|
||||
item=$(check_mail_downloads || true)
|
||||
[[ -n "$item" ]] && items+=("$item")
|
||||
item=$(check_saved_state || true)
|
||||
[[ -n "$item" ]] && items+=("$item")
|
||||
item=$(check_swap_cleanup || true)
|
||||
[[ -n "$item" ]] && items+=("$item")
|
||||
item=$(check_local_snapshots || true)
|
||||
[[ -n "$item" ]] && items+=("$item")
|
||||
item=$(check_developer_cleanup || true)
|
||||
[[ -n "$item" ]] && items+=("$item")
|
||||
|
||||
# Output items as JSON
|
||||
local first=true
|
||||
|
||||
@@ -67,13 +67,13 @@ echo -e "${YELLOW}4. Checking code optimizations...${NC}"
|
||||
OPTIMIZATION_SCORE=0
|
||||
TOTAL_CHECKS=0
|
||||
|
||||
# Check 1: Simplified keyboard input (0.05s timeout)
|
||||
# Check 1: Keyboard input handling (restored to 1s for reliability)
|
||||
((TOTAL_CHECKS++))
|
||||
if grep -q "read -r -s -n 1 -t 0.05" lib/common.sh; then
|
||||
echo -e "${GREEN} ✓ Keyboard timeout optimized (0.05s)${NC}"
|
||||
if grep -q "read -r -s -n 1 -t 1" lib/common.sh; then
|
||||
echo -e "${GREEN} ✓ Keyboard timeout properly configured (1s)${NC}"
|
||||
((OPTIMIZATION_SCORE++))
|
||||
else
|
||||
echo -e "${YELLOW} ⚠ Keyboard timeout not optimized${NC}"
|
||||
echo -e "${YELLOW} ⚠ Keyboard timeout may be misconfigured${NC}"
|
||||
fi
|
||||
|
||||
# Check 2: Single-pass drain_pending_input
|
||||
|
||||
Reference in New Issue
Block a user