dorne/cli/curse.c

84 lines
1.7 KiB
C
Raw Normal View History

2025-09-10 22:49:05 +10:00
#include <locale.h>
#include <unistd.h>
#include "curse.h"
#define BASE_DELAY 1000000
#define CRS_LOCALE "en_US.UTF-8"
/* Set ncurses terminal mode (buffering, character processing,
* Key->SIG handling, and other termios(3) functionality).
*/
inline int termmode(enum crs_mode mode) {
switch (mode) {
case WMODE_CBREAK:
return cbreak();
case WMODE_RAW:
return raw();
case WMODE_NOCBREAK:
return nocbreak();
case WMODE_NORAW:
return noraw();
/* defaulting is not possible. */
}
}
/* Initialise ncurses for our usecase.
* WARNING: This function should only be called once.
*/
inline void init_ncurses(void) {
// ncurses expects a locale for consistent behaviour
setlocale(LC_ALL, CRS_LOCALE);
/* NCurses Init */
start_color();
/* Screen Configuration */
termmode(WMODE_CBREAK);
noecho(); // manually echo <- getch(3x)
curs_set(0); // hide cursor
// init_pair(1, COLOR_BLACK, COLOR_CYAN);
}
WINDOW *init_window(int x, int y, int width, int height) {
WINDOW *win = newwin(height, width, y, x);
nodelay(win, TRUE); // getch(3x) nonblocking IO
keypad(win, TRUE); // allow function keys
// intrflush(3x) - flush terminal input buffer on interrupt key
intrflush(win, FALSE);
return win;
}
2025-09-10 22:49:05 +10:00
int main(int argc, char *argv[]) {
init_ncurses();
WINDOW *stdscr = init_window(0, 0, 0, 0);
2025-09-10 22:49:05 +10:00
// Set background
wbkgd(stdscr, COLOR_PAIR(1));
attron(COLOR_PAIR(1));
attron(A_BOLD);
int counter = 0;
while (1) {
mvprintw(0, 0, "COUNTER %d", counter++);
refresh();
switch (getch()) {
case 'q':
goto end;
default:
break;
}
usleep(BASE_DELAY);
}
end:
endwin();
return 0;
}