Extra: Terminal Control Codes

Final Project Supplemental

The final project utilizes terminal control codes to style and control the output when rendering the chessgame, This will give a deeper explanation of how the starter code is structured and how the colored output works.

§1 ANSI Escape Codes #

Most terminal implementations support control codes which allow for special behavior when printed to the terminal output.

Control codes are used to control formatting, color, and other output options on terminals and consoles. Here are the key uses:

Text Formatting:

Text or Background Colors:

Cursor Movement:

Clearing the Screen:

Cursor Visibility:

Saving and Restoring Cursor Position:

Terminal Scrolling:

Bell or Alerts:

Customizing Terminal Appearance:

Interactive CLI Applications:

Highlighting Errors or Messages:


In the starter code for project 4, I use escape codes to display the board, cursor, and selection highlights.

A control code is just specifically formatted text printed to the terminal. For example, in C/C++ if you run the following line, the background will be changed to green.

printf("\033[42m");

These control codes are used widely in the starter code for project 4 to create the chess game.

In raw form, the output looks like this:

\033[?25l\033[?1049h\033[40m\033[2J\033[HControls: Arrow Keys, Space or Enter to Select ('q' to quit)

\033[100m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m
. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m
. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m⚔ \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m
. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m
. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m+ \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m
. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m
. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m
. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m

In a terminal this becomes:

basic

Here, the color codes are used to darken the background, highlight the “cursor” position, and highlight pieces/positions.


Some ANSI codes used in project 4 starter.cpp:

You can view many more example of ANSI escape codes here: https://en.wikipedia.org/wiki/ANSI_escape_code