diff --git a/src/cli.c b/src/cli.c index cc39cac..bb60885 100644 --- a/src/cli.c +++ b/src/cli.c @@ -40,13 +40,12 @@ #define KEY_ESCAPE_1 27 #define KEY_ESCAPE_2 91 #define KEY_ESCAPE_3 51 -#define KEY_ARROW_UP 65 -#define KEY_ARROW_DOWN 66 -#define KEY_ARROW_RIGHT 67 -#define KEY_ARROW_LEFT 68 +#define KEY_ARROW_UP 65 // accessible after sequence KEY_ESCAPE_1, 2 +#define KEY_ARROW_DOWN 66 // accessible after sequence KEY_ESCAPE_1, 2 +#define KEY_ARROW_RIGHT 67 // accessible after sequence KEY_ESCAPE_1, 2 +#define KEY_ARROW_LEFT 68 // accessible after sequence KEY_ESCAPE_1, 2 #define KEY_BACKSPACE 127 -#define KEY_DELETE 126 -// KEY_SUPPR = séquence: 27 91 51 (KEY_ESCAPE_1, 2, 3) puis 126 (KEY_DELETE) +#define KEY_DELETE 126 // accessible after sequence KEY_ESCAPE_1, 2, 3 #define C_CLEARSCREEN "\e[2J" #define C_CLEARLINE "\e[2K" @@ -63,6 +62,7 @@ #define NON_BLOCKING 1 +volatile struct winsize terminalSize; // // Get a character code from the keyboard @@ -126,32 +126,35 @@ static inline int getch(bool nonBlocking) return buf; } + // + // Get the screen size + // + void getScreenSize(int signum) +{ + // Get current terminal size + ioctl(STDOUT_FILENO, TIOCGWINSZ, &terminalSize); +} + // // Print monitor screen // - void decorateMonitor(struct winsize *terminalSize) + void decorateMonitor(int signum) { - printf(C_CLEARSCREEN "\n"); + getScreenSize(signum); - // print decorations - for (int i = 0; i < terminalSize->ws_col; i++) { - printf(C_COLOR_REVERSE " " C_COLOR_NORMAL); - } + printf(C_CLEARSCREEN "\n"); + fflush(stdout); for (int i = 0; - i < (terminalSize->ws_row-2) * terminalSize->ws_col; + i < (terminalSize.ws_row * terminalSize.ws_col); i++) { - if ((i % terminalSize->ws_col == 0) || - (i % terminalSize->ws_col == terminalSize->ws_col - 1)) + if ((i < terminalSize.ws_col) || + (i > terminalSize.ws_row * (terminalSize.ws_col - 1))) printf(C_COLOR_REVERSE " " C_COLOR_NORMAL); else printf(C_COLOR_NORMAL " " C_COLOR_NORMAL); } - for (int i = 0; i < terminalSize->ws_col; i++) { - printf(C_COLOR_REVERSE " " C_COLOR_NORMAL); - } - fflush(stdout); } @@ -169,29 +172,13 @@ int connectedMonitor(int sockfd) char curChar; int answerLength; - struct winsize terminalSize, oldTerminalSize; - - // Get current terminal size - ioctl(STDOUT_FILENO, TIOCGWINSZ, &terminalSize); - memcpy(&oldTerminalSize, &terminalSize, sizeof(struct winsize)); - - decorateMonitor(&terminalSize); - + signal(SIGWINCH ,decorateMonitor); + decorateMonitor(0); while (!pleaseStop) { // Zeroing buffer bzero(sendBuff, sizeof(sendBuff)); - - // Get current terminal size and redecorate if it has changed - ioctl(STDOUT_FILENO, TIOCGWINSZ, &terminalSize); - if (terminalSize.ws_row != oldTerminalSize.ws_row || - terminalSize.ws_col != oldTerminalSize.ws_col - ) { - memcpy(&oldTerminalSize, &terminalSize, sizeof(struct winsize)); - decorateMonitor(&terminalSize); - } - // Read command from terminal curChar = getch(NON_BLOCKING); @@ -226,6 +213,8 @@ int connectedMonitor(int sockfd) /* } */ } printf("%sEnd of monitoring session\n\e[0m", C_COLOR_YELLOW); + + return 0; } // @@ -240,12 +229,15 @@ void connectedCommandLine(int sockfd) int historyIndex = 0, historyModifier; char curChar; - int curLineLength, curPosition, continueReadLine, answerLength; + int curLineLength, curPosition, continueReadLine, answerLength, + promptLength; const char *exitCommand = "exit"; const char *monitorCommand = "monitor"; const char *promptString = C_COLOR_BLUE "gem-graph console" C_COLOR_NORMAL "> "; + promptLength = strlen("gem-graph console"); + for (;;) { // Zeroing buffer bzero(sendBuff, sizeof(sendBuff)); @@ -392,7 +384,9 @@ void connectedCommandLine(int sockfd) break; default: - if (curLineLength < SEND_BUFFER_SIZE && curChar > 0) { + if (curLineLength < MIN(SEND_BUFFER_SIZE, + terminalSize.ws_col - promptLength - 3) + && curChar > 0) { printf("%s", C_SAVE_CURSORPOS); memmove(sendBuff + curPosition + 1, sendBuff + curPosition, @@ -494,6 +488,9 @@ int main(void) printLog("%sConnected to server!\n\n", C_COLOR_GREEN); + signal(SIGWINCH, getScreenSize); + getScreenSize(0); + connectedCommandLine(sockfd); // close the socket