Home / Articles / BSD / FreeBSD / Output control in FreeBSD console

Output control in FreeBSD console

Output control in FreeBSD console
Output control in FreeBSD console
  • Currently 0 out of 5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Rating: 0/5 (0 votes cast)

Thank you for rating!

You have already rated this page, you can only rate it once!

Your rating has been changed, thanks for rating!

Log in or create a user account to rate this page.


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

\033[m

cancels color modifiers

\033[0m

cancels color modifiers

\033[1m

bright

\033[4m

underlined in PuTTY, bright in FreeBSD

\033[5m

bright in PuTTY, no effects in FreeBSD

\033[7m

inversion

Weird Stuff

\033[12m

after this command PuTTY will turn into a herd of mojibake...

\033[21m

underlined in PuTTY, no effects in FreeBSD

Symbol color

\033[30m

black font

\033[31m

red font

\033[32m

green font

\033[33m

yellow font in PuTTY, brown in FreeBSD (if not set to bright)

\033[34m

blue font

\033[35m

crimson font

\033[36m

light blue font

\033[37m

grey font

Background color

\033[40m

black background

\033[41m

red background

\033[42m

green background

\033[43m

yellow background in PuTTY, brown in FreeBSD

\033[44m

blue background

\033[45m

crimson background

\033[46m

light background

\033[47m

grey background

For purposes of clarity I made a comprised table of colors in color-samples.txt color-samples.txt 4.05 kB. Here are some screenshots for PuTTY and FreeBSD.

Comprised table of colors for PuTTY

Comprised table of colors for PuTTY

Comprised table of colors for FreeBSD

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

\033[#A

moves the cursor to # position

\033[#B

moves the cursor down the line to # position

\033[#C

moves the cursor to the right to # position

\033[#D

moves the cursor to the left to # position

\033[#E

moves the cursor down to # position then positions it at the start of the line

\033[#F

moves the cursor up to # position then positions it at the end of the line

\033[#G

moves the cursor to position # of the current line (works only in PuTTY)

\033[#;#H

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:

An example of implementing progress bar using console and /bin/sh

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.



Read also

Install MySQL server on FreeBSD

Install MySQL server on FreeBSD

Install Apache in FreeBSD

Install Apache in FreeBSD

Log in or create a user account to post a comment.

easyos.net
117217982212577402640

Quick navigation

General navigation