From 8500cddbf3a99778be7d73be187fbb83dc31bd38 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Thu, 5 Aug 2021 15:14:59 +0200 Subject: [PATCH] New monitor layout --- include/cmds.h | 3 ++- src/cli.c | 64 +++++++++++++++++++++++++++++++++----------------- src/cmds.c | 7 ++++++ 3 files changed, 51 insertions(+), 23 deletions(-) diff --git a/include/cmds.h b/include/cmds.h index 57d8db5..910254a 100644 --- a/include/cmds.h +++ b/include/cmds.h @@ -26,6 +26,7 @@ //char *CmdHelp(char **, Server_t *); char *CmdModel(char*, char**, Server_t*); char *CmdShutdown(char*, char**, Server_t*); +char *CmdHelp(char*, char**, Server_t*); struct { const char *name; @@ -35,7 +36,7 @@ struct { Command_t cmdList[] = { - //{"help", CmdHelp, "Help command"}, + {"help", CmdHelp, "Help command"}, {"model", CmdModel, "Model command"}, {"shutdown", CmdShutdown, "Shutdown command"}, }; diff --git a/src/cli.c b/src/cli.c index bb60885..860fc42 100644 --- a/src/cli.c +++ b/src/cli.c @@ -126,35 +126,58 @@ static inline int getch(bool nonBlocking) return buf; } - // - // Get the screen size - // - void getScreenSize(int signum) +// +// Get the screen size +// +void getScreenSize(int signum) { // Get current terminal size ioctl(STDOUT_FILENO, TIOCGWINSZ, &terminalSize); } - // - // Print monitor screen - // - void decorateMonitor(int signum) +// +// Set cursor location +// +void setCursorLocation(char x, char y) +{ + printf("\x1b[%d;%dH", y, x); +} + +// +// Print monitor screen +// +void decorateMonitor(int signum) { getScreenSize(signum); - printf(C_CLEARSCREEN "\n"); - fflush(stdout); + printf(C_CLEARSCREEN); - for (int i = 0; - i < (terminalSize.ws_row * terminalSize.ws_col); - i++) { - 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); + setCursorLocation(1,1); + + printf("%sMONITOR", + C_COLOR_GREEN C_COLOR_REVERSE + ); + + for (int i = 0; i < terminalSize.ws_col - sizeof("MONITOR") + 1; i++) { + printf(" "); + } + printf(C_COLOR_NORMAL); + + setCursorLocation(1,terminalSize.ws_row - 1); + printf("%s", + C_COLOR_GREEN C_COLOR_REVERSE + ); + for (int i = 0; i < terminalSize.ws_col; i++) { + printf(" "); } + printf("%sQ%s Quit\t%sS%s Start/Stop", + C_COLOR_NORMAL C_COLOR_REVERSE, + C_COLOR_NORMAL, + C_COLOR_REVERSE, + C_COLOR_NORMAL); + + setCursorLocation(1, 2); fflush(stdout); } @@ -182,10 +205,6 @@ int connectedMonitor(int sockfd) // Read command from terminal curChar = getch(NON_BLOCKING); - if (curChar > 0) { - printLog("Char detected : %d\n", curChar); - } - switch (curChar) { case 'q': pleaseStop = true; @@ -212,6 +231,7 @@ int connectedMonitor(int sockfd) /* // Invalid command */ /* } */ } + printf(C_CLEARSCREEN); printf("%sEnd of monitoring session\n\e[0m", C_COLOR_YELLOW); return 0; diff --git a/src/cmds.c b/src/cmds.c index 22e2bd3..b2d82cd 100644 --- a/src/cmds.c +++ b/src/cmds.c @@ -187,3 +187,10 @@ char *CmdShutdown(char *buf, char **argv, Server_t *args) return buf; } + +char *CmdHelp(char *buf, char **argv, Server_t *args) +{ + strcat(buf, "List of known commands:\n"); + + return buf; +}