Ncurses
ncurses (new curses)는 프로그래머가 텍스트 사용자 인터페이스를 터미널 독립 방식으로 기록할 수 있도록 API를 제공하는 프로그래밍 라이브러리이다. 단말 에뮬레이터에서 실행하는 GUI같은 응용 소프트웨어를 개발하는 툴킷이다. 원격 셸을 이용하면 겪을 수 있는 레이턴시를 줄이기 위해 화면의 변화도 최적화한다.
How to install
Hello world
#include <ncurses.h>
int main()
{
initscr(); // Start curses mode
printw("Hello World !!!"); // Print Hello World
refresh(); // Print it on to the real screen
getch(); // Wait for user input
endwin(); // End curses mode
return 0;
}
Non-blocking keyboard input
Attribute
-
attron()
: 속성에 대한 Bit-mask를 ON 한다. -
attroff()
: 속성에 대한 Bit-mask를 OFF 한다.
Attribute Bit-mask
- A_NORMAL: Normal display (no highlight)
- A_STANDOUT: Best highlighting mode of the terminal.
- A_UNDERLINE: Underlining
- A_REVERSE: Reverse video
- A_BLINK: Blinking
- A_DIM: Half bright
- A_BOLD: Extra bright or bold
- A_PROTECT: Protected mode
- A_INVIS: Invisible or blank mode
- A_ALTCHARSET: Alternate character set
- A_CHARTEXT: Bit-mask to extract a character
- COLOR_PAIR(n): Color-pair number n
attron vs attrset
그럼 대체 attron() 함수와 attrset() 함수의 차이는 무엇일까? attron 이 그것에 주어진 속성만을 켤 수 있는데 반해 attrset 은 윈도우의 속성들을 설정할 수 있다. 즉 attset() 는 윈도우가 현재 어떤 속성을 가지고 있는지 상관없이 새 속성들을 설정할 수 있다. attroff() 또한 그것에 인자로 주어진 속성만을 끌 수 있다. 이런 기능은 속성을 쉽게 다룰 수 있는 유용함을 제공하는 반면, 만약 당신이 주의깊게 그것을 사용하지 않는다면 윈도우가 어떤 속성을 가지고 있는지 놓칠 수도 있고 화면표시를 모두 망칠 수도 있을 것이다. 이것 특히 메뉴를 다룰때에 잘 일어나는 문제다. 그러니 미리 속성에 대한 정책을 세워놓고 그것을 고수하도록 하라. 아 또한 모든 속성을 끄고 일반 모드로 돌려놓는 attrset(A_NORMAL) 함수와 동일한 기능의 standend() 함수를 언제든지 호출할 수도 있다.
Color
- COLOR_BLACK: 0
- COLOR_RED: 1
- COLOR_GREEN: 2
- COLOR_YELLOW: 3
- COLOR_BLUE: 4
- COLOR_MAGENTA: 5
- COLOR_CYAN: 6
- COLOR_WHITE: 7
Example
#include <ncurses.h>
#include <unistd.h> /* only for sleep() */
int kbhit(void)
{
int ch = getch();
if (ch != ERR) {
ungetch(ch);
return 1;
} else {
return 0;
}
}
int main(void)
{
initscr();
cbreak();
noecho();
nodelay(stdscr, TRUE);
scrollok(stdscr, TRUE);
while (1) {
if (kbhit()) {
printw("Key pressed! It was: %d\n", getch());
refresh();
} else {
printw("No key pressed yet...\n");
refresh();
sleep(1);
}
}
}
Troubleshooting
curses.h no such file or directory
curses.h
파일을 찾지 못할 경우 CentOS에서는 아래와 같이 ncurses 라이브러리를 설치한다.
Download
- Ncurses-wrapper-class-example.zip
- NCurses wrapper class example. (by. lcw)