Skip to content

Commit

Permalink
Teach Command Palette to filter out duplicate command lines (#11116)
Browse files Browse the repository at this point in the history
Closes #11093
  • Loading branch information
Don-Vito authored Sep 2, 2021
1 parent 8ffea2c commit 7908164
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions src/cascadia/TerminalApp/CommandPalette.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1275,15 +1275,39 @@ namespace winrt::TerminalApp::implementation
void CommandPalette::_updateRecentCommands(const hstring& command)
{
const auto recentCommands = ApplicationState::SharedInstance().RecentCommands();
// If there aren't and recent commands already in the state, then we
// don't need to copy any.
const auto countToCopy = std::min(recentCommands ? recentCommands.Size() : 0, CommandLineHistoryLength - 1);
std::vector<hstring> newRecentCommands{ countToCopy + 1 };
til::at(newRecentCommands, 0) = command;
if (countToCopy)
// If this is the first time we've opened the commandline mode and
// there aren't any recent commands, then just store the new command.
if (!recentCommands)
{
ApplicationState::SharedInstance().RecentCommands(single_threaded_vector(std::move(std::vector{ command })));
return;
}

const auto numNewRecentCommands = std::min(recentCommands.Size() + 1, CommandLineHistoryLength);

std::vector<hstring> newRecentCommands;
newRecentCommands.reserve(numNewRecentCommands);

std::unordered_set<hstring> uniqueCommands;
uniqueCommands.reserve(numNewRecentCommands);

newRecentCommands.push_back(command);
uniqueCommands.insert(command);

for (const auto& c : recentCommands)
{
recentCommands.GetMany(0, { newRecentCommands.data() + 1, countToCopy });
if (newRecentCommands.size() >= CommandLineHistoryLength)
{
// Don't store more than CommandLineHistoryLength commands
break;
}

if (uniqueCommands.emplace(c).second)
{
newRecentCommands.push_back(c);
}
}

ApplicationState::SharedInstance().RecentCommands(single_threaded_vector(std::move(newRecentCommands)));
}
}

0 comments on commit 7908164

Please sign in to comment.