Recently while working on a project, I figured that I should prettify my output a little bit. First I tried to implement something that works with the python logging module. But since the logging module seemed too heavy for what I needed: easy pretty terminal printing. I decided to write a small little module myself. I looked around the web, found a bunch of scripts that implemented it but nothing that gave a nice and simple explanation. So here we go.
What are ANSI Codes?
ANSI (American National Standard Institute) codes, or escape sequences are codes, meaning characters, that are part of the printed text. Instead of being displayed like every other characters they are being recognized by terminals and used to change formatting.
Some options include:
- Background Color
What to do
This is going to be really basic for easy formatting. I am going to cover SGR, Select Graphic Rendition. Initially you need to tell the terminal that you are going to use an ANSI Escape Code.
- CSI n [;k] m
So here comes the small python module I talked about that implements everything. (Explanation will follow)
This is the core of everything. The Escape Sequence introduces the new formatting and the
ENDC resets everything again.
Here we simple join all the parts together:
- Formatting Sequence
- Actual Text
- Reseting Sequence
Here we set up all the different options that we can reuse later. Note that some of them aren’t supported very well since its always up to the terminal application to implement these features.
This is just a simple dictionary object containing the color names and the codes that are associated to them.
This function can be used to print bold, white and underlined text in the terminal. Additional formatting options can be achieved exactly in the same way.
In the end the usage is pretty simple after the setup from before:
Why use them?
Shells don’t have to be plain white and black. ANSI Formatting can help command line applications to display information better and more efficiently.
If you want to know more than this, Google is your friend. I hope this gives you a basic background on the topic. There is a lot of material out there and a lot to learn.