From 0bb5c1f4066a72110262ad0b70baf3bc20b07855 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Mon, 16 Mar 2026 12:37:21 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20Palette:=20Enhance=20CLI=20UX=20?= =?UTF-8?q?by=20clearing=20dynamic=20lines=20with=20`\033[K`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 💡 What: Replaced hardcoded padding spaces with the `\033[K` (Erase in Line) ANSI escape sequence to prevent text artifacts when updating dynamic lines using `\r`. Also added `venv/` to `.gitignore`. 🎯 Why: Relying on hardcoded padding strings is brittle and error-prone, occasionally leading to lingering characters on-screen. Clearing from the cursor to the end of the line ensures clean rendering. ♿ Accessibility: Improves the clarity and immediate readability of the terminal game interface by ensuring artifacts never obscure current state information. Co-authored-by: EiJackGH <172181576+EiJackGH@users.noreply.github.com> --- .Jules/palette.md | 4 ++++ .gitignore | 1 + src/main.cpp | 8 ++++---- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/.Jules/palette.md b/.Jules/palette.md index c4e3778..925ae53 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-06-15 - Erase in Line (`\033[K`) for clean terminal updates +**Learning:** When updating a terminal line dynamically using a carriage return (`\r`), relying on hardcoded padding spaces to overwrite old content is fragile and messy. If the new string is shorter than the old one, the trailing characters from the previous string remain visible, leading to visual artifacts. +**Action:** Use the ANSI escape sequence `\033[K` (Erase in Line) immediately after `\r`. This clears the line from the cursor to the end, ensuring that whatever follows is printed on a clean slate. diff --git a/.gitignore b/.gitignore index eb2c19a..d07c78a 100644 --- a/.gitignore +++ b/.gitignore @@ -44,3 +44,4 @@ highscore.txt # Persistent data highscore.txt +venv/ 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; } }