Use nmsterm_get_clearscr() such that we no longer need to return cursor row position from the init function.
modified: src/nms.c modified: src/nmsterm.c modified: src/nmsterm.h modified: src/nmsterm_ncurses.c
This commit is contained in:
parent
089a089bc4
commit
a2a6b28df1
11
src/nms.c
11
src/nms.c
@ -122,7 +122,16 @@ char nms_exec(char *string) {
|
||||
setlocale(LC_ALL, "");
|
||||
|
||||
// Initialize terminal
|
||||
origRow = nmsterm_init_terminal();
|
||||
nmsterm_init_terminal();
|
||||
|
||||
if (!nmsterm_get_clearscr()) {
|
||||
// Get current row position
|
||||
origRow = nmsterm_get_cursor_row();
|
||||
|
||||
// nms_get_cursor_row() may display output in some terminals. So
|
||||
// we need to reposition the cursor to the start of the row.
|
||||
nmsterm_move_cursor(origRow, 0);
|
||||
}
|
||||
|
||||
// Get terminal window rows/cols
|
||||
maxRows = nmsterm_get_rows();
|
||||
|
@ -43,11 +43,9 @@ static int foregroundColor = COLOR_BLUE; // Foreground color settin
|
||||
|
||||
// Function prototypes
|
||||
static void nmsterm_set_terminal(int);
|
||||
static int nmsterm_get_cursor_row(void);
|
||||
|
||||
// Initialize terminal window
|
||||
int nmsterm_init_terminal(void) {
|
||||
int origRow = 0;
|
||||
void nmsterm_init_terminal(void) {
|
||||
|
||||
// Turn off line buffering and echo
|
||||
nmsterm_set_terminal(0);
|
||||
@ -59,18 +57,7 @@ int nmsterm_init_terminal(void) {
|
||||
CLEAR_SCR();
|
||||
CURSOR_HOME();
|
||||
CURSOR_HIDE();
|
||||
} else {
|
||||
|
||||
// Get current row position
|
||||
origRow = nmsterm_get_cursor_row();
|
||||
|
||||
// nms_get_cursor_row() may display output in some terminals. So
|
||||
// we need to reposition the cursor to the start of the row.
|
||||
CURSOR_MOVE(origRow, 0);
|
||||
}
|
||||
|
||||
// Return cursor row position
|
||||
return origRow;
|
||||
}
|
||||
|
||||
void nmsterm_restore_terminal(void) {
|
||||
@ -212,6 +199,39 @@ void nmsterm_set_foregroundcolor(char *c) {
|
||||
foregroundColor = COLOR_BLUE;
|
||||
}
|
||||
|
||||
/*
|
||||
* nms_get_cursor_row() returns the row position of the cursor as reported
|
||||
* by the terminal program via the ANSI escape code
|
||||
*/
|
||||
int nmsterm_get_cursor_row(void) {
|
||||
int i, r = 0;
|
||||
int row = 0;
|
||||
char buf[10];
|
||||
char *cmd = "\033[6n";
|
||||
|
||||
memset(buf, 0, sizeof(buf));
|
||||
|
||||
write(STDOUT_FILENO, cmd, sizeof(cmd));
|
||||
|
||||
r = read(STDIN_FILENO, buf, sizeof(buf));
|
||||
|
||||
for (i = 0; i < r; ++i) {
|
||||
if (buf[i] == 27 || buf[i] == '[') {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (buf[i] >= '0' && buf[i] <= '9') {
|
||||
row = (row * 10) + (buf[i] - '0');
|
||||
}
|
||||
|
||||
if (buf[i] == ';' || buf[i] == 'R' || buf[i] == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return row;
|
||||
}
|
||||
|
||||
/*
|
||||
* nmsterm_set_terminal() turns off terminal echo and line buffering when
|
||||
* passed an integer value that evaluates to true. It restores the
|
||||
@ -242,35 +262,3 @@ static void nmsterm_set_terminal(int s) {
|
||||
|
||||
state = s;
|
||||
}
|
||||
/*
|
||||
* nms_get_cursor_row() returns the row position of the cursor as reported
|
||||
* by the terminal program via the ANSI escape code
|
||||
*/
|
||||
static int nmsterm_get_cursor_row(void) {
|
||||
int i, r = 0;
|
||||
int row = 0;
|
||||
char buf[10];
|
||||
char *cmd = "\033[6n";
|
||||
|
||||
memset(buf, 0, sizeof(buf));
|
||||
|
||||
write(STDOUT_FILENO, cmd, sizeof(cmd));
|
||||
|
||||
r = read(STDIN_FILENO, buf, sizeof(buf));
|
||||
|
||||
for (i = 0; i < r; ++i) {
|
||||
if (buf[i] == 27 || buf[i] == '[') {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (buf[i] >= '0' && buf[i] <= '9') {
|
||||
row = (row * 10) + (buf[i] - '0');
|
||||
}
|
||||
|
||||
if (buf[i] == ';' || buf[i] == 'R' || buf[i] == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return row;
|
||||
}
|
||||
|
@ -9,7 +9,7 @@
|
||||
#define NMSTERM_H 1
|
||||
|
||||
// Function prototypes
|
||||
int nmsterm_init_terminal(void);
|
||||
void nmsterm_init_terminal(void);
|
||||
void nmsterm_restore_terminal(void);
|
||||
int nmsterm_get_rows(void);
|
||||
int nmsterm_get_cols(void);
|
||||
@ -25,6 +25,7 @@ void nmsterm_beep(void);
|
||||
int nmsterm_get_clearscr(void);
|
||||
void nmsterm_set_clearscr(int);
|
||||
void nmsterm_set_foregroundcolor(char *);
|
||||
int nmsterm_get_cursor_row(void);
|
||||
|
||||
|
||||
#endif
|
||||
|
@ -5,6 +5,7 @@
|
||||
* under the terms of the MIT License. See LICENSE for more details.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <ncurses.h>
|
||||
|
||||
// Static settings
|
||||
@ -13,7 +14,7 @@ static int clearScr = 1; // clearScr flag
|
||||
static int foregroundColor = COLOR_BLUE; // Foreground color setting
|
||||
|
||||
// Initialize terminal window
|
||||
int nmsterm_init_terminal(void) {
|
||||
void nmsterm_init_terminal(void) {
|
||||
initscr();
|
||||
cbreak();
|
||||
noecho();
|
||||
@ -23,9 +24,6 @@ int nmsterm_init_terminal(void) {
|
||||
start_color();
|
||||
init_pair(1, foregroundColor, COLOR_BLACK);
|
||||
}
|
||||
|
||||
// Since ncurses always clears the screen, we always return zero.
|
||||
return 0;
|
||||
}
|
||||
|
||||
void nmsterm_restore_terminal(void) {
|
||||
|
Loading…
Reference in New Issue
Block a user