Output control in FreeBSD console

If the word "console" in your dictionary is not connected to gaming industry and associates with a black screen and grey letters, this article is for you! The article describes how to use color output text and cursor position control. As an example I'll give a script of a colored progress bar that displays percentage of completion. The progress is shown with the bar animation going from left to right and the current value displayed on right from the progress bar.
Control character "escape" (ASCII - 0x1B HEX, 033 OCT, 27 DEC)
If you want to manage the console, terminal window, and other useful things always start with the control character, so-called escape character. Its ASCII-code is equal to 27; when editing the character is usually displayed as ^[
. However, this is a non-printing character so it's not that simple to input it into the console.
To get around the problem you need to use special commands that are capable of expanding provided lines, replacing the code of the character by the actual character. In the FreeBSD console there are at least two such commands - built-in echo -e
in /bin/sh and utility /usr/bin/printf. It looks like this (display of the control symbol "escape"):
# interpreter /bin/sh echo -e "\033" # display escape symbol using printf printf "\033"
Escape symbol is directly followed by the commands that control the terminal (console, SSH client etc.). Generally it looks like this (output of red text):
# the command outputs bright-red text "hello" on black background printf "\033[1;31mhello\033[m\n"
This is how printf outputted line is interpreted:
\033[1;31m
- color modifier (bright + red text);hello
- the text itself;\033[m
- reset color modifier (return to default settings);\n
- new line.
Color modifiers
Color modifiers are specified by following construction [#m
, where # - number of the color or sequence of numbers separated by a semicolon.
Attributes |
|
---|---|
| cancels color modifiers |
| cancels color modifiers |
| bright |
| underlined in PuTTY, bright in FreeBSD |
| bright in PuTTY, no effects in FreeBSD |
| inversion |
Weird Stuff |
|
| after this command PuTTY will turn into a herd of mojibake... |
| underlined in PuTTY, no effects in FreeBSD |
Symbol color |
|
| black font |
| red font |
| green font |
| yellow font in PuTTY, brown in FreeBSD (if not set to bright) |
| blue font |
| crimson font |
| light blue font |
| grey font |
Background color |
|
| black background |
| red background |
| green background |
| yellow background in PuTTY, brown in FreeBSD |
| blue background |
| crimson background |
| light background |
| grey background |
For purposes of clarity I made a comprised table of colors in
. Here are some screenshots for PuTTY and FreeBSD.Comprised table of colors for PuTTY
Comprised table of colors for FreeBSD
In the table cells you can see parameters of a modifier; the color in the cell represents the color you'll get using the current parameters of the modifier.
Examples of color output:
printf "\033[33;1mbright-yellow inscription\033[m\n" printf "\033[1;42mbright-white inscription on a green background\033[m\n" printf "\033[1;42;7;5mbright-green inscription on a bright-white background (only for PuTTY)\033[m\n"
Do not forget that the color modifiers can be grouped with the help of a separator, in this case a semicolon. Also, after color output it's a good idea to reset (cancel) the modifiers, in order to return the console to its original state.
Cursor control
| moves the cursor to # position |
| moves the cursor down the line to # position |
| moves the cursor to the right to # position |
| moves the cursor to the left to # position |
| moves the cursor down to # position then positions it at the start of the line |
| moves the cursor up to # position then positions it at the end of the line |
| moves the cursor to position # of the current line (works only in PuTTY) |
| moves the cursor to specified coordinates |
Examples of text output with applied cursor position modifiers:
printf "\033[20;20Hhello\n" printf "\033[1E\033[20Chello,\033[1Aworld\033[2E"
An example of implementing progress bar using console and /bin/sh
To summarize this article I'll give an example of implementing progress bar graphics using /bin/sh. Here is an example of a code for implementing progress bar:
#!/bin/sh # function, that displays progress bar progress_bar() { progress=$(( $1 * 30 / 100 )) bar=`printf "\33[41m% ${progress}s\33[m" ""` printf "\33[37D[%- 38s] %- 4s" "${bar}" "$1%" } # percent counter i=0 # inscription with shifting the cursor to the right in full length of the progress bar printf "Check system: \33[37C" # cycles percent while [ $i -le 100 ]; do progress_bar $i i=$(( $i + 1 )) sleep 1 done # because the "progress_bar" doesn't re-position cursor to a new line, after progress output we have to do this on our own. echo
This should look something like this:
Honestly, I didn't bother much creating precise formulas that calculate progress bar coordinates and all the numbers were approximated. But the principle behind the idea is quite simple - the information about the percentage is applied to the variable $bar
which is then represented in graphics terms (red bar), which is then aligned to the right in full length of the entire band. Thus, with each output there is a shift to the left in full length of the progress bar, to provide a static view.