From e12a40f6bf12de391ad9a07b285b157798a0e37e Mon Sep 17 00:00:00 2001 From: tw93 Date: Fri, 30 Jan 2026 18:16:31 +0800 Subject: [PATCH] fix: use \033[2K to fully clear spinner lines and prevent text remnants Fixes text remnants and extra blank lines when spinner messages change. Issues fixed: 1. Text remnants when switching from longer to shorter messages (e.g., 'Cleaning...ems...') 2. Extra blank lines appearing after section headers Root causes: - \033[K only clears from cursor to end of line, leaving remnants when new messages are shorter - stop_section_spinner was clearing lines even when no spinner was running Changes: - lib/core/base.sh: - Changed stop_section_spinner(), safe_clear_line(), safe_clear_lines() to use \033[2K - Added guard in stop_section_spinner to only clear when spinner is actually running - lib/core/ui.sh: - Clear line once when starting spinner (before loop) to ensure clean start - Normal spinner rotation uses \r without clearing (performance optimization) Performance: Line clearing happens only once per spinner start, not on every loop iteration. Fixes #390 --- bin/clean.sh | 4 ---- lib/core/base.sh | 13 ++++++++----- lib/core/ui.sh | 3 +++ 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/bin/clean.sh b/bin/clean.sh index dede154..6d11602 100755 --- a/bin/clean.sh +++ b/bin/clean.sh @@ -142,10 +142,6 @@ cleanup() { stop_inline_spinner 2> /dev/null || true - if [[ -t 1 ]]; then - printf "\r\033[K" >&2 || true - fi - cleanup_temp_files stop_sudo_session diff --git a/lib/core/base.sh b/lib/core/base.sh index 0f0170c..8622f65 100644 --- a/lib/core/base.sh +++ b/lib/core/base.sh @@ -626,9 +626,12 @@ start_section_spinner() { # Stop spinner and clear the line # Usage: stop_section_spinner stop_section_spinner() { - stop_inline_spinner 2> /dev/null || true - if [[ -t 1 ]]; then - echo -ne "\r\033[K" >&2 || true + # Only clear line if spinner was actually running + if [[ -n "${INLINE_SPINNER_PID:-}" ]]; then + stop_inline_spinner 2> /dev/null || true + if [[ -t 1 ]]; then + echo -ne "\r\033[2K" >&2 || true + fi fi } @@ -646,7 +649,7 @@ safe_clear_lines() { # Clear lines one by one (more reliable than multi-line sequences) local i for ((i = 0; i < lines; i++)); do - printf "\033[1A\r\033[K" > "$tty_device" 2> /dev/null || return 1 + printf "\033[1A\r\033[2K" > "$tty_device" 2> /dev/null || return 1 done return 0 @@ -660,7 +663,7 @@ safe_clear_line() { # Use centralized ANSI support check is_ansi_supported 2> /dev/null || return 1 - printf "\r\033[K" > "$tty_device" 2> /dev/null || return 1 + printf "\r\033[2K" > "$tty_device" 2> /dev/null || return 1 return 0 } diff --git a/lib/core/ui.sh b/lib/core/ui.sh index ef44d92..536082b 100755 --- a/lib/core/ui.sh +++ b/lib/core/ui.sh @@ -301,6 +301,9 @@ start_inline_spinner() { [[ -z "$chars" ]] && chars="|/-\\" local i=0 + # Clear line on first output to prevent text remnants from previous messages + printf "\r\033[2K" >&2 || true + # Cooperative exit: check for stop file instead of relying on signals while [[ ! -f "$stop_file" ]]; do local c="${chars:$((i % ${#chars})):1}"