diff --git a/CLI/CLI.c b/CLI/CLI.c index a17eac0..4825a58 100644 --- a/CLI/CLI.c +++ b/CLI/CLI.c @@ -107,6 +107,39 @@ void CLI_PrintHistory() } free(historyList); } + +void historyPrevius() +{ + char* line; + if ( + (History_getCurrPos(History) == History_getSize(History)) + && (FIFOBuffChar_getSize(FIFO) > 0) + ) + { + // add current text to end of history + line = fifoToString(FIFO); + History_put(History, line); + } + + // empty current line + while (FIFOBuffChar_getSize(FIFO) > 0) + { + CLI_stringOut((char*)"\x1b[D \x1b[D"); + FIFOBuffChar_pop(FIFO); + } + + // get previus command + int ret = History_getPrev(History, &line); + + // write line + if ((ret >= 0) && (line != NULL)) + { + for (int i=0; *(line + i) != '\0'; i++) + { + CLI_charIn(*(line + i)); + } + } +} #endif int tryExecute(FIFOBuffChar_t* fifo) @@ -234,8 +267,8 @@ bool CLI_charIn(char c) { #ifdef HISTORY case 'A': // arrow up - // history previus - CLI_stringOut((char*)"(key: arrow up)"); + historyPrevius(); + // CLI_stringOut((char*)"(key: arrow up)"); CLI_State = CLI_State_Default; break; case 'B': // arrow down diff --git a/History/History.c b/History/History.c index 4c9bcfa..612c6d2 100644 --- a/History/History.c +++ b/History/History.c @@ -167,6 +167,33 @@ int History_getFullHistory(History_t* history, char*** list) return retCode; } +int History_getCurrPos(History_t* history) +{ + int retCode = 0; + + if (history->CurrEl_p != NULL) + { + History_element_t* el = history->FirstEl_p; + for (int i = 0; (el != history->CurrEl_p) && (retCode >= 0); i++) + { + if (el->nextEl_p == NULL) + { + retCode = 2; + } + else + { + el = (History_element_t*)el->nextEl_p; + } + } + } + else + { + retCode = History_getSize(history); + } + + return retCode; +} + unsigned int History_getSize(History_t *history) { return history->size; diff --git a/History/history.h b/History/history.h index d6c7b60..35bc020 100644 --- a/History/history.h +++ b/History/history.h @@ -31,6 +31,8 @@ extern int History_put(History_t *history, char* line); extern int History_getPrev(History_t* history, char** line); extern int History_getNext(History_t* history, char** line); +extern int History_getCurrPos(History_t* history); + extern int History_getFullHistory(History_t* history, char*** list); extern unsigned int History_getSize(History_t *history);