Better cli : arrows and edition
This commit is contained in:
parent
2774726c65
commit
c6d99559b5
79
src/cli.c
79
src/cli.c
|
@ -39,8 +39,16 @@
|
||||||
#define KEY_DIRECTIONS 91
|
#define KEY_DIRECTIONS 91
|
||||||
#define KEY_ARROW_UP 65
|
#define KEY_ARROW_UP 65
|
||||||
#define KEY_ARROW_DOWN 66
|
#define KEY_ARROW_DOWN 66
|
||||||
|
#define KEY_ARROW_RIGHT 67
|
||||||
|
#define KEY_ARROW_LEFT 68
|
||||||
#define KEY_DELETE 127
|
#define KEY_DELETE 127
|
||||||
|
|
||||||
|
#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"
|
||||||
|
|
||||||
static char getch(void)
|
static char getch(void)
|
||||||
{
|
{
|
||||||
char buf = 0;
|
char buf = 0;
|
||||||
|
@ -90,7 +98,7 @@ void connectedCommunication(int sockfd)
|
||||||
int historyIndex = 0, historyModifier;
|
int historyIndex = 0, historyModifier;
|
||||||
|
|
||||||
char curChar;
|
char curChar;
|
||||||
int curLineLength, continueReadLine, answerLength;
|
int curLineLength, curPosition, continueReadLine, answerLength;
|
||||||
|
|
||||||
char *exitCommand = "exit";
|
char *exitCommand = "exit";
|
||||||
char *promptString = "gem-graph console> ";
|
char *promptString = "gem-graph console> ";
|
||||||
|
@ -102,9 +110,10 @@ void connectedCommunication(int sockfd)
|
||||||
|
|
||||||
// Read command from terminal
|
// Read command from terminal
|
||||||
curLineLength = 0;
|
curLineLength = 0;
|
||||||
|
curPosition = 0;
|
||||||
continueReadLine = 1;
|
continueReadLine = 1;
|
||||||
historyModifier = -1;
|
historyModifier = -1;
|
||||||
while (continueReadLine && (curChar = getch())) { //TODO empty line in history
|
while (continueReadLine && (curChar = getch())) {
|
||||||
|
|
||||||
switch (curChar) {
|
switch (curChar) {
|
||||||
|
|
||||||
|
@ -114,17 +123,31 @@ void connectedCommunication(int sockfd)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case KEY_DELETE:
|
case KEY_DELETE:
|
||||||
|
if (strlen(sendBuff) == 0)
|
||||||
if (!strlen(sendBuff))
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// Delete last char
|
// Delete last char
|
||||||
sendBuff[--curLineLength] = 0;
|
sendBuff[--curPosition] = 0;
|
||||||
|
curLineLength--;
|
||||||
|
|
||||||
printf("\33[2K\r%s%s",
|
if (curLineLength >= 0) {
|
||||||
|
|
||||||
|
printf("%s", C_SAVE_CURSORPOS);
|
||||||
|
|
||||||
|
memmove(sendBuff + curPosition, sendBuff + curPosition + 1,
|
||||||
|
SEND_BUFFER_SIZE);
|
||||||
|
|
||||||
|
//sendBuff[curLineLength--] = 0;
|
||||||
|
|
||||||
|
printf("%s\r%s%s",
|
||||||
|
C_CLEARLINE,
|
||||||
promptString,
|
promptString,
|
||||||
sendBuff
|
sendBuff
|
||||||
);
|
);
|
||||||
|
|
||||||
|
printf("%s", C_RESTORE_CURSORPOS);
|
||||||
|
printf("%s", C_CURSORLEFT);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case KEY_ESCAPE:
|
case KEY_ESCAPE:
|
||||||
|
@ -137,7 +160,8 @@ void connectedCommunication(int sockfd)
|
||||||
break;
|
break;
|
||||||
historyModifier = (historyModifier + 1)
|
historyModifier = (historyModifier + 1)
|
||||||
% (historyIndex + 1);
|
% (historyIndex + 1);
|
||||||
printf("\33[2K\r%s%s",
|
printf("%s\r%s%s",
|
||||||
|
C_CLEARLINE,
|
||||||
promptString,
|
promptString,
|
||||||
&historyBuff[
|
&historyBuff[
|
||||||
(historyIndex - historyModifier - 1)
|
(historyIndex - historyModifier - 1)
|
||||||
|
@ -149,15 +173,16 @@ void connectedCommunication(int sockfd)
|
||||||
* SEND_BUFFER_SIZE
|
* SEND_BUFFER_SIZE
|
||||||
], SEND_BUFFER_SIZE);
|
], SEND_BUFFER_SIZE);
|
||||||
curLineLength = strlen(sendBuff);
|
curLineLength = strlen(sendBuff);
|
||||||
|
curPosition = curLineLength;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case KEY_ARROW_DOWN:
|
case KEY_ARROW_DOWN:
|
||||||
|
|
||||||
if (historyIndex <= 0)
|
if (historyIndex <= 0)
|
||||||
break;
|
break;
|
||||||
historyModifier = (historyModifier + historyIndex)
|
historyModifier = (historyModifier + historyIndex)
|
||||||
% (historyIndex + 1);
|
% (historyIndex + 1);
|
||||||
printf("\33[2K\r%s%s",
|
printf("%s\r%s%s",
|
||||||
|
C_CLEARLINE,
|
||||||
promptString,
|
promptString,
|
||||||
&historyBuff[
|
&historyBuff[
|
||||||
(historyIndex - historyModifier - 1)
|
(historyIndex - historyModifier - 1)
|
||||||
|
@ -169,16 +194,46 @@ void connectedCommunication(int sockfd)
|
||||||
* SEND_BUFFER_SIZE
|
* SEND_BUFFER_SIZE
|
||||||
], SEND_BUFFER_SIZE);
|
], SEND_BUFFER_SIZE);
|
||||||
curLineLength = strlen(sendBuff);
|
curLineLength = strlen(sendBuff);
|
||||||
|
curPosition = curLineLength;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case KEY_ARROW_LEFT:
|
||||||
|
if (curPosition > 0) {
|
||||||
|
printf("%s", C_CURSORLEFT);
|
||||||
|
curPosition--;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case KEY_ARROW_RIGHT:
|
||||||
|
if (curPosition < curLineLength) {
|
||||||
|
printf("%s", C_CURSORRIGHT);
|
||||||
|
curPosition++;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
sendBuff[curLineLength++] = curChar;
|
if (curLineLength < SEND_BUFFER_SIZE) {
|
||||||
printf("%c", curChar);
|
printf("%s", C_SAVE_CURSORPOS);
|
||||||
break;
|
|
||||||
|
|
||||||
|
memmove(sendBuff + curPosition + 1, sendBuff + curPosition,
|
||||||
|
SEND_BUFFER_SIZE);
|
||||||
|
|
||||||
|
sendBuff[curPosition++] = curChar;
|
||||||
|
curLineLength++;
|
||||||
|
|
||||||
|
printf("%s\r%s%s",
|
||||||
|
C_CLEARLINE,
|
||||||
|
promptString,
|
||||||
|
sendBuff
|
||||||
|
);
|
||||||
|
|
||||||
|
printf("%s", C_RESTORE_CURSORPOS);
|
||||||
|
printf("%s", C_CURSORRIGHT);
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
|
|
|
@ -87,6 +87,9 @@ void *serverCommunicationInstance(void *server)
|
||||||
clientPort,
|
clientPort,
|
||||||
receiveBuff);
|
receiveBuff);
|
||||||
|
|
||||||
|
if (receiveBuff[0] == '\0')
|
||||||
|
break;
|
||||||
|
|
||||||
// get args in an array
|
// get args in an array
|
||||||
tokenIndex = 0;
|
tokenIndex = 0;
|
||||||
argv = (char**) realloc(argv, 1 * sizeof(char*));
|
argv = (char**) realloc(argv, 1 * sizeof(char*));
|
||||||
|
|
Loading…
Reference in New Issue