From 54cfb857dba0de343112a8a7e4f0bee0bcd39ac5 Mon Sep 17 00:00:00 2001 From: Mike Griese Date: Tue, 14 May 2024 14:20:06 -0500 Subject: [PATCH] Remove spaces from snippets in the SXNUI (#17261) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Couple different issues: * The suggestions UI can't filter snippets without a name, that have a space in them, because '␣' != ' '. This instead removes the visualized space from the name shown in the SXN UI. * Similarly, we generate an action with leading backspaces to remove the current commandline. Then we visualize those BS's as a part of the generated name. Same thing - can't filter to that. Before in blue: ![image](https://github.com/microsoft/terminal/assets/18356694/b65e102b-3d23-4d66-9fb9-cfcbb32cf963) ![image](https://github.com/microsoft/terminal/assets/18356694/0a0f4a0e-3ba5-4b61-8f80-1b988fbbb319) closes #16577 closes #16578 --- .../TerminalSettingsModel/ActionMap.cpp | 20 +++++++++++++++++-- src/inc/til/string.h | 18 +++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/cascadia/TerminalSettingsModel/ActionMap.cpp b/src/cascadia/TerminalSettingsModel/ActionMap.cpp index 220cd825565..7e480a33866 100644 --- a/src/cascadia/TerminalSettingsModel/ActionMap.cpp +++ b/src/cascadia/TerminalSettingsModel/ActionMap.cpp @@ -6,6 +6,7 @@ #include "ActionMap.h" #include "Command.h" #include "AllShortcutActions.h" +#include #include "ActionMap.g.cpp" @@ -964,17 +965,32 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation cmdImpl.copy_from(winrt::get_self(command)); const auto inArgs{ command.ActionAndArgs().Args().try_as() }; - + const auto inputString{ inArgs ? inArgs.Input() : L"" }; auto args = winrt::make_self( winrt::hstring{ fmt::format(FMT_COMPILE(L"{:\x7f^{}}{}"), L"", numBackspaces, - (std::wstring_view)(inArgs ? inArgs.Input() : L"")) }); + inputString) }); Model::ActionAndArgs actionAndArgs{ ShortcutAction::SendInput, *args }; auto copy = cmdImpl->Copy(); copy->ActionAndArgs(actionAndArgs); + if (!copy->HasName()) + { + // Here, we want to manually generate a send input name, but + // without visualizing space and backspace + // + // This is exactly the body of SendInputArgs::GenerateName, but + // with visualize_nonspace_control_codes instead of + // visualize_control_codes, to make filtering in the suggestions + // UI easier. + + const auto escapedInput = til::visualize_nonspace_control_codes(std::wstring{ inputString }); + const auto name = fmt::format(std::wstring_view(RS_(L"SendInputCommandKey")), escapedInput); + copy->Name(winrt::hstring{ name }); + } + return *copy; }; diff --git a/src/inc/til/string.h b/src/inc/til/string.h index fcb6905e282..5070ae472fe 100644 --- a/src/inc/til/string.h +++ b/src/inc/til/string.h @@ -24,6 +24,24 @@ namespace til // Terminal Implementation Library. Also: "Today I Learned" } return str; } + // The same as the above, but it doesn't visualize BS nor SPC. + _TIL_INLINEPREFIX std::wstring visualize_nonspace_control_codes(std::wstring str) noexcept + { + for (auto& ch : str) + { + // NOT backspace! + if (ch < 0x20 && ch != 0x08) + { + ch += 0x2400; + } + // NOT space + else if (ch == 0x7f) + { + ch = 0x2421; // replace del with ␡ + } + } + return str; + } _TIL_INLINEPREFIX std::wstring visualize_control_codes(std::wstring_view str) {