Octave - colored prompt messages

24.10.2016
Octave Prompt with colors

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 ^ (or \e, \033, \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.

^[0;49;30m

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.

Code
Color
Preview
39
Default
Colors in the bash
30
Black
Exapmle of colors in bash
31
Red
Example of colorful output in bash
32
Green
Example of colorful output in bash
33
Yellow
Example of colorful output in bash
34
Blue
Example of colorful output in bash
35
Magenta
Example of colorful output in bash
36
Cyan
Example of colorful output in bash
37
Light Gray
Example of colorful output in bash
90
Dark Gray
Example of colorful output in bash
91
Light Red
Example of colorful output in bash
92
Light Green
Example of colorful output in bash
93
Light Yellow
Example of colorful output in bash
94
Light Blue
Example of colorful output in bash
95
Light Magenta
Example of colorful output in bash
96
Light Cyan
Example of colorful output in bash
97
White
Example of colorful output in bash

How to use it? It's easy enough, all you need to do are few follow steps:

  1. Download the script
  2. Create directory where you will store this script for further usage
  3. Then in your Octave's config (by default it should be ~/.octaverc on 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 (.octaverc) is 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 less program.

References:


Hey👋 I'm Andrey. I have a quite wide range of experience from hardware debugging and kernel development to web scrapping. During the day I work as a Software Engineer on Linux filesystems. In this blog I post my personal short tutorials or intresting technical notes. I love open source and use mainly free software. Btw I use Arch. Feel free to ping me here:

email or @alberand or @alberand_