D.R.Y
This commit is contained in:
parent
3a98096bfd
commit
19fb0a2638
33
main.c
33
main.c
|
@ -47,19 +47,19 @@ void insertInStartString(commandLine *cmdLine, int* toInsert ) {
|
|||
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) {
|
||||
for (int i = arrayLength - 1; i > 0 ; i --) {
|
||||
for (int j = 0; j < cmdLine[i - 1].length; j ++)
|
||||
cmdLine[i].string[j] = cmdLine[i - 1].string[j];
|
||||
|
||||
cmdLine[i].length = cmdLine[i - 1].length;
|
||||
cmdLine[i].position = cmdLine[i - 1].position;
|
||||
copyCommandLine(&(cmdLine[i]), cmdLine[i - 1]);
|
||||
}
|
||||
for (int i = 0; i < toInsert.length; i ++)
|
||||
cmdLine[0].string[i] = toInsert.string[i];
|
||||
|
||||
cmdLine[0].length = toInsert.length;
|
||||
cmdLine[0].position = toInsert.position;
|
||||
copyCommandLine(&(cmdLine[0]), toInsert);
|
||||
}
|
||||
|
||||
void insert(int *cmd_string, int toInsert , int position, int len) {
|
||||
|
@ -88,14 +88,7 @@ void resetBuffer (commandLine *buffer) {
|
|||
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[]) {
|
||||
initscr();
|
||||
|
@ -161,12 +154,14 @@ int main (int argc, char* argv[]) {
|
|||
case KEY_UP:
|
||||
historyPosition += historyPosition < historySize? 1 : 0;
|
||||
resetBuffer(&buffer);
|
||||
if (historyPosition > 0) copyCommandLine(&buffer, history[historyPosition - 1]);
|
||||
if (historyPosition > 0)
|
||||
copyCommandLine(&buffer, history[historyPosition - 1]);
|
||||
break;
|
||||
case KEY_DOWN:
|
||||
historyPosition -= historyPosition > 0? 1 : 0;
|
||||
resetBuffer(&buffer);
|
||||
if (historyPosition > 0) copyCommandLine(&buffer, history[historyPosition - 1]);
|
||||
if (historyPosition > 0)
|
||||
copyCommandLine(&buffer, history[historyPosition - 1]);
|
||||
break;
|
||||
case KEY_BACKSPACE:
|
||||
pop(&buffer);
|
||||
|
|
Loading…
Reference in New Issue