In Unix terminal we can use colorful output for emphasizing important pieces of information such as fatal errors or warnings. As a student in the Technical University I have a lot of problem sets to solve in Octave. Its prompt is similar to the normal Unix terminal (and I suppose it is). To make the output of my scripts more readable I tried to implement a few function to colorize some important messages.
The functions make usage of fprintf function which draw obtained text to the
standard output or stderr (first argument is the output stream). The second
argument is construction which wraps the message passed as
msg by special so
called ANSI escape sequences. These sequences tells prompt to display text with
some special styling such as color, underline, background color etc.
% The set of function to print fancy messages in octave prompt. To use it just % call function name and as argument send a message you want to show. % To test it use following commands: % infom("Information message"); error("Error message"); % success("Success message"); head("This is head message"); % Prevent octave to run it immediately. 1; function infom(msg) fprintf(1, [char(27), ... '[94m' msg, ... char(27), ... '[0m\n'] ); endfunction function error(msg) fprintf(1, [char(27), ... '[91m' msg, ... char(27), ... '[0m\n'] ); endfunction function success(msg) fprintf(1, [char(27), ... '[32m' msg, ... char(27), ... '[0m\n'] ); endfunction function head(msg) fprintf(1, [char(27), ... '[90m', ... '==============================================================', ... '========\n', ... msg '\n', ... '==============================================================', ... '========\n',... char(27), ... '[0m'] ); endfunction
Background and foreground colors can be changed in the construction shown below.
It consist of escape character
\x1B) and format
code surrounded by the [ and m characters. The first number is
responsible for text formatting (normal, bold, dim, underlined...), the second
for background color and third one for foreground color.
So, the sequence above means 0 - normal text (all attributes to default), 49 - default background color and 30 - black text color. You can found more codes at the following page and in table below.
How to use it? It's easy enough, all you need to do are few follow steps:
- Download the script
- Create directory where you will store this script for further usage
- Then in your Octave's config (by default it should be
~/.octavercon Linux system) add and don't forgot to change next commands:
# Change to address where your script is addpath("/home/andrew/Documents/Octave/") messages PAGER_FLAGS("-r")
First line adds your directory to the global search. So, when you want to run
this script you can just type in the Octave prompt
messages and this script
will be run.
Second line runs this script. Because this configuration file (
run at the startup of the Octave this script will be automatically executed.
Last command add parameter to
less program which is used when output of your
script isn't suitable for one screen of the terminal. This parameter is needed
to correctly display colors while you see output over the