writting a fun cli apps with ncurses

Do you feel like writing a nice CLI Application? Where you can manipulate your text in any position and any combination of colors. There are plenty of CLI apps are nicely build with positioning and colors, such as vim, kismet, irssi, screen etc. You can do it in c/c++ with the help of ncurses library.

Do it with ncurses is not difficult, check out the ncurses programming HowTo. This tutorial was well written, it covers from beginners (hello world example) to advance level (uses widget libraries). It includes also some fun game examples just to illustrate the capabilities of ncurses. I have wrote one after taking two days reading the ebook.

matrix 0.2

Matrix 0.2
A simple matrix animation using ncurses library, license under GNU GPL.


Usage: matrix [option]...
matrix --version
matrix --help

-c [1-7] Color value from 1 to 7, default is 2
-t [string] Text heading for each flow string
-s [1-9] The flow speed, 1 fastest, 9 slowest
-l [1-50] Default have 25 flow strings, you can add more
-b Use black as background
-v Print Version
-h Print Usage

Internal Usage:
q | enter Quit the program
[1-7] Changing color

matrix 0.2 bin
matrix 0.2 source code

To compile the source code, you must have ncurses library installed.

g++ -o matrix{,.cc} -lncurses

For more g++ examples, check out linux.byexamples.com.
To print string in colors without ncurses, here.
To print string at any position without ncurses, here.

create a timer

Sometimes, you need a timer function. For example, you wanna check some live status every 5 seconds. By using sleep() is just not the way of doing, because the programs suppose to process for other things within this 5 seconds. We need a non-blocking timer function for that.

I created two functions uses timeval structure, and gettimeofday(), does the job of timer. Bellow is the code that illustrate how it works:

#include<sys/time.h>
#include<stdio.h>

int SetTimer(struct timeval &tv, time_t sec)
{
    gettimeofday(&tv,NULL);
    tv.tv_sec+=sec;

    return 1;
}

int CheckTimer(struct timeval &tv, time_t sec)
{
    struct timeval ctv;
    gettimeofday(&ctv,NULL);

    if( (ctv.tv_sec &gt; tv.tv_sec) )
    {
        gettimeofday(&tv,NULL);
        tv.tv_sec+=sec;
        return 1;
    }
    else
        return 0;
}

int main()
{
    struct timeval tv;
    SetTimer(tv,5); //set up a delay timer
    printf("start counting.\n");
    while(1)
        if (CheckTimer(tv,5)==1)
            printf("Welcome to cc.byexamples.com\n");
    return 0;
}

SetTimer() are use to set timer for the first time, and CheckTimer() use to check for the previous timer value. If it is times up, it will returns 1 at the same time initiate a new time value based on what you pass in at second argument. By doing that, is to allow flexibility of changing time interval value.

The minimum value for this timer is one second, for timer that works for less than one seconds, replace the SetTimer and CheckTimer as bellow:

int SetTimer(struct timeval &tv, int usec)
{
    gettimeofday(&tv,NULL);
    tv.tv_usec+=usec;

    return 1;
}

int CheckTimer(struct timeval &tv, int usec)
{
    struct timeval ctv;
    gettimeofday(&ctv,NULL);

    if( (ctv.tv_usec &gt;= tv.tv_usec) || (ctv.tv_sec &gt; tv.tv_sec) )
    {
        gettimeofday(&tv,NULL);
        tv.tv_usec+=usec;
        return 1;
    }
    else
        return 0;
}

P.S. The code must be compile in c++, I uses reference which it is not available in c. You can compile the code with g++ ,

g++ -o mytimer{,.cc}

More examples of gcc/g++ compiler shows here.

Feel free to enhance it, I appreciate any comments regarding modifications, suggestions on how to improve it.