1
0
mirror of https://github.com/tw93/Mole.git synced 2026-02-15 20:25:04 +00:00

refactor: improve pagination robustness and reduce code duplication

This commit is contained in:
Tw93
2025-12-30 14:48:10 +08:00
parent 91351d9440
commit f5dad048ff
2 changed files with 42 additions and 20 deletions

View File

@@ -449,6 +449,27 @@ select_purge_categories() {
# Recalculate items_per_page dynamically to handle window resize # Recalculate items_per_page dynamically to handle window resize
items_per_page=$(_get_items_per_page) items_per_page=$(_get_items_per_page)
# Clamp pagination state to avoid cursor drifting out of view
local max_top_index=0
if [[ $total_items -gt $items_per_page ]]; then
max_top_index=$((total_items - items_per_page))
fi
if [[ $top_index -gt $max_top_index ]]; then
top_index=$max_top_index
fi
if [[ $top_index -lt 0 ]]; then
top_index=0
fi
local visible_count=$((total_items - top_index))
[[ $visible_count -gt $items_per_page ]] && visible_count=$items_per_page
if [[ $cursor_pos -gt $((visible_count - 1)) ]]; then
cursor_pos=$((visible_count - 1))
fi
if [[ $cursor_pos -lt 0 ]]; then
cursor_pos=0
fi
printf "\033[H" printf "\033[H"
# Calculate total size of selected items for header # Calculate total size of selected items for header
local selected_size=0 local selected_size=0
@@ -477,8 +498,6 @@ select_purge_categories() {
IFS=',' read -r -a recent_flags <<< "${PURGE_RECENT_CATEGORIES:-}" IFS=',' read -r -a recent_flags <<< "${PURGE_RECENT_CATEGORIES:-}"
# Calculate visible range # Calculate visible range
local visible_count=$((total_items - top_index))
[[ $visible_count -gt $items_per_page ]] && visible_count=$items_per_page
local end_index=$((top_index + visible_count)) local end_index=$((top_index + visible_count))
# Draw only visible items # Draw only visible items

View File

@@ -21,6 +21,21 @@ opt_msg() {
fi fi
} }
run_launchctl_unload() {
local plist_file="$1"
local need_sudo="${2:-false}"
if [[ "${MOLE_DRY_RUN:-0}" == "1" ]]; then
return 0
fi
if [[ "$need_sudo" == "true" ]]; then
sudo launchctl unload "$plist_file" 2> /dev/null || true
else
launchctl unload "$plist_file" 2> /dev/null || true
fi
}
flush_dns_cache() { flush_dns_cache() {
# Skip actual flush in dry-run mode # Skip actual flush in dry-run mode
if [[ "${MOLE_DRY_RUN:-0}" == "1" ]]; then if [[ "${MOLE_DRY_RUN:-0}" == "1" ]]; then
@@ -354,18 +369,14 @@ opt_startup_items_cleanup() {
fi fi
if [[ "$need_sudo" == "true" ]]; then if [[ "$need_sudo" == "true" ]]; then
if [[ "${MOLE_DRY_RUN:-0}" != "1" ]]; then run_launchctl_unload "$plist_file" "$need_sudo"
sudo launchctl unload "$plist_file" 2> /dev/null || true
fi
if safe_sudo_remove "$plist_file"; then if safe_sudo_remove "$plist_file"; then
((broken_count++)) ((broken_count++))
else else
echo -e " ${YELLOW}!${NC} Failed to remove (sudo) $plist_file" echo -e " ${YELLOW}!${NC} Failed to remove (sudo) $plist_file"
fi fi
else else
if [[ "${MOLE_DRY_RUN:-0}" != "1" ]]; then run_launchctl_unload "$plist_file" "$need_sudo"
launchctl unload "$plist_file" 2> /dev/null || true
fi
if safe_remove "$plist_file" true; then if safe_remove "$plist_file" true; then
((broken_count++)) ((broken_count++))
else else
@@ -443,9 +454,7 @@ opt_startup_items_cleanup() {
debug_log "Removing orphaned helper (app not found, program missing): $plist_file" debug_log "Removing orphaned helper (app not found, program missing): $plist_file"
if [[ "$need_sudo" == "true" ]]; then if [[ "$need_sudo" == "true" ]]; then
if [[ "${MOLE_DRY_RUN:-0}" != "1" ]]; then run_launchctl_unload "$plist_file" "$need_sudo"
sudo launchctl unload "$plist_file" 2> /dev/null || true
fi
# remove the plist # remove the plist
safe_sudo_remove "$plist_file" safe_sudo_remove "$plist_file"
@@ -453,9 +462,7 @@ opt_startup_items_cleanup() {
((broken_count++)) ((broken_count++))
opt_msg "Removed orphaned helper: $(basename "$plist_file" .plist)" opt_msg "Removed orphaned helper: $(basename "$plist_file" .plist)"
else else
if [[ "${MOLE_DRY_RUN:-0}" != "1" ]]; then run_launchctl_unload "$plist_file" "$need_sudo"
launchctl unload "$plist_file" 2> /dev/null || true
fi
safe_remove "$plist_file" true safe_remove "$plist_file" true
((broken_count++)) ((broken_count++))
opt_msg "Removed orphaned helper: $(basename "$plist_file" .plist)" opt_msg "Removed orphaned helper: $(basename "$plist_file" .plist)"
@@ -471,18 +478,14 @@ opt_startup_items_cleanup() {
fi fi
if [[ "$need_sudo" == "true" ]]; then if [[ "$need_sudo" == "true" ]]; then
if [[ "${MOLE_DRY_RUN:-0}" != "1" ]]; then run_launchctl_unload "$plist_file" "$need_sudo"
sudo launchctl unload "$plist_file" 2> /dev/null || true
fi
if safe_sudo_remove "$plist_file"; then if safe_sudo_remove "$plist_file"; then
((broken_count++)) ((broken_count++))
else else
echo -e " ${YELLOW}!${NC} Failed to remove (sudo) $plist_file" echo -e " ${YELLOW}!${NC} Failed to remove (sudo) $plist_file"
fi fi
else else
if [[ "${MOLE_DRY_RUN:-0}" != "1" ]]; then run_launchctl_unload "$plist_file" "$need_sudo"
launchctl unload "$plist_file" 2> /dev/null || true
fi
if safe_remove "$plist_file" true; then if safe_remove "$plist_file" true; then
((broken_count++)) ((broken_count++))
else else