03. [20p] Quality-of-life improvements

Click GIF to maximize.

[10p] Task A - Color man pages

On Linux, you will consult the man pages a lot. Normally, these are displayed in a bland monochrome manner, which makes them hard to parse. If you need to convince yourselves, check the manual page for the manual itself:

$ man man

Let's try to spice things up a little! Your terminal is able to display quite a few colors.

$ sudo apt install colortest
$ colortest-16b

In olden days, terminals were implemented by a driver that represented each cell by two bytes in a matrix. One byte held the ASCII value of the displayed character. The other was used to specify character / background display color and other features such as blinking (if you're looking at that terminal cursor, you're spot on). In order for users (i.e.: not the driver) to specify what color to set, ANSI escape codes were invented. These consisted of sequences of a 0x1b byte (or 033 in octal representation), followed by [, then ; separated numerical values representing the codes, and terminated by the m character. Let's try to print a fancy “hello world” to the terminal (33 is yellow, 1 is bold, 0 is reset):

$ echo "\033[1;33m Hello World! \033[0m"

We can use this… Add this at the end of your .zshrc file, source it, and open the same man page again:

# color schemes for man pages
man() {
    LESS_TERMCAP_mb=$'\e[1;34m'   \
    LESS_TERMCAP_md=$'\e[1;32m'   \
    LESS_TERMCAP_so=$'\e[1;33m'   \
    LESS_TERMCAP_us=$'\e[1;4;31m' \
    LESS_TERMCAP_me=$'\e[0m'      \
    LESS_TERMCAP_se=$'\e[0m'      \
    LESS_TERMCAP_ue=$'\e[0m'      \
    command man "$@"
}

What's happening here:

  • man(): we're basically replacing the man command with a bash function.
  • LESS_TERMCAP_*: these are variables used (indirectly) by man to format its output. The authors of these pages decide how and when to use them. We decide what they are. By defining these variables before running a bash command, we set them only in the environment of the started process and will disappear when said process terminates.
  • command man “$@”: here, command tells bash / zsh to use the original man command, not the function we just defined. If you're wandering why this is necessary, think back to the whole tmux and zsh fiasco described earlier. The “$@” here is substituted with all the arguments that the man bash function received and passes them on to the man command.

[10p] Task B - Color iproute2 output

If you scroll back to the first gif in this lab, you might notice the execution of the ip addr show command, to list the network interfaces on my system, and their IP addresses. This is similar to the ipconfig command in Windows cmd. In fact, prior to ip, Linux had an even more similar command, called ifconfig (yeah…) But ifconfig is now deprecated and is not even installed by default in most modern distributions. People still (incorrectly) use it because they don't like the monochrome output of ip and it's output is more nicely formatted. But ip can use ANSI escape codes if you feed it the -c flag. To this end, let's specify an alias (basically a shortcut) in .zshrc.

# alias for iproute2 color output
alias ip='ip -c'

Source .zshrc and try ip addr show again. Now, every time you run ip, it automatically expands to ip -c.

ii/labs/01/tasks/03.txt · Last modified: 2021/12/31 02:47 by radu.mantu
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0