Vocabulary
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, typecc -o begin -l ncurses begin.c |
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 |
#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(); } |
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(); } |
#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(); } |