Introduction to Linux Terminal graphics library Curses and instance analysis

Source: Internet
Author: User
With the popularization of computer knowledge, more and more computer enthusiasts begin to understand and use Linux. The ease and freedom of Linux gives us different feelings. However, we cannot meet the requirements of basic commands.

With the popularization of computer knowledge, more and more computer enthusiasts begin to understand and use Linux .. The ease and freedom of Linux gives us different feelings. However, we are not satisfied with basic commands and operations on user interfaces such as KDE and Gnome. what should we do? Programming, right, programming! For programmers, Linux has a good programming environment: gcc (GNU Compiler Collection) can compile C, C ++, Java and many other languages, and many function libraries can be called in Linux. knowing how to use these function libraries will add many useful tools to your programming toolbox. today, let's take a look at curses, a graphic function library that is widely used in Linux/Unix, instead of a medieval witch.

Friends who have studied TC2.0 before will certainly remember that TC has a Graphics library BGI (Borland Graphics Interface, do you still remember the header file :). we can use it to draw user interfaces and beautiful images under DOS. linux/Unix programming gives people the feeling of being cool. it seems that Linux programming is carried out on black and black terminals. indeed, many Linux experts like to work in the terminal mode. after being familiar with some commands, this mode of work is still very efficient. but it's annoying to watch black and black screens for a long time, especially for Cainiao who are new to Linux like me :) Is there a tool that allows us to compile good-looking images in Linux? The answer is yes. it is curses!

The name of curses originates from "cursor optimization", that is, cursor optimization. it was first written by Bill Joy and Ken Arnold of the University of Berkeley to process the screen display of a game rogue. later, Mark Horton of Bell's lab re-compiled curses in System III Unix. currently, almost all Unix and Linux operating systems have the curses function library. curses also supports mouse and some menu and panel processing. it can be said that curses is the best choice for Linux Terminal graphics programming (for example, the famous text editor vi is based on curses)

OK. Let's talk about it in a few minutes. now let's get started:

First, we should understand that the screen mode used on the terminal is text-based. before using curses, use the initscr () function to initialize the screen. correspondingly, the endwin () function must be called to close the curses state when the program ends.

With this concept, we can write the famous "Hello, world! "Curses version:

  1. /* Convert A very simple example of curses programmingcoder: jellendate: 3-26-2004 into */# include int main () {initscr (); box (stdscr, ACS_VLINE, ACS_HLINE ); /* draw a box */move (LINES/2, COLS/2);/* move the cursor to the center */waddstr (stdscr, "Hello, world! "); Refresh (); getch (); endwin (); return 0 ;}

Haha, I forgot to talk about it just now. we call the curses library in C language (it's no surprise that C is the official Linux language, but you can also use C ++ or Python to call curses, this does not matter ). suppose we save this program as hello. c

But don't rush to use gcc-o hello. c to compile and run (I know you will use gcc. because the curses library is not in the standard path, we need to add the-lcurses connection option, like this:

Gcc-o hello. c-lcurses

Now you can run the program with./hello to see if the window is as expected.

Now let's take a line to analyze the code.

# Include/* This is the header file that each curses program must contain, indicating that the curses library is used */

Then, the first initscr () in the main function initializes the screen to start working in curses graphical mode.

In fact, we do not have our own window. We use the stdscr standard screen (just like the stdin standard input and stdout standard output in C ), it's the computer screen in front of us (but it's still black)

Box (stdscr, ACS_VLINE, ACS_HLINE); draws a box. with this box, we feel like a "form. stdscr is the standard screen. ACS_VLINE and ACS_HLINE represent the basic elements on both sides of the box, you can also use ''' | ''' and '''-''', but there may be no ACS_VLINE. ACS_HLINE looks good.

Move (LINES/2, COLS/2); waddstr (stdscr, "Hello, world! ");

Move the cursor to the center of the screen, and then output "Hello, world! "

LINES and COLS are the macros defined by curses, representing the maximum number of rows and columns on the current screen. the Function of waddstr () is to print the string "hello, world!" on stdscr! "

The screen is divided into physical screens (what we see) and logical screens (in memory). when we call a function, we modify the logic screen, which is not displayed on the current physical screen. so now there is nothing on the screen, and we need to call refresh () to display our changes to the logic screen on the physical screen (display. use getch () to pause the screen.

Finally, call endwin () to end curses and restore the original screen.

Well, we have completed the analysis of the first example. is it quite simple?

But what is unsatisfactory is the screen or the way it looks. there is no color. do you want to add it? That is also quite easy: first use the start_color () function to enable the color mode, and then set the color we want.

The color in curses is paired, and a background color is required for a foreground color. use init_pair () before initialization.

For example, init_pair (1, COLOR_BLUE, COLOR_GREEN); defines a set of colors, COLOR_BLUE is the foreground color, and COLOR_GREEN is the background color. 1 is their markup number (used by other functions). let's look at a simple example:

  1. /* Initialize A simple curses color demo programCoder: jellenDate: 3-26-2004 running */# include int main () {initscr ();/* initialize the screen */if (start_color () = OK)/* enable color */{init_pair (1, COLOR_RED, COLOR_GREEN);/* create a color pair */attron (COLOR_PAIR (1 )); /* enable character output color */move (LINES/2, COLS/2); waddstr (stdscr, "Yet another Hell O, world! "); Attroff (COLOR_PAIR (1);/* Turn Off color Display */refresh ();} else {waddstr (stdscr," Can not init color "); refresh ();} endwin ();/* close curses status */return 0 ;}

This program is saved as color. c.

Then you can compile it like this: gcc-o color. c-lcurses

Use./color to run a colored word? Well, you may be able to write exquisite programs using curses in the future.

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.