LXF SHELL
Adding colour to the LXF Shell project
Removing his head from the retro demoscene clouds, Ferenc Deák is back down to earth and adding colour to our ongoing LXF Shell project.
Part Five!
Don’t miss next issue, subscribe on page 16!
Credit: https://github.com/fritzone/lxf-shell
OUR EXPERT
Ferenc Deák is a colourful chap, bursting with ideas and expertise to not only add a splash of colour to the LXF Shell, but also to make it increasingly useful.
Having taken a well-deserved pause, which we spent in the world of demo colours and pixels, H we’re ready to honour the pledge we made in the fourth episode of our shell series, and we’re back. Here we stand, poised to delve deeper into the intricacies of the Linux terminal and shell functionality. Not surprisingly after being affected by the colourful series on 90s demoscene effects (see LXF315-LXF318), our focus today is on the visually striking features of the Linux terminal, and how we can use them in our shell. So, be prepared for colours and curses.
Beyond black and white
In Linux terminals, printing coloured text is achieved through ANSI escape codes. These provide a means to specify text colours, background colours and text formatting. By incorporating these codes into your commands, you can create visually appealing and informative displays right in your terminal window.
The syntax for using ANSI escape codes is straightforward. Here’s a basic example: $ echo -e “\e[31mThis is red text\e[0m”
Let’s dissect this command:
\e[31m : Sets the text colour to red. This is red text : Your desired text output.
\e[0m : Resets the text colour to the default. The output of the command above is the following:
QUICK TIP
The code for the shell can still be found at https://github. com/fritzone/lxf-shell.
Not only the foreground of the text can be changed, we can also change the background, too: $ echo -e “\e[31;44mThis is red text on blue background\e[0m”
Here’s the breakdown:
\e : This is the ANSI escape character, indicating the start of an escape sequence.
[31;44m : This defines the colour attributes – 31 specifies the foreground (red in this case), and 44 is the background (blue in this case). The m at the end indicates the end of the colour attribute sequence.
This is red text on blue background : This is the text that will be printed with the specified colours.
\e[0m : This sequence resets the text attributes to the default.
There are 16 different foreground and background colours; the very colourful screenshot (opposite page) presents all possible combinations, and the following very short Python program is responsible for this: