From 3b26597bd6acd16776e8543104cd97799f35171d Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Fri, 9 Jul 2021 00:37:03 +0200 Subject: [PATCH] History and correction --- src/cli.c | 93 ++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 75 insertions(+), 18 deletions(-) diff --git a/src/cli.c b/src/cli.c index 957ff51..83f37c2 100644 --- a/src/cli.c +++ b/src/cli.c @@ -35,6 +35,12 @@ #define SERVER_IP_ADDR "127.0.0.1" #define SERVER_PORT 9000 +#define KEY_ESCAPE 27 +#define KEY_DIRECTIONS 91 +#define KEY_ARROW_UP 65 +#define KEY_ARROW_DOWN 66 +#define KEY_DELETE 127 + static char getch(void) { char buf = 0; @@ -75,55 +81,106 @@ static char getch(void) } -char sendBuff[SEND_BUFFER_SIZE] = {0}; -char receiveBuff[RECEIVE_BUFFER_SIZE] = {0}; -char historyBuff[SEND_BUFFER_SIZE * NHISTORY] = {0}; -char curChar; -char *exitCommand = "exit"; -volatile int curLineLength, readLine, historyIndex, historyModifier = 0; void connectedCommunication(int sockfd) { + char sendBuff[SEND_BUFFER_SIZE] = {0}; + char receiveBuff[RECEIVE_BUFFER_SIZE] = {0}; + + char historyBuff[SEND_BUFFER_SIZE * NHISTORY] = {0}; + int historyIndex = 0, historyModifier; + + char curChar; + int curLineLength, readLine; + + char *exitCommand = "exit"; + char *promptString = "gem-graph console> "; for (;;) { // Zeroing buffer bzero(sendBuff, sizeof(sendBuff)); - printf("gem-graph console> "); + printf("%s", promptString); // Read command from terminal curLineLength = 0; readLine = 1; - historyModifier = 1; + historyModifier = -1; while (readLine && (curChar = getch())) { switch (curChar) { + case '\n': printf("%c", curChar); readLine = 0; break; - case 27: - if (getch() == 91) - if (getch() == 65) { - historyModifier--; - printf("%s", &historyBuff[ - (historyIndex - historyModifier - 1) - * SEND_BUFFER_SIZE - ]); + case KEY_DELETE: + if (!strlen(sendBuff)) + break; + + // Delete last char + sendBuff[--curLineLength] = 0; + + printf("\33[2K\r%s%s", + promptString, + sendBuff + ); + break; + + case KEY_ESCAPE: + if (getch() == KEY_DIRECTIONS) { + + switch(getch()) { + + case KEY_ARROW_UP: + if (historyIndex <= 0) + break; + historyModifier = (historyModifier + 1) + % (historyIndex); + printf("\33[2K\r%s%s", + promptString, + &historyBuff[ + (historyIndex - historyModifier - 1) + * SEND_BUFFER_SIZE + ] + ); memcpy(sendBuff, &historyBuff[ (historyIndex - historyModifier - 1) * SEND_BUFFER_SIZE ], SEND_BUFFER_SIZE); + curLineLength = strlen(sendBuff); + break; - curLineLength = curLineLength + strlen(sendBuff); + case KEY_ARROW_DOWN: + + if (historyIndex <= 0) + break; + historyModifier = (historyModifier + historyIndex - 1) + % (historyIndex); + printf("\33[2K\r%s%s", + promptString, + &historyBuff[ + (historyIndex - historyModifier - 1) + * SEND_BUFFER_SIZE + ] + ); + memcpy(sendBuff, &historyBuff[ + (historyIndex - historyModifier - 1) + * SEND_BUFFER_SIZE + ], SEND_BUFFER_SIZE); + curLineLength = strlen(sendBuff); + break; } - + } break; + default: sendBuff[curLineLength++] = curChar; printf("%c", curChar); break; + } + fflush(stdout); }