Ncurses Programming Howto

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

NCURSES Programming HOW TO

1. Introduction

In the olden days of teletype terminals, terminals were away from computers and were connected to them
through serial cables. The terminals could be configured by sending a series of bytes. All the capabilities (such
as moving the cursor to a new location, erasing part of the screen, scrolling the screen, changing modes etc.)
of terminals could be accessed through these series of bytes. These control seeuqnces are usually called
escape sequences, because they start with an escape(0x1B) character. Even today, with proper emulation, we
can send escape sequences to the emulator and achieve the same effect on a terminal window.

Suppose you wanted to print a line in color. Try typing this on your console.

echo "^[[0;31;40mIn Color"

The first character is an escape character, which looks like two characters ^ and [. To be able to print it, you
have to press CTRL+V and then the ESC key. All the others are normal printable characters. You should be able
to see the string "In Color" in red. It stays that way and to revert back to the original mode type this.

echo "^[[0;37;40m"

Now, what do these magic characters mean? Difficult to comprehend? They might even be different for
different terminals. So the designers of UNIX invented a mechanism named termcap. It is a file that lists all the
capabilities of a particular terminal, along with the escape sequences needed to achieve a particular effect. In
the later years, this was replaced by terminfo. Without delving too much into details, this mechanism allows
application programs to query the terminfo database and obtain the control characters to be sent to a
terminal or terminal emulator.

1.1. What is NCURSES?

You might be wondering, what the import of all this technical gibberish is. In the above scenario, every
application program is supposed to query the terminfo and perform the necessary stuff (sending control
characters etc.). It soon became difficult to manage this complexity and this gave birth to 'CURSES'. Curses is a
pun on the name "cursor optimization". The Curses library forms a wrapper over working with raw terminal
codes, and provides highly flexible and efficient API (Application Programming Interface). It provides functions
to move the cursor, create windows, produce colors, play with mouse etc. The application programs need not
worry about the underlying terminal capabilities.

So what is NCURSES? NCURSES is a clone of the original System V Release 4.0 (SVr4) curses. It is a freely
distributable library, fully compatible with older version of curses. In short, it is a library of functions that
manages an application's display on character-cell terminals. In the remainder of the document, the terms
curses and ncurses are used interchangeably.

A detailed history of NCURSES can be found in the NEWS file from the source distribution. The current package
is maintained by Thomas Dickey. You can contact the maintainers at [email protected].

1.2. What we can do with NCURSES


NCURSES not only creates a wrapper over terminal capabilities, but also gives a robust framework to create
nice looking UI (User Interface)s in text mode. It provides functions to create windows etc. Its sister libraries
panel, menu and form provide an extension to the basic curses library. These libraries usually come along with
curses. One can create applications that contain multiple windows, menus, panels and forms. Windows can be
managed independently, can provide 'scrollability' and even can be hidden.

Menus provide the user with an easy command selection option. Forms allow the creation of easy-to-use data
entry and display windows. Panels extend the capabilities of ncurses to deal with overlapping and stacked
windows.

These are just some of the basic things we can do with ncurses. As we move along, We will see all the
capabilities of these libraries.

1.3. Where to get it

All right, now that you know what you can do with ncurses, you must be rearing to get started. NCURSES is
usually shipped with your installation. In case you don't have the library or want to compile it on your own,
read on.

You might also like