What is Ansi Escape Code?

Asked

Viewed 532 times

2

For handling of the linux terminal (other OS console also), is using a string started with a hexadecimal value 0x1B known as ANSI Escape Code.

For example printf("\x1B[32mMy Text\n").

How does the ASNI Escape Code?

What he is exactly?

1 answer

1


ANSI Escape Code

There is a pattern made based on a standard ANSI to control outputs made in a text terminal.

The manipulation of these outputs is done by commands called ANSI Escape Codes, that are activated by the value character 27 or 1B hexadecimal (Esc button) followed by [ (square bracket), after which a value and a letter are passed to represent your action.

Examples

To position the cursor at a certain screen position, use the letters H or f, passing along the row and column to the cursor.

printf("\x1B[%d;%dH",6, 5);
printf("\x1B[%d;%df",6, 5);

To clear the screen, use the value 2 followed by the character J.

printf("\x1B[2J");

To change the color of the text to be written, use the value of the color followed by m.

printf("\x1B[31mRed"); // Escreve em vermelho
printf("\x1B[01;32mBoldGreen"); // Escreve em verde e negrito
printf("\x1B[00mTexto Normal"); // Retira estilo

Browser other questions tagged

You are not signed in. Login or sign up in order to post.