diff --git a/src/shellmatta_history.c b/src/shellmatta_history.c index a97d17e..e824e83 100644 --- a/src/shellmatta_history.c +++ b/src/shellmatta_history.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019 Stefan Strobel + * Copyright (c) 2019 - 2021 Stefan Strobel * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this @@ -38,7 +38,7 @@ static void appendHistoryByte(shellmatta_instance_t *inst, char byte) /** -# append the byte */ inst->historyBuffer[inst->historyEnd] = byte; - /** -# check if the we overwrite an existing stored command */ + /** -# check if we overwrite an existing stored command */ if(inst->historyEnd == inst->historyStart) { /** -# move the start pointer to the next termination (0) */ @@ -84,6 +84,56 @@ static bool getHistoryByte(shellmatta_instance_t *inst, char *byte) return ret; } +/** + * @brief compares the current buffer to the last command in the history buffer + * @param[in] inst pointer to a shellmatta instance + * @return true: current command is identical to the last one in the history buffer + */ +static bool compareLastCommand(shellmatta_instance_t *inst) +{ + bool ret = false; + uint32_t i; + uint32_t cnt; + + /** -# check if there is anything in the buffer */ + if(inst->historyStart != inst->historyEnd) + { + i = inst->historyEnd; + cnt = 0u; + + ret = true; + + while((true == ret) && (cnt < inst->inputCount)) + { + /** -# terminate compare on first mismatch */ + if((inst->historyBuffer[i] != inst->buffer[cnt]) || (0u == inst->historyBuffer[i])) + { + ret = false; + } + + if(0u == i) + { + i = inst->historyBufferSize; + } + else + { + i --; + } + + if(cnt < inst->inputCount) + { + cnt ++; + } + else + { + ret = false; + } + } + } + + return ret; +} + /** * @brief navigates in the history buffer by the given number of commands * @param[in, out] inst pointer to a shellmatta instance @@ -165,7 +215,7 @@ bool history_navigate(shellmatta_instance_t *inst, int32_t cnt) /** * @brief stores the current command from the instances buffer into the * history buffer - * @param[in] inst pointer to a shellmatta instance + * @param[in] inst pointer to a shellmatta instance */ void history_storeCmd(shellmatta_instance_t *inst) { @@ -174,21 +224,19 @@ void history_storeCmd(shellmatta_instance_t *inst) /** -# check if we have enough room for the command in the history buffer * and there is a new command to be stored */ if( (inst->historyBufferSize > inst->inputCount) - && (0u != inst->inputCount) - && (true == inst->dirty)) + && (0u != inst->inputCount) + && (true == inst->dirty) + && (true != compareLastCommand(inst))) { /** -# append the command termination */ appendHistoryByte(inst, 0u); /** -# append the command byte wise in reverse direction */ for(i = inst->inputCount; i > 0u; i --) - { + { appendHistoryByte(inst, inst->buffer[i - 1u]); } } - - /** -# remove the dirty flag - everything is nice and saved */ - inst->dirty = false; } /** @@ -221,7 +269,6 @@ void history_restoreCmd(shellmatta_instance_t *inst) if(true == anythingToRestore) { utils_writeEcho(inst, inst->buffer, inst->inputCount); - inst->dirty = false; } (void)history_navigate(inst, 1); }