added check if the current entered command matches the last command in the history buffer - if the command is already stored it will not be stored again

This commit is contained in:
prozessorkern 2021-01-20 21:13:01 +01:00
parent ac6ffb9602
commit 88c33895f6

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2019 Stefan Strobel <stefan.strobel@shimatta.net> * Copyright (c) 2019 - 2021 Stefan Strobel <stefan.strobel@shimatta.net>
* *
* This Source Code Form is subject to the terms of the Mozilla Public * 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 * 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 */ /** -# append the byte */
inst->historyBuffer[inst->historyEnd] = 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) if(inst->historyEnd == inst->historyStart)
{ {
/** -# move the start pointer to the next termination (0) */ /** -# move the start pointer to the next termination (0) */
@ -84,6 +84,56 @@ static bool getHistoryByte(shellmatta_instance_t *inst, char *byte)
return ret; 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 * @brief navigates in the history buffer by the given number of commands
* @param[in, out] inst pointer to a shellmatta instance * @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 * @brief stores the current command from the instances buffer into the
* history buffer * 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) void history_storeCmd(shellmatta_instance_t *inst)
{ {
@ -174,8 +224,9 @@ void history_storeCmd(shellmatta_instance_t *inst)
/** -# check if we have enough room for the command in the history buffer /** -# check if we have enough room for the command in the history buffer
* and there is a new command to be stored */ * and there is a new command to be stored */
if( (inst->historyBufferSize > inst->inputCount) if( (inst->historyBufferSize > inst->inputCount)
&& (0u != inst->inputCount) && (0u != inst->inputCount)
&& (true == inst->dirty)) && (true == inst->dirty)
&& (true != compareLastCommand(inst)))
{ {
/** -# append the command termination */ /** -# append the command termination */
appendHistoryByte(inst, 0u); appendHistoryByte(inst, 0u);
@ -186,9 +237,6 @@ void history_storeCmd(shellmatta_instance_t *inst)
appendHistoryByte(inst, inst->buffer[i - 1u]); 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) if(true == anythingToRestore)
{ {
utils_writeEcho(inst, inst->buffer, inst->inputCount); utils_writeEcho(inst, inst->buffer, inst->inputCount);
inst->dirty = false;
} }
(void)history_navigate(inst, 1); (void)history_navigate(inst, 1);
} }