这是 ncurses 的示例代码。我在linux下测试过它,但它也应该在mac os下工作。
#include <stdlib.h>
#include <stdio.h>
#include <curses.h>
int main(void) {
WINDOW * mainwin;
if ( (mainwin = initscr()) == NULL ) {
fprintf(stderr, "Error initialising ncurses.\n");
exit(EXIT_FAILURE);
}
mvaddstr(13, 33, "Input: ");
refresh();
char input[2];
input[0] = getch();
input[1] = '\0';
mvaddstr(15, 33, "Your Input is: ");
mvaddstr(15, 48, input);
mvaddstr(17, 33, "Press any key to exit");
getch();
delwin(mainwin);
endwin();
refresh();
return EXIT_SUCCESS;
}
有必要链接到 ncurses。我使用 cmake 来管理我的构建:
cmake_minimum_required(VERSION 3.5)
project(ncurses)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic -Wextra")
find_package(Curses REQUIRED)
add_executable(ncurses main.cpp)
target_link_libraries(ncurses ${CURSES_LIBRARIES})