Back to TILs

C++ ANSI codes

Date: 2020-03-11Last modified: 2023-01-26

Table of contents

Some ANSI codes:

  // 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));
    }
  }
Output with mixed lines.
Fig. 1 - Output with mixed lines.

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));
    }
  }
Output with cleaned lines.
Fig. 2 - Output with cleaned lines.

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.
Line number 0 final.
Line number 1 final.
Line number 2 final.
Line number 3 final.
Line number 4 final.
Line number 1 final.
Line number 2 final.
Line number 3 final.
Line number 4 final.
Line number 5 final.
Line number 2 final.
Line number 3 final.
Line number 4 final.
Line number 5 final.
Line number 6 final.
Line number 3 final.
Line number 4 final.
Line number 5 final.
Line number 6 final.
Line number 7 final.
Line number 4 final.
Line number 5 final.
Line number 6 final.
Line number 7 final.
Line number 8 final.
Line number 5 final.
Line number 6 final.
Line number 7 final.
Line number 8 final.
Line number 9 final.
Line number 6 final.
Line number 7 final.
Line number 8 final.
Line number 9 final.
Line number 10 final.
Line number 7 final.
Line number 8 final.
Line number 9 final.
Line number 10 final.
Line number 11 final.
Line number 8 final.
Line number 9 final.
Line number 10 final.
Line number 11 final.
Line number 12 final.
Line number 9 final.
Line number 10 final.
Line number 11 final.
Line number 12 final.
Line number 13 final.

Line number 0 final 2.

Line number 1 final 2.

Line number 2 final 2.

Line number 3 final 2.

Line number 4 final 2.

Line number 1 final 2.

Line number 2 final 2.

Line number 3 final 2.

Line number 4 final 2.

Line number 5 final 2.

Line number 2 final 2.

Line number 3 final 2.

Line number 4 final 2.

Line number 5 final 2.

Line number 6 final 2.

Line number 3 final 2.

Line number 4 final 2.

Line number 5 final 2.

Line number 6 final 2.

Line number 7 final 2.

Line number 4 final 2.

Line number 5 final 2.

Line number 6 final 2.

Line number 7 final 2.

Line number 8 final 2.

Line number 5 final 2.

Line number 6 final 2.

Line number 7 final 2.

Line number 8 final 2.

Line number 9 final 2.

Line number 6 final 2.

Line number 7 final 2.

Line number 8 final 2.

Line number 9 final 2.

Line number 10 final 2.

Line number 7 final 2.

Line number 8 final 2.

Line number 9 final 2.

Line number 10 final 2.

Line number 11 final 2.

Line number 8 final 2.

Line number 9 final 2.

Line number 10 final 2.

Line number 11 final 2.

Line number 12 final 2.

Line number 9 final 2.

Line number 10 final 2.

Line number 11 final 2.

Line number 12 final 2.

Line number 13 final 2.

References