Last Updated:

Writing the first program on Ncurses

 

Ncurses is a library designed to manage terminal I/O. Allows you to specify the screen coordinates and color of the displayed characters. This article is a translation of this material. Write in the comments if you find an error or inaccuracy in the translation.

The material is designed primarily for users of Unix-like operating systems. However, for Windows users, there is a clone of the library – PDcurses.ncurses

 

History

In ancient times, all the few capabilities of terminals (teletype terminals) were supported using sequences of bytes. These control sequences, commonly referred to as -sequences, began with a special character. Echoes of that time have reached us. Today, we can do the same with our terminal (actually a terminal emulator, but for brevity, here and hereafter, called a terminal) using exactly the same technology.escape0x1B

Let's look at an example. Let's say you want to write the line "In color" in red. To do this, let's write in the terminal (works only for UNIX-like systems!):

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

The first character is a -symbol ("^[", guess for yourself which HEX-code this symbol has). To type it in the terminal, you need to press , and then . Then type as is.escapeCTRL+VESC

The result of the command: Displaying Color Text in Terminal

The same trick can be pulled off programmatically:

#include <iostream>

int main()
{
    std::cout << "\x1B[0;31;40mIn color" << std::endl;
    return 0;
}

In -characters are specified by a symbol (for example, - moving to a new line, - returning the carriage). Accordingly, to drive the -symbol 0×1B into the stream, you need to enter \x1B.С\C++escape\\n\rescape

Let's imagine that you wanted to use different colors in your program to make the program look more beautiful than usual. Wouldn't it be difficult to remember the meanings of each control character?

And besides, these symbols are most likely different for each type of terminal. It was in order to avoid memorizing the meaning of each symbol, as well as to write code that does not depend on the type of terminal, that the .ncurses

What is ncurses and what is it eaten with?

Initially, there was a library called (the name comes from the abbreviation "cursor optimization"). This library worked directly with the terminal, using the system's API, and at the same time allowed developers not to care about different types of terminals. The library allowed the application to move the cursor, create windows (not GUI), control color, control the mouse, etc. without using system-specific control symbols.curses

ncurses is a clone of the original System V Release 4.0 (SVr4), which in turn was an improvement on the classic one (the name comes from "new curses")cursescursesBSD

But it's not only a wrapper over system-specific control characters, it's also a good framework for creating a beautiful text-mode user interface. The library contains window and menu management functions, as well as a "sister library" that controls the panels.ncursespanel

Here is an example of an application based on : 

ncurses

Ncurses

Of course, this is not all the possibilities provided by this library. As we move forward, we'll see a lot more, no doubt :)

Installing ncurses

Installation is very simple. To do this, open the terminal and write:ncurses

sudo apt-get install libncursesw5-dev

All. The library is installed and now we are ready to write code.

Hello world! (Hello world!)

So, welcome to the world of . Before diving into the library, let's write a simple program that will say "Hello!" to the whole world.ncurses

I almost forgot, to compile programs that use the library, you need to set the flag :ncurses-lncurses

gcc <программа> -lncurses

Program code:

#include <ncurses.h>

int main()
{
     initscr(); // Change to curses mode
     printw("Hello world!\n"); // Display greeting in buffer
     refresh(); // Display the greeting on the real screen
     getch(); // Waiting for the user to press some key
     endwin(); // Exit curses mode. Required command.
     return 0;
}

Description of the code

ncurses.h

 

ncurses.h is the header file. It declares all the functions and constants of libraries. Must be connected.ncurses

initscr()

 

The function puts the terminal into -mode. It cleans the screen, allocates memory for the necessary data for the library to work, and initializes the window (this is not about today, unfortunately). A call is required before any use of the . In some cases, a function may fail if there is not enough memory. However, such situations are unlikely, given the resources provided to modern computers and the resources required for the normal operation of the library.initscr()cursesstdscrinitscr()ncurses

printw()

 

The function performs the same role as , and has exactly the same syntax. Why is it used ? Because they don't work in -mode. Otherwise, this function is completely similar to . For reference: the input is carried out using the function (similar to ).printw()printf()printw()std::coutprintf()cursesprintf()scanw()scanf()

refresh()

 

The function does not send data directly to the screen, but to the so-called buffer. The function checks the buffer and, if it is not empty, sends information to the screen (updates only the changed areas of the screen), it is somewhat similar to the . Often, calling it is not necessary, but sometimes without calling this function, you can get strange errors. printw()refresh()flush()stdio

getch()

 

The function is probably familiar to everyone from the library. So, this is its full analogue (even a little better, as we will see later). The function waits for any key on the keyboard to be pressed and returns the code of that key.getch()conio.h

endwin()

 

At the end, do not forget to exit the -mode, otherwise, after the program is completed, your terminal will behave "strangely". Exiting -mode is handled by the . It clears the screen, frees up the memory it allocated, and puts the terminal into normal mode (the one it was in before the program ran).cursescursesendwin()initscr()