Basic Knowledge
In the era of widespread use of teletype machines, teletype machines acted as output terminals connected to central computers via cables. Users had to send a series of specific control commands to the terminal program in order to control the output on the terminal screen. For example, changing the cursor position on the screen, clearing the content of a certain area on the screen, scrolling the screen, switching display modes, underlining text, changing the appearance, color, brightness, and so on of characters. These controls are implemented through a string called an escape sequence. The escape sequences are called so because these continuous bytes start with a 0x1B
character, which is the escape character (the character entered by pressing the ESC
key). Even now, we can simulate the output effect of teletype terminals from that era by inputting escape sequences into terminal emulation programs. If you want to display a piece of text on the terminal (or terminal emulation program) with colored background, you can input the following escape sequence into your command prompt:
echo "^[[0;31;40mIn Color"
Here, ^
and [
are the so-called escape characters. (Note: in this case, ^[
is one character. It is not entered by typing the ^
and [
characters sequentially. To print this character, you must first press Ctrl+V
and then press the ESC
key.) After executing the above command, you should see the background of In Color
changed to red. From then on, all displayed text will be outputted with this effect. If you want to terminate this effect and return to the original format, you can use the following command:
echo "^[[0;37;40m"
Now do you know the purpose of these characters (escape sequences)? (Try changing the parameters between the semicolons and see what results you get.) Maybe it will be different from what you imagine? It may be because the terminal environment is different, which depends on the different terminals or operating systems. (You can't make a monochrome terminal display colored characters, right?) In order to avoid such compatibility issues and achieve consistent output on different terminals, the designers of UNIX invented a mechanism called termcap
. termcap
is actually a file that is released together with the escape sequences. This file lists all the escape sequences that the current terminal can execute correctly, ensuring that the execution result of the input escape sequences conforms to the specifications in this file. However, in the years following the invention of this mechanism, another mechanism called terminfo
gradually replaced termcap
. Since then, users no longer need to consult the complex escape sequence specifications in termcap
when programming, and only need to access the terminfo
database to control the screen output.
Assuming that all applications access the terminfo
database to control output (such as sending control characters, etc.) under the situation of using terminfo
, soon these code calls will make the entire program difficult to control and manage. The emergence of these problems led to the birth of CURSES
. The name CURSES
comes from a pun called cursor optimization
.
The CURSES
library provides users with a flexible and efficient API (application programming interface) by encapsulating the underlying control codes (escape sequences) of the terminal, which allows users to control the cursor, create windows, change foreground and background colors, and handle mouse operations. This enables users to bypass those annoying low-level mechanisms when writing applications on character terminals.
NCURSES
is a clone of CURSES
from System V Release 4.0 (SVr4)
. It is a freely configurable library that is fully compatible with older versions of CURSES
. In short, it is a library that allows applications to directly control the display of the terminal screen. When the CURSES
library is mentioned later, it also refers to the NCURSES
library.
NCURSES
not only encapsulates the underlying terminal functions, but also provides a fairly stable working framework to generate beautiful interfaces. It includes functions for creating windows. And its sister libraries, Menu
, Panel
, and Form
, are extensions of the CURSES
base library. These libraries are generally distributed together with CURSES
. We can build an application that contains multiple windows, menus, panels, and forms. The windows can be managed independently, such as scrolling or hiding them. Menus allow users to create command options for easy execution of commands. Forms allow users to create windows for simple data input and display. Panels are extensions of NCURSES window management functions, and can overlay or stack windows.