Skip to content

Commit db4ef42

Browse files
🎨 Palette: Use Erase in Line ANSI escape sequence for dynamic text updates
Co-authored-by: EiJackGH <172181576+EiJackGH@users.noreply.github.com>
1 parent f36fbc5 commit db4ef42

3 files changed

Lines changed: 8 additions & 4 deletions

File tree

‎.Jules/palette.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,6 @@
2525
## 2026-03-02 - Hiding the Cursor in CLI Games
2626
**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.
2727
**Action:** Always hide the cursor for interactive CLI games and explicitly restore it across all exit paths, including async-signal-safe signal handlers.
28+
## 2026-03-22 - Erase in Line with ANSI escape sequence
29+
**Learning:** Using `\033[K` after `\r` efficiently clears dynamic terminal lines, preventing trailing text artifacts without needing hardcoded spaces.
30+
**Action:** Always use `\033[K` (Erase in Line) immediately after the carriage return `\r` instead of hardcoding padding spaces.

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,4 @@ highscore.txt
4444

4545
# Persistent data
4646
highscore.txt
47+
venv/

‎src/main.cpp‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ int main() {
9494
}
9595

9696
for (int i = 3; i > 0; --i) {
97-
std::cout << "\rStarting in " << i << "... " << std::flush;
97+
std::cout << "\r\033[KStarting in " << i << "... " << std::flush;
9898
auto start_wait = std::chrono::steady_clock::now();
9999
while (std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start_wait).count() < 1000) {
100100
int elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start_wait).count();
@@ -108,7 +108,7 @@ int main() {
108108
}
109109
}
110110
}
111-
std::cout << "\rGO! \n" << std::flush;
111+
std::cout << "\r\033[KGO!\n" << std::flush;
112112
std::this_thread::sleep_for(std::chrono::milliseconds(200));
113113
tcflush(STDIN_FILENO, TCIFLUSH);
114114

@@ -137,10 +137,10 @@ int main() {
137137
}
138138

139139
if (updateUI) {
140-
std::cout << "\r" << CLR_SCORE << "Score: " << score << CLR_RESET << " "
140+
std::cout << "\r\033[K" << CLR_SCORE << "Score: " << score << CLR_RESET << " "
141141
<< (hardMode ? CLR_HARD "[HARD MODE]" : CLR_NORM "[NORMAL MODE]")
142142
<< (score > initialHighscore && initialHighscore > 0 ? " NEW BEST! 🥳" : "")
143-
<< " " << std::flush;
143+
<< std::flush;
144144
updateUI = false;
145145
}
146146
}

0 commit comments

Comments
 (0)