Skip to content

Commit

Permalink
truncate and hex, debug assert
Browse files Browse the repository at this point in the history
  • Loading branch information
PankajBhojwani committed Apr 12, 2024
1 parent bdf42c2 commit 12f3aa9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
13 changes: 11 additions & 2 deletions src/cascadia/TerminalSettingsModel/ActionAndArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -465,8 +465,17 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
auto result = fmt::format(L"User.{}", std::wstring{ actionKeyString.begin(), actionKeyString.end() });
if (_Args)
{
// If there are args, append the hash of the args
fmt::format_to(std::back_inserter(result), L".{}", _Args.Hash());
// If there are args, we need to append the hash of the args
// However, to make it a little more presentable we
// 1. truncate the hash to 32 bits
// 2. convert it to a hex string
// there is a _tiny_ chance of collision because of the truncate but unlikely for
// the number of commands a user is expected to have
const auto argsHash32 = static_cast<uint32_t>(_Args.Hash() & 0xFFFFFFFF);
std::wstringstream stream;
stream << std::hex << std::uppercase << argsHash32;
const auto argsHash32InHex = stream.str();
fmt::format_to(std::back_inserter(result), L".{}", argsHash32InHex);
}
return winrt::hstring{ result };
}
Expand Down
6 changes: 5 additions & 1 deletion src/cascadia/TerminalSettingsModel/ActionMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -797,11 +797,15 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation

bool ActionMap::GenerateIDsForActions()
{
// Note: this should ONLY be called for the action map in the user's settings file
bool fixedUp{ false };
for (auto actionPair : _ActionMap)
{
auto cmdImpl{ winrt::get_self<Command>(actionPair.second) };

// Note: this function should ONLY be called for the action map in the user's settings file
// this debug assert should verify that for debug builds
assert(cmdImpl->Origin() == OriginTag::User);

if (cmdImpl->ID().empty())
{
fixedUp = cmdImpl->GenerateID() || fixedUp;
Expand Down
4 changes: 2 additions & 2 deletions src/cascadia/UnitTests_SettingsModel/SerializationTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ namespace SettingsModelUnitTests
}
],
"actions": [
{ "command": { "action": "sendInput", "input": "VT Griese Mode" }, "id" : "User.sendInput.15093368865568800249", "keys": "ctrl+k" }
{ "command": { "action": "sendInput", "input": "VT Griese Mode" }, "id": "User.sendInput.E02B3DF9", "keys": "ctrl+k" }
],
"theme": "system",
"themes": []
Expand Down Expand Up @@ -974,7 +974,7 @@ namespace SettingsModelUnitTests
"name": "foo",
"command": { "action": "sendInput", "input": "just some input" },
"keys": "ctrl+shift+w",
"id" : "User.sendInput.3448838294654165202"
"id" : "User.sendInput.A020D2"
}
]
})" };
Expand Down

0 comments on commit 12f3aa9

Please sign in to comment.