C++ ANSI codes
Date: 2020-03-11Last modified: 2024-11-02
Table of contents
Some ANSI codes:
"\033[F"
– move cursor to the beginning of the previous line"\033[A"
– move cursor up one line"\033[2K\r"
— clear current line
// These 5 lines will act as a background.
// Here the placeholder is intentionaly more long that the final string.
for (int i = 0; i < 5; ++i) {
cout << "Line number " << i << " with long placeholder." << endl;
}
this_thread::sleep_for(seconds(2));
for (int j = 0; j < 10; ++j) {
// Move up 5 lines
cout << "\033[F";
cout << "\033[F";
cout << "\033[F";
cout << "\033[F";
cout << "\033[F";
// Write 5 lines down again
for (int i = 0; i < 5; ++i) {
cout << "Line number " << i + j << " final."
<< "\n";
this_thread::sleep_for(milliseconds(20));
}
}
You noticed the line mixed content. This is because no cleaning of current line was done.
this_thread::sleep_for(seconds(2));
for (int j = 0; j < 10; ++j) {
// Move up 5 lines
cout << "\033[F";
cout << "\033[F";
cout << "\033[F";
cout << "\033[F";
cout << "\033[F";
// Write 5 lines down again
for (int i = 0; i < 5; ++i) {
// Clear current line from cursor position until the end of line
cout << "\033[2K\r";
cout << "Line number " << i + j << " final 2."
<< "\n";
this_thread::sleep_for(milliseconds(20));
}
}
Possible output
Line number 0 with long placeholder.
Line number 1 with long placeholder.
Line number 2 with long placeholder.
Line number 3 with long placeholder.
Line number 4 with long placeholder.
[F[F[F[F[FLine number 0 final.
Line number 1 final.
Line number 2 final.
Line number 3 final.
Line number 4 final.
[F[F[F[F[FLine number 1 final.
Line number 2 final.
Line number 3 final.
Line number 4 final.
Line number 5 final.
[F[F[F[F[FLine number 2 final.
Line number 3 final.
Line number 4 final.
Line number 5 final.
Line number 6 final.
[F[F[F[F[FLine number 3 final.
Line number 4 final.
Line number 5 final.
Line number 6 final.
Line number 7 final.
[F[F[F[F[FLine number 4 final.
Line number 5 final.
Line number 6 final.
Line number 7 final.
Line number 8 final.
[F[F[F[F[FLine number 5 final.
Line number 6 final.
Line number 7 final.
Line number 8 final.
Line number 9 final.
[F[F[F[F[FLine number 6 final.
Line number 7 final.
Line number 8 final.
Line number 9 final.
Line number 10 final.
[F[F[F[F[FLine number 7 final.
Line number 8 final.
Line number 9 final.
Line number 10 final.
Line number 11 final.
[F[F[F[F[FLine number 8 final.
Line number 9 final.
Line number 10 final.
Line number 11 final.
Line number 12 final.
[F[F[F[F[FLine number 9 final.
Line number 10 final.
Line number 11 final.
Line number 12 final.
Line number 13 final.
[F[F[F[F[F[2K
Line number 0 final 2.
[2K
Line number 1 final 2.
[2K
Line number 2 final 2.
[2K
Line number 3 final 2.
[2K
Line number 4 final 2.
[F[F[F[F[F[2K
Line number 1 final 2.
[2K
Line number 2 final 2.
[2K
Line number 3 final 2.
[2K
Line number 4 final 2.
[2K
Line number 5 final 2.
[F[F[F[F[F[2K
Line number 2 final 2.
[2K
Line number 3 final 2.
[2K
Line number 4 final 2.
[2K
Line number 5 final 2.
[2K
Line number 6 final 2.
[F[F[F[F[F[2K
Line number 3 final 2.
[2K
Line number 4 final 2.
[2K
Line number 5 final 2.
[2K
Line number 6 final 2.
[2K
Line number 7 final 2.
[F[F[F[F[F[2K
Line number 4 final 2.
[2K
Line number 5 final 2.
[2K
Line number 6 final 2.
[2K
Line number 7 final 2.
[2K
Line number 8 final 2.
[F[F[F[F[F[2K
Line number 5 final 2.
[2K
Line number 6 final 2.
[2K
Line number 7 final 2.
[2K
Line number 8 final 2.
[2K
Line number 9 final 2.
[F[F[F[F[F[2K
Line number 6 final 2.
[2K
Line number 7 final 2.
[2K
Line number 8 final 2.
[2K
Line number 9 final 2.
[2K
Line number 10 final 2.
[F[F[F[F[F[2K
Line number 7 final 2.
[2K
Line number 8 final 2.
[2K
Line number 9 final 2.
[2K
Line number 10 final 2.
[2K
Line number 11 final 2.
[F[F[F[F[F[2K
Line number 8 final 2.
[2K
Line number 9 final 2.
[2K
Line number 10 final 2.
[2K
Line number 11 final 2.
[2K
Line number 12 final 2.
[F[F[F[F[F[2K
Line number 9 final 2.
[2K
Line number 10 final 2.
[2K
Line number 11 final 2.
[2K
Line number 12 final 2.
[2K
Line number 13 final 2.