Install and use the curses library in Linux

Source: Internet
Author: User
Tags nonl
The curses library is a set of functions that programmers can use to set the cursor position and Character styles displayed on the terminal screen. The curses library was initially developed by the UCB development team. Most programs that control the terminal screen use curses. The database, once composed of a set of simple functions, now contains many complex features.

Ubuntu installation method:

Sudo apt-Get install libncurses5-dev

The basic usage of curses is as follows:

1. include header file: Curses. h

2. When compiling, add the link statement-lcurses, for example, GCC temp. C-o temp-lcurses.

3. Important functions:

Initscr (): Initialize the curses library and ttty. (Before starting curses programming, you must use the initscr () function to enable the curses mode)

Endwin (): Disable curses and reset TTY. (The last function called at the end of curses programming)

 

Cbreak (): When the cbreak mode is enabled, all input characters except delete or Ctrl are read one by one.

Crmode (): enables the terminal to enter the cbreak mode.
Both raw () and cbreak () can disable row buffering. The difference is: in raw () function mode, when processing control characters such as ctrlz, terminal, or exit (ctrlc, it is directly transmitted to the program for processing without generating terminal signals. In cbreak () function mode, the control characters are interpreted by the terminal driver as other characters.

 

Move (Y, X): move the cursor to the position of X and Y.
Getyx (Win, Y, X): Get the current cursor position. (Please note! Is y, X instead of & Y, & X)
Clear () and erase (): Clear the entire screen. (use refresh () with caution)


Echochar (CH): displays a character.

Int noecho (void): The input character is not echo.

 

NL ()/nonl (): Specifies whether the line break is a carriage return character. The NL function uses line breaks as the carriage return, while nonl does not.

Addch (CH): Draw the character Ch at the current position
Mvaddch (Y, X, CH): displays a character on (x, y). It is equivalent to calling move (Y, x); addch (CH );
Addstr (STR): draws the STR string at the current position.
Mvaddstr (Y, X, STR): displays a string on (x, y). It is equivalent to calling move (Y, x); addstr (STR );

Printw (format, STR): similar to printf (), output to the screen in a certain format.
Mvprintw (Y, X, format, STR): printw at the (x, y) position. equivalent to calling move (Y, x); printw (format, STR );

Getch (): Read a character from the keyboard. (note! Returns an integer)
Getstr (): reads a string of characters from the keyboard.
Scanw (format, & arg1, & arg2. ..): reads a string of characters from the keyboard, just like scanf.

 

Int mvhline (int x, int y, chtype CH, int N );
Int mvvline (int x, int y, chtype CH, int N); draw a line consisting of n ch at the cursor position (x, y. Mvhlin draws a horizontal line and mvvline draws a vertical line. The cursor position remains unchanged. If the call is successful, OK is returned; otherwise, err is returned.

Beep (): a beep.
Box (Win, win, CH2): automatically draws a box

 

Intrflush (window * Win, bool BF): Win is the standard output. When BF is true, enter break to speed up the interrupt response. However, the output information may be messy.

Refresh (): displays the screen as intended. Compare the differences between the working screen and the real screen, and then refresh sends out the characters and control codes that make the real screen consistent with the working screen through the terminal drive, output the image on the virtual screen to the terminal screen. (The Working screen is like the disk cache, and most functions in curses only modify it.) If the call is successful, OK is returned; otherwise, err is returned.


Standout (): Start standout mode (usually enable screen hair color)

Standend (): Disable standout Mode

Set of common initialization functions:

Void initial ()
{
Initscr (); // enable the curses Mode
Cbreak (); // enable the cbreak mode. All input characters except delete or Ctrl are read one by one.
Nonl (); // used to determine whether the return key is mapped to a newline character when data is input.
Noecho (); // echo () and noecho (): This function is used to control whether a character is displayed on the terminal when a token is input from the keyboard.
Intrflush (stdscr, false );
Keypad (stdscr, true); // when keypad is enabled, you can use some special characters on the keyboard, such as Up, down, left, right, and other direction keys.
Refresh (); // clears the screen
}

# Include <stdio. h>
# Include <curses. h>

Int main ()
{
Initscr ();
Clear ();
Move (10, 20 );
Addstr ("Hello, world ");
Move (lines-1, 0 );
Refresh ();
Getch ();
Endwin ();
Return 0;
}

 

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.