WIP: detect resizing

This commit is contained in:
Adrien Bourmault 2021-07-22 01:27:40 +02:00
parent 26e93c2f04
commit d7caf04b65
No known key found for this signature in database
GPG Key ID: 6EB408FE0ACEC664
1 changed files with 36 additions and 39 deletions

View File

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