From 0907c5071dafe5cf8da5851744b29be96ae42d80 Mon Sep 17 00:00:00 2001 From: Mike Griese Date: Fri, 10 May 2024 16:11:22 -0500 Subject: [PATCH] ... this takes care of the current command also appearing in the history --- src/cascadia/TerminalControl/ControlCore.cpp | 19 ++++++++++++++----- .../UnitTests_Control/ControlCoreTests.cpp | 3 ++- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/cascadia/TerminalControl/ControlCore.cpp b/src/cascadia/TerminalControl/ControlCore.cpp index 073be049190..2e1c5860af1 100644 --- a/src/cascadia/TerminalControl/ControlCore.cpp +++ b/src/cascadia/TerminalControl/ControlCore.cpp @@ -2246,19 +2246,28 @@ namespace winrt::Microsoft::Terminal::Control::implementation return winrt::hstring{ L"" }; }; + const auto currentCommand = _terminal->CurrentCommand(); + const auto trimmedCurrentCommand = trimToHstring(currentCommand); + for (const auto& commandInBuffer : bufferCommands) { - if (const auto hstr {trimToHstring(commandInBuffer)} ; !hstr.empty() ) + if (const auto hstr{ trimToHstring(commandInBuffer) }; + (!hstr.empty() && hstr != trimmedCurrentCommand)) { commands.push_back(hstr); } } - auto context = winrt::make_self(std::move(commands)); - - const auto currentCommand = _terminal->CurrentCommand(); - context->CurrentCommandline(trimToHstring(currentCommand)); + // If the very last thing in the list of recent commands, is exacly the + // same as the current command, then let's not include it in the + // history. It's literally the thing the user has typed, RIGHT now. + if (commands.back() == trimmedCurrentCommand) + { + commands.pop_back(); + } + auto context = winrt::make_self(std::move(commands)); + context->CurrentCommandline(trimmedCurrentCommand); return *context; } diff --git a/src/cascadia/UnitTests_Control/ControlCoreTests.cpp b/src/cascadia/UnitTests_Control/ControlCoreTests.cpp index 42b76cf4f67..290b796177c 100644 --- a/src/cascadia/UnitTests_Control/ControlCoreTests.cpp +++ b/src/cascadia/UnitTests_Control/ControlCoreTests.cpp @@ -543,8 +543,8 @@ namespace ControlUnitTests Log::Comment(L"Write 'Bar' to the command..."); conn->WriteInput(L"Bar"); { - DebugBreak(); auto historyContext{ core->CommandHistory() }; + // Bar shouldn't be in the history, it should be the current command VERIFY_ARE_EQUAL(1u, historyContext.History().Size()); VERIFY_ARE_EQUAL(L"Bar", historyContext.CurrentCommandline()); } @@ -556,6 +556,7 @@ namespace ControlUnitTests { auto historyContext{ core->CommandHistory() }; VERIFY_ARE_EQUAL(1u, historyContext.History().Size()); + // The current commandline is now empty VERIFY_ARE_EQUAL(L"", historyContext.CurrentCommandline()); } }