add ncurses wrapper

This commit is contained in:
Emile Clark-Boman 2025-09-10 22:49:05 +10:00
parent 361d63be94
commit c3074a75eb
3 changed files with 99 additions and 1 deletions

View file

@ -55,7 +55,7 @@ int mkpty(int *fdmx, int *fds) {
goto fail;
}
// Propagate file descriptors via parameters
/* Propagate file descriptors via parameters */
*fdmx = _fdmx;
*fds = _fds;
return EXIT_SUCCESS;

77
cli/curse.c Normal file
View file

@ -0,0 +1,77 @@
#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 */
*stdscr = initscr();
start_color();
/* Screen Configuration */
termmode(WMODE_CBREAK);
noecho(); // manually echo <- getch(3x)
nodelay(*stdscr, TRUE); // getch(3x) nonblocking IO
keypad(*stdscr, TRUE); // allow function keys
// intrflush(3x) - flush terminal input buffer on interrupt key
intrflush(*stdscr, FALSE);
curs_set(0); // hide cursor
// init_pair(1, COLOR_BLACK, COLOR_CYAN);
}
int main(int argc, char *argv[]) {
WINDOW *stdscr;
crs_init(&stdscr);
// 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;
}

21
cli/curse.h Normal file
View file

@ -0,0 +1,21 @@
#ifndef DORNE_CURSE_H
#define DORNE_CURSE_H
/* libncurses with wide-character support. */
#include <ncursesw/ncurses.h>
enum crs_mode {
/* tty cbreak mode */
WMODE_CBREAK,
/* tty raw mode */
WMODE_RAW,
/* tty cooked mode (ISIG & IXON not modified)*/
WMODE_NOCBREAK,
/* tty cooked mode (ISIG & IXON set) */
WMODE_NORAW,
};
int termmode(enum crs_mode mode);
void init_ncurses(void);
#endif /* DORNE_CURSE_H */