Write a Curses program using the C Language

Vocabulary


Getting Started

Example one — Using putty.exe or Terminal (in OSX) ssh youruser@shell.mfaca.sva.edu

First we must create a folder "curses" for our examples and enter it (use mkdir and cd). Once inside the new folder, we can create a document such as begin.c with nano (the text editor). The contents of the file can be something like:

#include <ncurses.h>

int main(){
     printf("hello world");
}

Compiling

Once you have typed this inside the nano window, you use Ctrl-o to initiate the writing of the data to the disk. Then, by quiting (Ctrl-x), and typing ls, you should see the source code file, begin.c. Now, to convert this to a program, type
cc -o begin -l ncurses begin.c
The -o tells the compiler which name to give to the oUTPUT file. The -l (ell) tells the compiler that the code uses the ncurses library. And the begin.c is the source code we want to compile.

After executing this, we should be able to type ls and see the compiled program (begin) along with the source code file. If you get an error message and the executable program is not built, that means you need to check the source code. Usually a line number will be given to tell you where the error is in your code.

Executing

Running the program can be accomplished by typing
./begin
The ./ is needed because the current directory is probably not included in our PATH variable. This means that if we type only the word "begin" the bash shell will search for the program, but will not find the one we just compiled. Recall the "." is a short-hand notation for the current directory.

Using the Curses library

Modifying the original code document (nano begin.c), we will now add some functions that are actually part of the ncurses library.

#include <ncurses.h>

int main(){
     // Initialize the curses toolkit
     initscr();

     // Do some stuff with the terminal window
     clear();
     printw("hello world");
     refresh();

     sleep(5);
     // Clean up
     endwin();
}
This time the clear() function clears the terminal before the printw() function delivers its string.

Drawing in the terminal with text

The assignment for next week is to experiment with the Curses library to produce images in the terminal window. If you feel you are capable of doing more (getting input, for example) I provide some code examples for this type of thing. But most of you are new to both C and Unix, so I know that some of you will struggle to understand the syntax of the language. Nevertheless, this is an opportunity to create something visual, and this is a course in the School of Visual Arts....

Examples

In this last example, we will use the for loop to draw characters across the terminal window.

#include <ncurses.h>

int main(){
     // Initialize the curses toolkit
     initscr();

     // Do some stuff with the terminal window
     clear();
     int x,y;
     for(x=0;x<COLS;x++)
       for(y=0;y<LINES;y++)
         mvaddch(y,x,'Q');
     refresh();

     sleep(5);
     // Clean up
     endwin();
}
We can add color to this example by adding a color variable and using attron() and attroff() to turn on and off the color attributes. Notice that the attributes have to be defined early in the program before they are used.

#include <ncurses.h>

int main(){
     initscr();

     start_color(); /* Start color */
     clear();
     init_pair(1, COLOR_RED, COLOR_BLACK);
     init_pair(2, COLOR_BLUE, COLOR_BLACK);
     int x,y,color;
     for(x=0;x<COLS;x++){
       for(y=0;y<LINES;y++){
         if(color==1) color=2;
         else color=1;
         attron(COLOR_PAIR(color));
         mvaddch(y,x,'X');
         attroff(COLOR_PAIR(color));
       }
     }
     refresh();

     sleep(5);
     endwin();
}

Links that can help

http://web.cs.mun.ca/~rod/ncurses/ncurses.html
http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/
http://heather.cs.ucdavis.edu/~matloff/UnixAndC/CLanguage/Curses.pdf
http://www.mv.com/users/drd/ncurses-intro.html
http://www.linuxselfhelp.com/HOWTO/NCURSES-Programming-HOWTO/color.html
http://snap.nlc.dcccd.edu/learn/fuller3/chap6/chap6.html
http://www.faqs.org/docs/Linux-HOWTO/NCURSES-Programming-HOWTO.html
http://cng.ateneo.edu/cng/wyu/classes/cs1593/gui/ncurses/ncurses_report.pdf
http://artcontext.org/edu/topics/curses/