Skip to content

Commit

Permalink
string of numbers is unsightly but it works
Browse files Browse the repository at this point in the history
  • Loading branch information
PankajBhojwani committed Mar 27, 2024
1 parent 2093660 commit 6c32539
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 37 deletions.
23 changes: 23 additions & 0 deletions src/cascadia/TerminalSettingsModel/ActionAndArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,29 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
return found != GeneratedActionNames.end() ? found->second : L"";
}

// Function Description:
// - This will generate an ID for this ActionAndArgs, based on the ShortcutAction and the Args
// - It will always create the same ID if the ShortcutAction and the Args are the same
// - Note: this should only be called for User-created actions
// - Example: The "SendInput 'abc'" action will have the generated ID "User.sendInput.<hash of 'abc'>"
// Return Value:
// - The ID, based on the ShortcutAction and the Args
winrt::hstring ActionAndArgs::GenerateID() const
{
if (_Action != ShortcutAction::Invalid)
{
auto actionKeyString = ActionToStringMap.find(_Action)->second;
auto result = RS_(L"OriginTagUser") + L"." + std::wstring{ actionKeyString.begin(), actionKeyString.end() };
if (_Args)
{
// If there are args, append the hash of the args
result = result + L"." + std::to_wstring(_Args.Hash());
}
return result;
}
return L"";
}

winrt::hstring ActionAndArgs::Serialize(const winrt::Windows::Foundation::Collections::IVector<Model::ActionAndArgs>& args)
{
Json::Value json{ Json::objectValue };
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalSettingsModel/ActionAndArgs.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
com_ptr<ActionAndArgs> Copy() const;

hstring GenerateName() const;
hstring GenerateID() const;

WINRT_PROPERTY(ShortcutAction, Action, ShortcutAction::Invalid);
WINRT_PROPERTY(IActionArgs, Args, nullptr);
Expand Down
36 changes: 1 addition & 35 deletions src/cascadia/TerminalSettingsModel/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,40 +121,6 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
return hstring{ _ID };
}

// Function Description:
// - generate an ID for this command and populate the _ID field
// - this function _will_ overwrite an existing ID if there is one, it is
// on the caller to make sure that either there was no ID or the overwrite is okay
// - this function should only be called to generate IDs for user-created commands
void Command::_generateID()
{
if (_ActionAndArgs)
{
// lambda function to remove whitespace and capitalize each letter after a removed space
auto removeWhitespaceAndCapitalize = [](wchar_t& x, bool& capitalizeNext) {
if (std::iswspace(x))
{
capitalizeNext = true; // Capitalize the next character
return true; // Remove the whitespace
}
else if (capitalizeNext)
{
x = std::towupper(x); // Capitalize the letter
capitalizeNext = false; // Reset flag
}
return false; // Keep the character
};

std::wstring noWhitespaceName{ get_self<implementation::ActionAndArgs>(_ActionAndArgs)->GenerateName() };
bool capitalizeNext;
noWhitespaceName.erase(std::remove_if(noWhitespaceName.begin(), noWhitespaceName.end(), [&capitalizeNext, removeWhitespaceAndCapitalize](wchar_t& x) {
return removeWhitespaceAndCapitalize(x, capitalizeNext);
}),
noWhitespaceName.end());
_ID = RS_(L"OriginTagUser") + L"." + noWhitespaceName;
}
}

void Command::Name(const hstring& value)
{
if (!_name.has_value() || _name.value() != value)
Expand Down Expand Up @@ -360,7 +326,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
}
else if (origin == OriginTag::User)
{
result->_generateID();
result->_ID = get_self<implementation::ActionAndArgs>(result->_ActionAndArgs)->GenerateID();
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/cascadia/TerminalSettingsModel/Command.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,6 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
std::optional<std::wstring> _iconPath;
bool _nestedCommand{ false };

void _generateID();

static std::vector<Model::Command> _expandCommand(Command* const expandable,
Windows::Foundation::Collections::IVectorView<Model::Profile> profiles,
Windows::Foundation::Collections::IVectorView<Model::ColorScheme> schemes);
Expand Down

0 comments on commit 6c32539

Please sign in to comment.