From a6ccdef7e2a845e73efc7152f1d369c3a0cfd037 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sat, 14 Mar 2026 12:28:03 +0000 Subject: [PATCH] UX: Use ANSI \033[K to prevent terminal output artifacts Replaces hardcoded padding spaces with the ANSI Erase in Line sequence (\033[K) after carriage returns (\r). This ensures clean, reliable terminal updates without "ghost" characters from previous, longer strings. Co-authored-by: EiJackGH <172181576+EiJackGH@users.noreply.github.com> --- .Jules/palette.md | 4 ++++ src/main.cpp | 8 ++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/.Jules/palette.md b/.Jules/palette.md index c4e3778..482780b 100644 --- a/.Jules/palette.md +++ b/.Jules/palette.md @@ -25,3 +25,7 @@ ## 2026-03-02 - Hiding the Cursor in CLI Games **Learning:** In terminal applications that require rapid visual updates or where user input doesn't involve typing text, an actively blinking cursor can be a visual distraction. Hiding it during interaction (`\033[?25l`) and rigorously ensuring it is restored (`\033[?25h`) on exit—including signal interrupts—significantly improves the aesthetic and focus. **Action:** Always hide the cursor for interactive CLI games and explicitly restore it across all exit paths, including async-signal-safe signal handlers. + +## 2026-03-14 - Preventing Terminal Output Artifacts +**Learning:** When dynamically updating text in a CLI application on the same line (using `\r`), simply overwriting with new text or appending a fixed number of spaces can lead to "ghost" characters if the new string is shorter than the previous one. The previous text's tail isn't erased, creating confusing visual artifacts. +**Action:** Always use the ANSI escape sequence `\033[K` (Erase in Line) immediately after a carriage return `\r` (e.g., `\r\033[K`) to definitively clear the line from the cursor position to the end. This ensures the line is pristine before writing new content, providing a clean and reliable UX without resorting to hardcoded padding. diff --git a/src/main.cpp b/src/main.cpp index e72f1da..0231ec5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -94,7 +94,7 @@ int main() { } for (int i = 3; i > 0; --i) { - std::cout << "\rStarting in " << i << "... " << std::flush; + std::cout << "\r\033[KStarting in " << i << "... " << std::flush; auto start_wait = std::chrono::steady_clock::now(); while (std::chrono::duration_cast(std::chrono::steady_clock::now() - start_wait).count() < 1000) { int elapsed = std::chrono::duration_cast(std::chrono::steady_clock::now() - start_wait).count(); @@ -108,7 +108,7 @@ int main() { } } } - std::cout << "\rGO! \n" << std::flush; + std::cout << "\r\033[KGO!\n" << std::flush; std::this_thread::sleep_for(std::chrono::milliseconds(200)); tcflush(STDIN_FILENO, TCIFLUSH); @@ -137,10 +137,10 @@ int main() { } if (updateUI) { - std::cout << "\r" << CLR_SCORE << "Score: " << score << CLR_RESET << " " + std::cout << "\r\033[K" << CLR_SCORE << "Score: " << score << CLR_RESET << " " << (hardMode ? CLR_HARD "[HARD MODE]" : CLR_NORM "[NORMAL MODE]") << (score > initialHighscore && initialHighscore > 0 ? " NEW BEST! 🥳" : "") - << " " << std::flush; + << std::flush; updateUI = false; } }