一. Skeleton

#include <ncurses.h>

int main(void){                                                                                                                                         
    /* Initialize ncurses */                                                                                                                            
    initscr();    
   
    /* Properly close ncurses */
    endwin();
    return 0;
}

注意:

1、#include <ncurses.h>

2、–lncurses

3、ncurses.h 包含了下面这些头文件: stdio.h、 unctrl.h、 stdarg.h、 stddef.h

1.1 initscr 与 endwin

initscr 会创建两个屏幕:standard screen(stdscr) 和 current screen(curscr);stdscr 与你终端窗口的尺寸一致,但你向 stdscr 写的东西并不会立刻显示在你的终端窗口上,还需要使用 refresh 函数

curscr 是终端屏幕在 ncurses 的内部表示,refresh 函数会比较 stdscr 与 curscr 的不同,然后更新 curscr

#include <ncurses.h>

int main(void){                                                                                                                                         
    /* Initialize ncurses */                                                                                                                            
    initscr();                                                                                                                                          

    addstr("hello, world.\n");

    getch();
    /* Properly close ncurses */
    endwin();
    return 0;
}

endwin 会撤销 Ncurses 对你终端做的任何改变以及其它一些清理工作。一定不能把它忘了 !!!

二. Writing Text

这里有三个很流行的函数

addch(ch); 
addstr(*str); 
printw(format,var[,var...]);
#include <ncurses.h>                                                                                                                                    

int main(void){                                                                                                                                         
    /* Initialize ncurses */                                                                                                                            
    initscr();                                                                                                                                          
  
    addch('A');
    addch('\n');
    refresh();
    napms(1000);
    
    addstr("hello, world.\n");
    refresh();
    napms(1000);
    
    printw("hello, world.\n");
    refresh();
    getch();
    /* Properly close ncurses */
    endwin();
    return 0;
}

napms 函数会暂停输出,单位是 ms,1 s = 1000 ms

三. move

move(r, c)

move 函数将光标移动到 stdscr 的 r 行,c 列的位置;在 Ncurses 中,每个窗口都有自己的光标,其它窗口光标的移动不影响本窗口

#include <ncurses.h>

class Ncurses{
public:
    virtual ~Ncurses() = default;

public:
    virtual void program(void) const = 0;

    void run(void) const{
        initscr();
        program();
        endwin();
    }
};

class MyProgram : public Ncurses{
public:
    void program(void) const override{
        move(1, 1);
        printw("hello, world.\n");
    }
};

int main(void){
    MyProgram my_program;
    my_program.run();
    return 0;
}

四. reading text

这里有 3 个很流行的函数

getch();
getnstr(*str,length);
scanw(format,var[,var...]);

1、getch 会立刻从终端获取一个字符,不需要按下 enter 键

2、getnstr 从输入中获取 length 个字符,然后放入 str 中,确保 str 有足够的大小,不要忘记 0

3、scanw 与 C 语言中 scanf 的行为一致

class MyProgram : public Ncurses{
public:
    void program(void) const override{
        printw("Please enter the first 7 letters of your first name: ");
        refresh();
        char first_name[8] = {0};
        getnstr(first_name, 7);
        

        printw("Please enter the first 7 letters of your last name: ");
        refresh();
        char last_name[8] = {0};
        getnstr(last_name, 7);
        
        printw("Welcome %s-%s\n", first_name, last_name);
        refresh();
        getch();
    }
};