Better cli monitor layout

This commit is contained in:
Adrien Bourmault 2021-08-06 01:12:01 +02:00
parent 8500cddbf3
commit c8d1cb1038
No known key found for this signature in database
GPG Key ID: 6EB408FE0ACEC664
1 changed files with 81 additions and 131 deletions

212
src/cli.c
View File

@ -20,15 +20,12 @@
//=-------------------------------------------------------------------------=//
#include "../include/base.h"
#include "../include/terminal.h"
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/socket.h>
#include <unistd.h>
#include <termios.h>
#include <fcntl.h>
#define SEND_BUFFER_SIZE 80
#define RECEIVE_BUFFER_SIZE 80 * 24
@ -47,126 +44,85 @@
#define KEY_BACKSPACE 127
#define KEY_DELETE 126 // accessible after sequence KEY_ESCAPE_1, 2, 3
#define C_CLEARSCREEN "\e[2J"
#define C_CLEARLINE "\e[2K"
#define C_CURSORLEFT "\e[1D"
#define C_CURSORRIGHT "\e[1C"
#define C_SAVE_CURSORPOS "\e7"
#define C_RESTORE_CURSORPOS "\e8"
#define C_COLOR_RED "\e[01;31m"
#define C_COLOR_YELLOW "\e[00;33m"
#define C_COLOR_GREEN "\e[00;32m"
#define C_COLOR_BLUE "\e[01;34m"
#define C_COLOR_REVERSE "\e[7m"
#define C_COLOR_NORMAL "\e[0m"
#define NON_BLOCKING 1
volatile struct winsize terminalSize;
//
// Get a character code from the keyboard
//
static inline int getch(bool nonBlocking)
{
int buf = 0;
// old terminal
struct termios old = {0};
// force flush stdout
fflush(stdout);
// Set non-blocking mode if asked
if(nonBlocking)
fcntl(0, F_SETFL, O_NONBLOCK);
if(tcgetattr(0, &old) < 0) {
printLog("%sError getting terminal settings! (%s)\n",
C_COLOR_RED,
strerror(errno));
return -1;
}
old.c_lflag &= ~ICANON; // disable buffered I/O
old.c_lflag &= ~ECHO; // set no echo mode
if(tcsetattr(0, TCSANOW, &old) < 0) {
printLog("%sError setting terminal settings! (%s)\n",
C_COLOR_RED,
strerror(errno));
return -1;
}
buf = getchar();
if(buf < 0) {
// Check target busy (try again)
if(errno == EAGAIN)
return 0;
printLog("%sError reading character! (%s)\n",
C_COLOR_RED,
strerror(errno));
return -1;
}
old.c_lflag |= ICANON; // enable buffered I/O
old.c_lflag |= ECHO; // set echo mode
if(tcsetattr(0, TCSADRAIN, &old) < 0) {
printLog("%sError resetting terminal settings! (%s)\n",
C_COLOR_RED,
strerror(errno));
return -1;
}
// Reset blocking mode
if(nonBlocking)
fcntl(0, F_SETFL, 0);
return buf;
}
//
// Get the screen size
//
void getScreenSize(int signum)
{
// Get current terminal size
ioctl(STDOUT_FILENO, TIOCGWINSZ, &terminalSize);
}
//
// Set cursor location
//
void setCursorLocation(char x, char y)
{
printf("\x1b[%d;%dH", y, x);
}
//
// Print monitor screen
//
void decorateMonitor(int signum)
{
const char titleText[] = "GEM-GRAPH MONITOR";
const char screenTitleText[] = "Model view";
const char infosTitleText[] = "Model informations";
const char infosNameText[] = "Name ";
const char infosStatusText[] = "Status ";
const char infosOwnerText[] = "Owner ";
const char infosSchedulerText[] = "Scheduler id";
const char infosScThreadsText[] = "+ threads ";
getScreenSize(signum);
printf(C_CLEARSCREEN);
clearScreen();
setCursorLocation(1,1);
printf("%sMONITOR",
C_COLOR_GREEN C_COLOR_REVERSE
);
for (int i = 0; i < terminalSize.ws_col - sizeof("MONITOR") + 1; i++) {
printf(C_COLOR_NORMAL C_COLOR_WHITE_ON_BLUE);
for (int i = 0; i < terminalSize.ws_col; i++) {
printf(" ");
}
printf(C_COLOR_NORMAL);
setCursorLocation(1,terminalSize.ws_row - 1);
printf("%s",
C_COLOR_GREEN C_COLOR_REVERSE
setCursorLocation(1,1);
printf("%s%s",
C_COLOR_NORMAL C_COLOR_WHITE_ON_BLUE,
titleText
);
setCursorLocation(1,2);
printf(C_COLOR_NORMAL C_COLOR_BLACK_ON_GREEN);
for (int i = 0; i < terminalSize.ws_col; i++) {
printf(" ");
}
setCursorLocation(1,2);
printf("%s%s",
C_COLOR_NORMAL C_COLOR_BLACK_ON_GREEN,
screenTitleText
);
setCursorLocation(terminalSize.ws_col / 3 * 2, 2);
printf("%s%s (void)",
C_COLOR_NORMAL C_COLOR_BLACK_ON_GREEN,
infosTitleText
);
setCursorLocation(terminalSize.ws_col / 3 * 2, 3);
printf("%s%s%s (void)",
C_COLOR_NORMAL C_COLOR_BLACK_ON_LIGHTGREY,
infosNameText,
C_COLOR_NORMAL
);
setCursorLocation(terminalSize.ws_col / 3 * 2, 4);
printf("%s%s%s (void)",
C_COLOR_BLACK_ON_GREEN,
infosStatusText,
C_COLOR_NORMAL
);
setCursorLocation(terminalSize.ws_col / 3 * 2, 5);
printf("%s%s%s (void)",
C_COLOR_BLACK_ON_LIGHTGREY,
infosOwnerText,
C_COLOR_NORMAL
);
setCursorLocation(terminalSize.ws_col / 3 * 2, 6);
printf("%s%s%s (void)",
C_COLOR_BLACK_ON_GREEN,
infosSchedulerText,
C_COLOR_NORMAL
);
setCursorLocation(terminalSize.ws_col / 3 * 2, 7);
printf("%s%s%s (void)",
C_COLOR_BLACK_ON_LIGHTGREY,
infosScThreadsText,
C_COLOR_NORMAL
);
setCursorLocation(1, terminalSize.ws_row - 1);
printf(C_COLOR_NORMAL C_COLOR_WHITE_ON_BLUE);
for (int i = 0; i < terminalSize.ws_col; i++) {
printf(" ");
}
@ -177,11 +133,10 @@ void decorateMonitor(int signum)
C_COLOR_REVERSE,
C_COLOR_NORMAL);
setCursorLocation(1, 2);
setCursorLocation(1, 3);
fflush(stdout);
}
//
// Monitor mode main loop
//
@ -231,7 +186,7 @@ int connectedMonitor(int sockfd)
/* // Invalid command */
/* } */
}
printf(C_CLEARSCREEN);
clearScreen();
printf("%sEnd of monitoring session\n\e[0m", C_COLOR_YELLOW);
return 0;
@ -287,21 +242,19 @@ void connectedCommandLine(int sockfd)
if (curLineLength >= 0) {
printf("%s", C_SAVE_CURSORPOS);
saveCursorLocation();
memmove(sendBuff + curPosition, sendBuff + curPosition + 1,
SEND_BUFFER_SIZE);
//sendBuff[curLineLength--] = 0;
printf("%s\r%s%s",
C_CLEARLINE,
promptString,
sendBuff
);
printf("%s", C_RESTORE_CURSORPOS);
printf("%s", C_CURSORLEFT);
restoreCursorLocation();
moveCursorBackward();
}
break;
@ -354,14 +307,14 @@ void connectedCommandLine(int sockfd)
case KEY_ARROW_LEFT:
if (curPosition > 0) {
printf("%s", C_CURSORLEFT);
moveCursorBackward();
curPosition--;
}
break;
case KEY_ARROW_RIGHT:
if (curPosition < curLineLength) {
printf("%s", C_CURSORRIGHT);
moveCursorForward();
curPosition++;
}
break;
@ -377,24 +330,21 @@ void connectedCommandLine(int sockfd)
if (curLineLength >= 0) {
printf("%s", C_SAVE_CURSORPOS);
saveCursorLocation();
memmove(sendBuff + curPosition, sendBuff + curPosition + 1,
SEND_BUFFER_SIZE);
//sendBuff[curLineLength--] = 0;
printf("%s\r%s%s",
C_CLEARLINE,
promptString,
sendBuff
);
printf("%s", C_RESTORE_CURSORPOS);
// printf("%s", C_CURSORRIGHT);
restoreCursorLocation();
}
else {
printf("%s", C_CURSORRIGHT); // pas sûr que ce soit utile...
moveCursorForward(); // XXX
}
}
break;
@ -407,7 +357,7 @@ void connectedCommandLine(int sockfd)
if (curLineLength < MIN(SEND_BUFFER_SIZE,
terminalSize.ws_col - promptLength - 3)
&& curChar > 0) {
printf("%s", C_SAVE_CURSORPOS);
saveCursorLocation();
memmove(sendBuff + curPosition + 1, sendBuff + curPosition,
SEND_BUFFER_SIZE);
@ -421,8 +371,8 @@ void connectedCommandLine(int sockfd)
sendBuff
);
printf("%s", C_RESTORE_CURSORPOS);
printf("%s", C_CURSORRIGHT);
restoreCursorLocation();
moveCursorForward();
}
break;
}