1
0
mirror of https://github.com/tw93/Mole.git synced 2026-02-15 20:25:04 +00:00
This commit is contained in:
Tw93
2025-11-27 07:51:38 +09:00
parent 36341369b6
commit 7c4c106558
5 changed files with 37 additions and 34 deletions

View File

@@ -108,12 +108,12 @@ show_system_health() {
local health_json="$1" local health_json="$1"
# Parse system health using jq with fallback to 0 # Parse system health using jq with fallback to 0
local mem_used=$(echo "$health_json" | jq -r '.memory_used_gb // 0' 2>/dev/null || echo "0") local mem_used=$(echo "$health_json" | jq -r '.memory_used_gb // 0' 2> /dev/null || echo "0")
local mem_total=$(echo "$health_json" | jq -r '.memory_total_gb // 0' 2>/dev/null || echo "0") local mem_total=$(echo "$health_json" | jq -r '.memory_total_gb // 0' 2> /dev/null || echo "0")
local disk_used=$(echo "$health_json" | jq -r '.disk_used_gb // 0' 2>/dev/null || echo "0") local disk_used=$(echo "$health_json" | jq -r '.disk_used_gb // 0' 2> /dev/null || echo "0")
local disk_total=$(echo "$health_json" | jq -r '.disk_total_gb // 0' 2>/dev/null || echo "0") local disk_total=$(echo "$health_json" | jq -r '.disk_total_gb // 0' 2> /dev/null || echo "0")
local disk_percent=$(echo "$health_json" | jq -r '.disk_used_percent // 0' 2>/dev/null || echo "0") local disk_percent=$(echo "$health_json" | jq -r '.disk_used_percent // 0' 2> /dev/null || echo "0")
local uptime=$(echo "$health_json" | jq -r '.uptime_days // 0' 2>/dev/null || echo "0") local uptime=$(echo "$health_json" | jq -r '.uptime_days // 0' 2> /dev/null || echo "0")
# Ensure all values are numeric (fallback to 0) # Ensure all values are numeric (fallback to 0)
mem_used=${mem_used:-0} mem_used=${mem_used:-0}
@@ -381,7 +381,7 @@ main() {
fi fi
# Validate JSON before proceeding # Validate JSON before proceeding
if ! echo "$health_json" | jq empty 2>/dev/null; then if ! echo "$health_json" | jq empty 2> /dev/null; then
if [[ -t 1 ]]; then if [[ -t 1 ]]; then
stop_inline_spinner stop_inline_spinner
fi fi

View File

@@ -110,7 +110,7 @@ scan_applications() {
# Check if cache exists and is fresh # Check if cache exists and is fresh
if [[ -f "$cache_file" ]]; then if [[ -f "$cache_file" ]]; then
local cache_age=$(($(date +%s) - $(get_file_mtime "$cache_file"))) local cache_age=$(($(date +%s) - $(get_file_mtime "$cache_file")))
[[ $cache_age -eq $(date +%s) ]] && cache_age=86401 # Handle missing file [[ $cache_age -eq $(date +%s) ]] && cache_age=86401 # Handle missing file
if [[ $cache_age -lt $cache_ttl ]]; then if [[ $cache_age -lt $cache_ttl ]]; then
# Cache hit - return immediately # Cache hit - return immediately
echo "$cache_file" echo "$cache_file"
@@ -633,7 +633,7 @@ main() {
local cache_file="$HOME/.cache/mole/app_scan_cache" local cache_file="$HOME/.cache/mole/app_scan_cache"
if [[ -f "$cache_file" ]]; then if [[ -f "$cache_file" ]]; then
local cache_age=$(($(date +%s) - $(get_file_mtime "$cache_file"))) local cache_age=$(($(date +%s) - $(get_file_mtime "$cache_file")))
[[ $cache_age -eq $(date +%s) ]] && cache_age=86401 # Handle missing file [[ $cache_age -eq $(date +%s) ]] && cache_age=86401 # Handle missing file
[[ $cache_age -lt 86400 ]] && needs_scanning=false [[ $cache_age -lt 86400 ]] && needs_scanning=false
fi fi

View File

@@ -48,17 +48,17 @@ readonly STAT_BSD="/usr/bin/stat"
get_file_size() { get_file_size() {
local file="$1" local file="$1"
$STAT_BSD -f%z "$file" 2>/dev/null || echo 0 $STAT_BSD -f%z "$file" 2> /dev/null || echo 0
} }
get_file_mtime() { get_file_mtime() {
local file="$1" local file="$1"
$STAT_BSD -f%m "$file" 2>/dev/null || echo 0 $STAT_BSD -f%m "$file" 2> /dev/null || echo 0
} }
get_file_owner() { get_file_owner() {
local file="$1" local file="$1"
$STAT_BSD -f%Su "$file" 2>/dev/null || echo "" $STAT_BSD -f%Su "$file" 2> /dev/null || echo ""
} }
# Security and Path Validation Functions # Security and Path Validation Functions
@@ -672,9 +672,9 @@ update_via_homebrew() {
local brew_tmp_file="" local brew_tmp_file=""
local brew_exit_file="" local brew_exit_file=""
cleanup_brew_update() { cleanup_brew_update() {
if [[ -n "$brew_pid" ]] && kill -0 "$brew_pid" 2>/dev/null; then if [[ -n "$brew_pid" ]] && kill -0 "$brew_pid" 2> /dev/null; then
kill -TERM "$brew_pid" 2>/dev/null || true kill -TERM "$brew_pid" 2> /dev/null || true
wait "$brew_pid" 2>/dev/null || true wait "$brew_pid" 2> /dev/null || true
fi fi
[[ -n "$brew_tmp_file" ]] && rm -f "$brew_tmp_file" [[ -n "$brew_tmp_file" ]] && rm -f "$brew_tmp_file"
[[ -n "$brew_exit_file" ]] && rm -f "$brew_exit_file" [[ -n "$brew_exit_file" ]] && rm -f "$brew_exit_file"
@@ -696,7 +696,10 @@ update_via_homebrew() {
# Redirect brew output to temp file to avoid interfering with spinner # Redirect brew output to temp file to avoid interfering with spinner
# Store exit code in a separate file to avoid wait issues with zsh # Store exit code in a separate file to avoid wait issues with zsh
brew_exit_file="${brew_tmp_file}.exit" brew_exit_file="${brew_tmp_file}.exit"
(brew update > "$brew_tmp_file" 2>&1 </dev/null; echo $? > "$brew_exit_file") & (
brew update > "$brew_tmp_file" 2>&1 < /dev/null
echo $? > "$brew_exit_file"
) &
brew_pid=$! brew_pid=$!
local elapsed=0 local elapsed=0
@@ -723,7 +726,7 @@ update_via_homebrew() {
# Get brew update exit code from file instead of wait # Get brew update exit code from file instead of wait
local brew_exit=0 local brew_exit=0
if [[ -f "$brew_exit_file" ]]; then if [[ -f "$brew_exit_file" ]]; then
brew_exit=$(cat "$brew_exit_file" 2>/dev/null || echo "0") brew_exit=$(cat "$brew_exit_file" 2> /dev/null || echo "0")
fi fi
rm -f "$brew_exit_file" rm -f "$brew_exit_file"
@@ -736,7 +739,7 @@ update_via_homebrew() {
local update_output="" local update_output=""
if [[ -f "$brew_tmp_file" ]]; then if [[ -f "$brew_tmp_file" ]]; then
update_output=$(cat "$brew_tmp_file" 2>/dev/null) update_output=$(cat "$brew_tmp_file" 2> /dev/null)
fi fi
# Check for errors in output (brew update may return 0 even on failure) # Check for errors in output (brew update may return 0 even on failure)

View File

@@ -220,7 +220,7 @@ opt_mail_downloads() {
local deleted=0 local deleted=0
for target_path in "${mail_dirs[@]}"; do for target_path in "${mail_dirs[@]}"; do
if [[ -d "$target_path" ]]; then if [[ -d "$target_path" ]]; then
deleted=$((deleted + $(find "$target_path" -type f -mtime +30 -delete -print 2>/dev/null | wc -l | tr -d ' '))) deleted=$((deleted + $(find "$target_path" -type f -mtime +30 -delete -print 2> /dev/null | wc -l | tr -d ' ')))
fi fi
done done
@@ -243,7 +243,7 @@ opt_saved_state_cleanup() {
# Only delete states older than 7 days (safer - won't lose recent work) # Only delete states older than 7 days (safer - won't lose recent work)
local deleted=0 local deleted=0
deleted=$(find "$state_dir" -type d -name "*.savedState" -mtime +7 -exec rm -rf {} \; -print 2>/dev/null | wc -l | tr -d ' ') deleted=$(find "$state_dir" -type d -name "*.savedState" -mtime +7 -exec rm -rf {} \; -print 2> /dev/null | wc -l | tr -d ' ')
if [[ $deleted -gt 0 ]]; then if [[ $deleted -gt 0 ]]; then
echo -e "${GREEN}${ICON_SUCCESS}${NC} Removed $deleted old saved state(s)" echo -e "${GREEN}${ICON_SUCCESS}${NC} Removed $deleted old saved state(s)"

View File

@@ -10,7 +10,7 @@ get_memory_info() {
# Total memory # 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)}" 2>/dev/null || echo "0") total_gb=$(awk "BEGIN {printf \"%.2f\", $total_bytes / (1024*1024*1024)}" 2> /dev/null || echo "0")
[[ -z "$total_gb" || "$total_gb" == "" ]] && total_gb="0" [[ -z "$total_gb" || "$total_gb" == "" ]] && total_gb="0"
# Used memory from vm_stat # Used memory from vm_stat
@@ -18,16 +18,16 @@ get_memory_info() {
vm_output=$(vm_stat 2> /dev/null || echo "") vm_output=$(vm_stat 2> /dev/null || echo "")
page_size=4096 page_size=4096
active=$(echo "$vm_output" | awk '/Pages active:/ {print $NF}' | tr -d '.' 2>/dev/null || echo "0") active=$(echo "$vm_output" | awk '/Pages active:/ {print $NF}' | tr -d '.' 2> /dev/null || echo "0")
wired=$(echo "$vm_output" | awk '/Pages wired down:/ {print $NF}' | tr -d '.' 2>/dev/null || echo "0") wired=$(echo "$vm_output" | awk '/Pages wired down:/ {print $NF}' | tr -d '.' 2> /dev/null || echo "0")
compressed=$(echo "$vm_output" | awk '/Pages occupied by compressor:/ {print $NF}' | tr -d '.' 2>/dev/null || echo "0") compressed=$(echo "$vm_output" | awk '/Pages occupied by compressor:/ {print $NF}' | tr -d '.' 2> /dev/null || echo "0")
active=${active:-0} active=${active:-0}
wired=${wired:-0} wired=${wired:-0}
compressed=${compressed:-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)}" 2>/dev/null || echo "0") used_gb=$(awk "BEGIN {printf \"%.2f\", $used_bytes / (1024*1024*1024)}" 2> /dev/null || echo "0")
[[ -z "$used_gb" || "$used_gb" == "" ]] && used_gb="0" [[ -z "$used_gb" || "$used_gb" == "" ]] && used_gb="0"
echo "$used_gb $total_gb" echo "$used_gb $total_gb"
@@ -41,16 +41,16 @@ get_disk_info() {
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 local total_kb used_kb
total_kb=$(echo "$df_output" | awk '{print $2}' 2>/dev/null || echo "0") total_kb=$(echo "$df_output" | awk '{print $2}' 2> /dev/null || echo "0")
used_kb=$(echo "$df_output" | awk '{print $3}' 2>/dev/null || echo "0") used_kb=$(echo "$df_output" | awk '{print $3}' 2> /dev/null || echo "0")
total_kb=${total_kb:-0} total_kb=${total_kb:-0}
used_kb=${used_kb:-0} used_kb=${used_kb:-0}
[[ "$total_kb" == "0" ]] && total_kb=1 # Avoid division by zero [[ "$total_kb" == "0" ]] && total_kb=1 # Avoid division by zero
total_gb=$(awk "BEGIN {printf \"%.2f\", $total_kb / (1024*1024)}" 2>/dev/null || echo "0") total_gb=$(awk "BEGIN {printf \"%.2f\", $total_kb / (1024*1024)}" 2> /dev/null || echo "0")
used_gb=$(awk "BEGIN {printf \"%.2f\", $used_kb / (1024*1024)}" 2>/dev/null || echo "0") used_gb=$(awk "BEGIN {printf \"%.2f\", $used_kb / (1024*1024)}" 2> /dev/null || echo "0")
used_percent=$(awk "BEGIN {printf \"%.1f\", ($used_kb / $total_kb) * 100}" 2>/dev/null || echo "0") used_percent=$(awk "BEGIN {printf \"%.1f\", ($used_kb / $total_kb) * 100}" 2> /dev/null || echo "0")
[[ -z "$total_gb" || "$total_gb" == "" ]] && total_gb="0" [[ -z "$total_gb" || "$total_gb" == "" ]] && total_gb="0"
[[ -z "$used_gb" || "$used_gb" == "" ]] && used_gb="0" [[ -z "$used_gb" || "$used_gb" == "" ]] && used_gb="0"
@@ -64,12 +64,12 @@ get_uptime_days() {
local boot_output boot_time 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' 2>/dev/null || echo "") boot_time=$(echo "$boot_output" | sed -n 's/.*sec = \([0-9]*\).*/\1/p' 2> /dev/null || echo "")
if [[ -n "$boot_time" && "$boot_time" =~ ^[0-9]+$ ]]; then if [[ -n "$boot_time" && "$boot_time" =~ ^[0-9]+$ ]]; then
local now=$(date +%s 2>/dev/null || echo "0") local now=$(date +%s 2> /dev/null || echo "0")
local uptime_sec=$((now - boot_time)) local uptime_sec=$((now - boot_time))
uptime_days=$(awk "BEGIN {printf \"%.1f\", $uptime_sec / 86400}" 2>/dev/null || echo "0") uptime_days=$(awk "BEGIN {printf \"%.1f\", $uptime_sec / 86400}" 2> /dev/null || echo "0")
else else
uptime_days="0" uptime_days="0"
fi fi