Skip to content

Commit 0907c50

Browse files
committed
... this takes care of the current command also appearing in the history
1 parent b85eb30 commit 0907c50

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

src/cascadia/TerminalControl/ControlCore.cpp

+14-5
Original file line numberDiff line numberDiff line change
@@ -2246,19 +2246,28 @@ namespace winrt::Microsoft::Terminal::Control::implementation
22462246
return winrt::hstring{ L"" };
22472247
};
22482248

2249+
const auto currentCommand = _terminal->CurrentCommand();
2250+
const auto trimmedCurrentCommand = trimToHstring(currentCommand);
2251+
22492252
for (const auto& commandInBuffer : bufferCommands)
22502253
{
2251-
if (const auto hstr {trimToHstring(commandInBuffer)} ; !hstr.empty() )
2254+
if (const auto hstr{ trimToHstring(commandInBuffer) };
2255+
(!hstr.empty() && hstr != trimmedCurrentCommand))
22522256
{
22532257
commands.push_back(hstr);
22542258
}
22552259
}
22562260

2257-
auto context = winrt::make_self<CommandHistoryContext>(std::move(commands));
2258-
2259-
const auto currentCommand = _terminal->CurrentCommand();
2260-
context->CurrentCommandline(trimToHstring(currentCommand));
2261+
// If the very last thing in the list of recent commands, is exacly the
2262+
// same as the current command, then let's not include it in the
2263+
// history. It's literally the thing the user has typed, RIGHT now.
2264+
if (commands.back() == trimmedCurrentCommand)
2265+
{
2266+
commands.pop_back();
2267+
}
22612268

2269+
auto context = winrt::make_self<CommandHistoryContext>(std::move(commands));
2270+
context->CurrentCommandline(trimmedCurrentCommand);
22622271
return *context;
22632272
}
22642273

src/cascadia/UnitTests_Control/ControlCoreTests.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -543,8 +543,8 @@ namespace ControlUnitTests
543543
Log::Comment(L"Write 'Bar' to the command...");
544544
conn->WriteInput(L"Bar");
545545
{
546-
DebugBreak();
547546
auto historyContext{ core->CommandHistory() };
547+
// Bar shouldn't be in the history, it should be the current command
548548
VERIFY_ARE_EQUAL(1u, historyContext.History().Size());
549549
VERIFY_ARE_EQUAL(L"Bar", historyContext.CurrentCommandline());
550550
}
@@ -556,6 +556,7 @@ namespace ControlUnitTests
556556
{
557557
auto historyContext{ core->CommandHistory() };
558558
VERIFY_ARE_EQUAL(1u, historyContext.History().Size());
559+
// The current commandline is now empty
559560
VERIFY_ARE_EQUAL(L"", historyContext.CurrentCommandline());
560561
}
561562
}

0 commit comments

Comments
 (0)