History and correction

This commit is contained in:
Adrien Bourmault 2021-07-09 00:37:03 +02:00
parent 439a0b0bb2
commit 3b26597bd6
No known key found for this signature in database
GPG Key ID: 6EB408FE0ACEC664
1 changed files with 75 additions and 18 deletions

View File

@ -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[
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 = curLineLength + strlen(sendBuff);
}
curLineLength = strlen(sendBuff);
break;
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);
}