This commit is contained in:
leca 2023-02-25 17:48:55 +03:00
parent 3a98096bfd
commit 19fb0a2638
1 changed files with 14 additions and 19 deletions

33
main.c
View File

@ -47,19 +47,19 @@ void insertInStartString(commandLine *cmdLine, int* toInsert ) {
cmdLine[0].string[i] = toInsert[i]; cmdLine[0].string[i] = toInsert[i];
} }
void copyCommandLine(commandLine* destination, commandLine source) {
for (int i = 0; i < source.length; i ++)
destination->string[i] = source.string[i];
destination->position = source.position;
destination->length = source.length;
}
void insertInStartCommandLine(commandLine *cmdLine, commandLine toInsert, int arrayLength) { void insertInStartCommandLine(commandLine *cmdLine, commandLine toInsert, int arrayLength) {
for (int i = arrayLength - 1; i > 0 ; i --) { for (int i = arrayLength - 1; i > 0 ; i --) {
for (int j = 0; j < cmdLine[i - 1].length; j ++) copyCommandLine(&(cmdLine[i]), cmdLine[i - 1]);
cmdLine[i].string[j] = cmdLine[i - 1].string[j];
cmdLine[i].length = cmdLine[i - 1].length;
cmdLine[i].position = cmdLine[i - 1].position;
} }
for (int i = 0; i < toInsert.length; i ++) copyCommandLine(&(cmdLine[0]), toInsert);
cmdLine[0].string[i] = toInsert.string[i];
cmdLine[0].length = toInsert.length;
cmdLine[0].position = toInsert.position;
} }
void insert(int *cmd_string, int toInsert , int position, int len) { void insert(int *cmd_string, int toInsert , int position, int len) {
@ -88,14 +88,7 @@ void resetBuffer (commandLine *buffer) {
buffer->position = 0; buffer->position = 0;
} }
void copyCommandLine(commandLine* destination, commandLine source) {
for (int i = 0; i < source.length; i ++)
destination->string[i] = source.string[i];
destination->position = source.position;
destination->length = source.length;
}
int main (int argc, char* argv[]) { int main (int argc, char* argv[]) {
initscr(); initscr();
@ -161,12 +154,14 @@ int main (int argc, char* argv[]) {
case KEY_UP: case KEY_UP:
historyPosition += historyPosition < historySize? 1 : 0; historyPosition += historyPosition < historySize? 1 : 0;
resetBuffer(&buffer); resetBuffer(&buffer);
if (historyPosition > 0) copyCommandLine(&buffer, history[historyPosition - 1]); if (historyPosition > 0)
copyCommandLine(&buffer, history[historyPosition - 1]);
break; break;
case KEY_DOWN: case KEY_DOWN:
historyPosition -= historyPosition > 0? 1 : 0; historyPosition -= historyPosition > 0? 1 : 0;
resetBuffer(&buffer); resetBuffer(&buffer);
if (historyPosition > 0) copyCommandLine(&buffer, history[historyPosition - 1]); if (historyPosition > 0)
copyCommandLine(&buffer, history[historyPosition - 1]);
break; break;
case KEY_BACKSPACE: case KEY_BACKSPACE:
pop(&buffer); pop(&buffer);