Christmas tree on the command line

The New Year is coming soon, I no longer want to think about serious work.

Everyone is trying to decorate something for the holiday: home, office, workplace ... Let's decorate something! For example, a command line prompt. To some extent, the command line is also a workplace.

In some distributions, it is already "decorated":

Christmas tree on the command line

In others, it is gray and inconspicuous:

Christmas tree on the command line

And we can do, for example, like this:

Christmas tree on the command line

Of course, the taste and color of all felt-tip pens are different. If such coloring seems clumsy and inappropriate to you, then know that this point of view has every right to life. And if you also want to add a little New Year's mood, read on for a small New Year's article from Cloud4Y.

To begin with, I will explain how the output of the terminal is β€œpainted”. This is done using escape sequences. And to be more precise - the control code sequences of the ANSI / VT100 terminal. Which automatically means that your terminal emulator must support this standard, otherwise the New Year's miracle will not happen. And yes, $SHELL is assumed to be in your bash.

These commands are called escape sequences because they start with the ASCII character "escape". There are many control sequences, and they allow, for example, to display terminal settings, control the display and movement of the cursor, change the font, delete and hide text. We will choose one of the variety of possibilities - changing the color of the text and background.

Execute code sequence *ESC*[{attr1};...;{attrn}m
as a symbol Escape its octal form is used, i.e. 33. As for attributes, here is a short list of possible values:

0 Reset all attributes
1 Bright (increased brightness)
2 Dim (dimming)
4 Underscore (underscore)
5 Blink (blink)
7 Reverse (inversion)
8 Hidden (hide text)

Foreground Colors (pen color, text is displayed in this color):
30 Black (black)
31 Red (red)
32 Green (green)
33 Yellow (yellow)
34 Blue (blue)
35 Magenta (magenta)
36 Cyan (blue)
37 White (white)

Background Colors (paper color, or background color):
40 Black (black)
41 Red (red)
42 Green (green)
43 Yellow (yellow)
44 Blue (blue)
45 Magenta (magenta)
46 Cyan (blue)
47 White (white)

Notice if you now command in the terminal: echo 33[0;31mΠ½Π΅ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ тСкст 33[0m’

... then you will predictably get monochrome gibberish at the output:

Christmas tree on the command line

Why? Because it was necessary to use the advanced features of the echo command. It is enough to add one key: echo -e β€˜ 33[0;31mΠ½Π΅ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ тСкст 33[0m’

Now the output will look correct:

Christmas tree on the command line

We figured out the coloring of the output to the terminal. Now let's see how to colorize the command line prompt.

This is done by changing the PS1 variable. The variable is responsible for the command line prompt. Its appearance can also be changed, including with the help of escape sequences. But there is a slight difference: you need to start the sequence with the symbol β€œ[”, and end with the symbol β€œ]”, otherwise it will be printed to the terminal.

All of the possible command sequences are detailed in the bash manual, so I'm going to let readers choose what they like to see at the command line prompt. As an example, I will give my value of the PS1 variable:

[ 33[34;1m]t[ 33[0m],[ 33[32m]u@l@h[ 33[0m]:[ 33[33m]W[ 33[0m],[ 33
[31m]![ 33[0m]$n

I will decipher this terrible spell:

[33[34;1m] - turn on the bright blue (second attribute) font color
t - display the current time in the format HH: MM: SS
[33[0m] - reset font color settings
, - just a comma (unexpected, right?)
[33[32m] - turn on the green color of the font
u@l@h - display the username, terminal device number and short host name, separated by the β€œ@” symbol
[33[0m] - reset the font color settings again
: - just a colon (suddenly!)
[33[33m] - enable yellow font color
W - display the name of the current directory
[33[0m] - again reset the font color settings
, - one more comma (who would have thought!)
[33[31m] - turn on the red font color
! - display the command number in the terminal
[33[0m] - do not forget to reset the font color settings
$ - output "#" for root and "$" for everyone else
n - line translation. For what? For the command to start at the left edge of the window, rather than at the end position of the command line prompt.

Where to redefine a variable? The most logical way to do this is in ~/.bashrc.

The appearance of the invitation is only limited by your imagination. In principle, nothing prevents you from using the above tools to make a command line prompt in the form of a Christmas tree. True, such an invitation will be very cumbersome, and the work will definitely not become more convenient. The Christmas tree can simply be displayed above the command line prompt when logging in (we still need to edit ~/.bashrc). Dare! And thanks for your attention.

Christmas tree on the command line

What else can you read on the blog? Cloud4Y

β†’ Configuring top in GNU/Linux
β†’ Pentesters at the forefront of cybersecurity
β†’ Startups that can surprise
β†’ Do data centers need pillows?
β†’ The house that the robot built

Subscribe to our Telegram-channel, so as not to miss the next article! We write no more than twice a week and only on business. We also remind you that Cloud4Y, a corporate cloud provider, has launched the FZ-152 Cloud at the price of a regular campaign. You can apply until December 31.

Source: habr.com

Add a comment