top of page
Writer's pictureBraulio Madrid

Add color to your prints

Many times, when we are programing in the early phase of the project, the main output that we use for make a debug, is the console, generally error messages that will belong to log file, however all these messages show up with the same default color in terminal. What if we would like print warning and error messages with different colors with the purpose of quickly visual identification or simply facilitate a little the life to own costumers, then you have arrived to correct post.


Are you know that the colors in terminal is simply a character? in fact it is so, in this time i offer the keys for interprets the colors and that you can change the colors like that want.


As well as low level programming languages reserve many #specialCharacters for make operations in machine languages as the variable assignation, sum o subtracts a value or concatenate strings, also exist special characters for text coloring, most new languages use ANSI-C standard because born of C language, include command terminals, operative systems are written with C language.


Most of terminals inherit some commands of C language as `printf` that show in screen the text that we want write.




As we see in the image, printf show a value behind username, different of echo command that show in front username.


As my terminal have apply a theme will produce undesired results, i will change to super user for the changes to take effect.


we will make are print an escape character with printf.

printf '\e[31m' # Imprime el color rojo

reviewing a bit, an escape character is a special symbol that we use for the interpreter or compiler leave out a certain character, generally other symbols as '\' as part of the programming language and include into flat text as \"" \{} \[] \() \n \t and other types of line spaces.

Also exist special characters as '\e' that only is supported in bash terminal, this in turn is summary of \033 character of ANSI-C in octal digit 33 or in turn is \x1b of hexadecimal digit.



Color codes

the next table show the color codes that you can use in terminal, also is possible combine many features separated values by ';' as long as it doesn't contradict.



printf '\e[1;5;7;33;41m' # negrilla;parpadeo;inverso;naranja;bg rojo

Color / Features

Foreground

Background

normal

\e[0m

negrilla

\e[1m

cursiva

\e[3m #no soportado en otras sh

subrayado

\e[4m

parpadeo

\e[5m

reverso

\e[7m

oculto

\e[8m

negro

\e[30m

\e[40m

rojo

\e[31m

\e[41m

verde

\e[32m

\e[42m

naranja

\e[33m

\e[43m

azul

\e[34m

\e[44m

magenta

\e[35m

\e[45m

cyan

\e[36m

\e[46m

gris claro

\e[37m

\e[47m

por defecto

\e[39m

\e[49m

gris oscuro

\e[90m

\e[100m

rosa

\e[91m

\e[101m

verde claro

\e[92m

\e[102m

amarillo

\e[93m

\e[103m

azul claro

\e[94m

\e[104m

purpura claro

\e[95m

\e[105m

cyan o teal

\e[96m

\e[106m

blanco

\e[97m

\e[107m

Also is possible to use these codes in other languages. In #python we can use the escape character '\x1b[<id-color>' for change the color.


This is a short post, but i considered curious and util for share, i hope that this tip is helpful in your projects, See you


Este es un post cortico, pero me pareció curioso y útil para compartir, espero que este truco les sea de ayuda en sus propios proyectos. Nos vemos en un próximo post.

5 views0 comments

Comments


bottom of page