Using the curses library to manage text-based screens

Source: Internet
Author: User

The Curses function library provides a terminal-independent way to write full-screen character-based programs. Curses also manages the keyboard, providing an easy-to-use non-blocking character input mode.

The Curses function library optimizes the movement of the cursor and minimizes the need to refresh the screen, thus reducing the number of characters that must be sent to the character terminal.

The curses library has several different implementations. Linux uses ncurses (new curses), which can be installed using the sudo apt-get install Libncurses5-dev.

Use the-lncurses option at compile time to link the Curses function library

GCC hello.c-lncurses
1 #include <ncurses.h>23IntMain ()4{5 INITSCR ();  6 Move (5, 15);  7 printw (%s ", " hello world ");  8  Refresh ()  9  Getch (); 10  Endwin ();  One return 0;12}                  

The following is displayed after running:

The concept of curses

The curses routines work on screens, windows, and child windows. The so-called screen is the device you're writing (usually a terminal screen or a xterm screen). The screen occupies all of the available display area on the device, and of course, if the device is a terminal window in the X window, then the screen is all the available character positions within that terminal window. Whenever there is at least one curses window, we call it STDSCR, which is exactly the same size as the physical screen. You can create some windows that are smaller than the screen, and the windows can overlap each other, and they can have their own multiple child windows, but each child window must always be contained within its parent window.

The Curses function library uses two data structures to map the terminal screens, which are STDSCR and CURSCR. In both, STDSCR is more important, and it is refreshed when the curses function produces output. The STDSCR data structure corresponds to the "standard screen", which is the default Output window in the curses program. The CURSCR data structure is similar to STDSCR, but it corresponds to what the current screen looks like. Content that is output to STDSCR is not displayed on the screen until the program calls the Refresh function. The curses library compares the differences between STDSCR (what the screen will look like) and CURSCR (what the screen looks like) when calling the Refresh function, and then refreshes the screen based on that difference.

The layout of the logical screen is implemented by a character array, which starts with the upper-left corner of the screen-coordinates (0,0) and is organized by line and column numbers. All curses functions use coordinates that are Y-values (line numbers) before, and X-values (column values) in the back.

An example is detailed:

WINDOW *INITSCR (void);

The INITSCR function initializes the terminal screen to curses mode, which clears all characters on the screen and waits for the next process. So before calling the other curses functions, call the INITSCR () function to initialize the screen. If successful, returns a pointer to the STDSCR structure, and if it fails, outputs a diagnostic error message and causes the program to exit.

int move (int new_x);

The move function is used to move the position of the logical cursor to the specified location.

int PRINTW (char* format, ...);

Format the string in the same way as the printf function, and then add it to the cursor's current position.

int refresh (void);

When printing with the PRINTW function, the string is actually printed to the STDSCR virtual window, not directly to the screen, but in the STDSCR buffer. In order to display the data in these buffers, you must use the Refresh () function to tell the curses system to output the contents of the buffer to the screen.

int getch (void);

Gets a character.

int Endwin (void);

Exit curses mode to release the memory occupied by the curses subsystem and related data structures.

Using the curses library to manage text-based screens

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.