From a2a6b28df13d516e369baf47f8d088445659ab34 Mon Sep 17 00:00:00 2001 From: Brian Barto Date: Wed, 18 Jan 2017 19:31:59 -0500 Subject: [PATCH] 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 --- src/nms.c | 11 +++++- src/nmsterm.c | 80 ++++++++++++++++++------------------------- src/nmsterm.h | 3 +- src/nmsterm_ncurses.c | 6 ++-- 4 files changed, 48 insertions(+), 52 deletions(-) diff --git a/src/nms.c b/src/nms.c index 449e49a..91cd4b3 100644 --- a/src/nms.c +++ b/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(); diff --git a/src/nmsterm.c b/src/nmsterm.c index 1785947..dacd410 100644 --- a/src/nmsterm.c +++ b/src/nmsterm.c @@ -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; -} diff --git a/src/nmsterm.h b/src/nmsterm.h index 572ffbe..8fce4bc 100644 --- a/src/nmsterm.h +++ b/src/nmsterm.h @@ -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 diff --git a/src/nmsterm_ncurses.c b/src/nmsterm_ncurses.c index 1de7aa2..b83cc80 100644 --- a/src/nmsterm_ncurses.c +++ b/src/nmsterm_ncurses.c @@ -5,6 +5,7 @@ * under the terms of the MIT License. See LICENSE for more details. */ +#include #include // 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) {