Use til::hstring_format wherever possible (#19733)

A follow-up of #19721 which added `til::hstring_format`.
This commit is contained in:
Leonard Hecker 2026-01-16 22:04:49 +01:00 committed by GitHub
parent 04bcba39ae
commit c4557594ce
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
20 changed files with 53 additions and 63 deletions

View File

@ -823,7 +823,7 @@ namespace TerminalAppLocalTests
VERIFY_ARE_EQUAL(3u, command.NestedCommands().Size());
_logCommandNames(command.NestedCommands());
{
winrt::hstring childCommandName{ fmt::format(L"Split pane, profile: {}", name) };
const auto childCommandName = fmt::format(FMT_COMPILE(L"Split pane, profile: {}"), name);
auto childCommand = command.NestedCommands().Lookup(childCommandName);
VERIFY_IS_NOT_NULL(childCommand);
auto childActionAndArgs = childCommand.ActionAndArgs();
@ -845,7 +845,7 @@ namespace TerminalAppLocalTests
VERIFY_IS_FALSE(childCommand.HasNestedCommands());
}
{
winrt::hstring childCommandName{ fmt::format(L"Split pane, split: down, profile: {}", name) };
const auto childCommandName = fmt::format(FMT_COMPILE(L"Split pane, split: down, profile: {}"), name);
auto childCommand = command.NestedCommands().Lookup(childCommandName);
VERIFY_IS_NOT_NULL(childCommand);
auto childActionAndArgs = childCommand.ActionAndArgs();
@ -867,7 +867,7 @@ namespace TerminalAppLocalTests
VERIFY_IS_FALSE(childCommand.HasNestedCommands());
}
{
winrt::hstring childCommandName{ fmt::format(L"Split pane, split: right, profile: {}", name) };
const auto childCommandName = fmt::format(FMT_COMPILE(L"Split pane, split: right, profile: {}"), name);
auto childCommand = command.NestedCommands().Lookup(childCommandName);
VERIFY_IS_NOT_NULL(childCommand);
auto childActionAndArgs = childCommand.ActionAndArgs();
@ -960,8 +960,8 @@ namespace TerminalAppLocalTests
for (auto name : std::vector<std::wstring>({ L"profile0", L"profile1", L"profile2" }))
{
winrt::hstring commandName{ fmt::format(L"New tab, profile: {}", name) };
auto command = rootCommand.NestedCommands().Lookup(commandName);
const auto childCommandName = fmt::format(FMT_COMPILE(L"New tab, profile: {}"), name);
auto command = rootCommand.NestedCommands().Lookup(childCommandName);
VERIFY_IS_NOT_NULL(command);
auto actionAndArgs = command.ActionAndArgs();
VERIFY_IS_NOT_NULL(actionAndArgs);
@ -1078,7 +1078,7 @@ namespace TerminalAppLocalTests
_logCommandNames(command.NestedCommands());
{
winrt::hstring childCommandName{ fmt::format(L"Split pane, profile: {}", name) };
const auto childCommandName = fmt::format(FMT_COMPILE(L"Split pane, profile: {}"), name);
auto childCommand = command.NestedCommands().Lookup(childCommandName);
VERIFY_IS_NOT_NULL(childCommand);
auto childActionAndArgs = childCommand.ActionAndArgs();
@ -1100,7 +1100,7 @@ namespace TerminalAppLocalTests
VERIFY_IS_FALSE(childCommand.HasNestedCommands());
}
{
winrt::hstring childCommandName{ fmt::format(L"Split pane, split: down, profile: {}", name) };
const auto childCommandName = fmt::format(FMT_COMPILE(L"Split pane, split: down, profile: {}"), name);
auto childCommand = command.NestedCommands().Lookup(childCommandName);
VERIFY_IS_NOT_NULL(childCommand);
auto childActionAndArgs = childCommand.ActionAndArgs();
@ -1122,7 +1122,7 @@ namespace TerminalAppLocalTests
VERIFY_IS_FALSE(childCommand.HasNestedCommands());
}
{
winrt::hstring childCommandName{ fmt::format(L"Split pane, split: right, profile: {}", name) };
const auto childCommandName = fmt::format(FMT_COMPILE(L"Split pane, split: right, profile: {}"), name);
auto childCommand = command.NestedCommands().Lookup(childCommandName);
VERIFY_IS_NOT_NULL(childCommand);
auto childActionAndArgs = childCommand.ActionAndArgs();

View File

@ -914,7 +914,7 @@ namespace winrt::TerminalApp::implementation
// Build the commandline to pass to wt for this set of NewTerminalArgs
// `-w -1` will ensure a new window is created.
const auto commandline = terminalArgs.ToCommandline();
winrt::hstring cmdline{ fmt::format(FMT_COMPILE(L"-w -1 new-tab {}"), commandline) };
const auto cmdline = fmt::format(FMT_COMPILE(L"-w -1 new-tab {}"), commandline);
// Build the args to ShellExecuteEx. We need to use ShellExecuteEx so we
// can pass the SEE_MASK_NOASYNC flag. That flag allows us to safely

View File

@ -2137,7 +2137,7 @@ namespace winrt::TerminalApp::implementation
const auto profileName{ profile.Name() };
if (profileName != Title())
{
return winrt::hstring{ fmt::format(FMT_COMPILE(L"{}: {}"), profileName, Title()) };
return til::hstring_format(FMT_COMPILE(L"{}: {}"), profileName, Title());
}
}

View File

@ -2,7 +2,6 @@
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Natvis Include="$(SolutionDir)tools\ConsoleTypes.natvis" />
<Natvis Include="$(MSBuildThisFileDirectory)..\..\natvis\wil.natvis" />
</ItemGroup>
<ItemGroup>
<PRIResource Include="Resources\en-US\Resources.resw" />
@ -33,8 +32,7 @@
</ClCompile>
<ClCompile Include="Toast.cpp" />
<ClCompile Include="LanguageProfileNotifier.cpp" />
<ClCompile Include="Monarch.cpp" />
<ClCompile Include="Peasant.cpp" />
<ClCompile Include="TerminalSettingsCache.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="pch.h" />
@ -60,14 +58,10 @@
<ClInclude Include="fzf/fzf.h">
<Filter>fzf</Filter>
</ClInclude>
<ClInclude Include="fzf/LICENSE">
<Filter>fzf</Filter>
</ClInclude>
<ClInclude Include="Toast.h" />
<ClInclude Include="LanguageProfileNotifier.h" />
<ClInclude Include="WindowsPackageManagerFactory.h" />
<ClInclude Include="Monarch.h" />
<ClInclude Include="Peasant.h" />
<ClInclude Include="TerminalSettingsCache.h" />
</ItemGroup>
<ItemGroup>
<Midl Include="AppLogic.idl">
@ -90,7 +84,8 @@
<Midl Include="TerminalWindow.idl" />
<Midl Include="TaskbarState.idl" />
<Midl Include="IPaneContent.idl" />
<Midl Include="Monarch.idl" />
<Midl Include="Remoting.idl" />
<Midl Include="HighlightedTextControl.idl" />
</ItemGroup>
<ItemGroup>
<Page Include="MinMaxCloseControl.xaml">
@ -123,6 +118,7 @@
<Page Include="AboutDialog.xaml" />
<Page Include="SuggestionsControl.xaml" />
<Page Include="SnippetsPaneContent.xaml" />
<Page Include="MarkdownPaneContent.xaml" />
</ItemGroup>
<ItemGroup>
<Filter Include="app">

View File

@ -1393,9 +1393,7 @@ namespace winrt::TerminalApp::implementation
// - a string for displaying the name of the window.
winrt::hstring WindowProperties::WindowIdForDisplay() const noexcept
{
return winrt::hstring{ fmt::format(FMT_COMPILE(L"{}: {}"),
std::wstring_view(RS_(L"WindowIdLabel")),
_WindowId) };
return til::hstring_format(FMT_COMPILE(L"{}: {}"), std::wstring_view(RS_(L"WindowIdLabel")), _WindowId);
}
// Method Description:
@ -1406,9 +1404,7 @@ namespace winrt::TerminalApp::implementation
// - a string for displaying the name of the window.
winrt::hstring WindowProperties::WindowNameForDisplay() const noexcept
{
return _WindowName.empty() ?
winrt::hstring{ fmt::format(FMT_COMPILE(L"<{}>"), RS_(L"UnnamedWindowName")) } :
_WindowName;
return _WindowName.empty() ? til::hstring_format(FMT_COMPILE(L"<{}>"), RS_(L"UnnamedWindowName")) : _WindowName;
}
bool WindowProperties::IsQuakeWindow() const noexcept

View File

@ -159,7 +159,7 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
if (HasLibraryResourceWithName(key))
{
displayString = GetLibraryResourceString(key);
displayString = hstring{ fmt::format(FMT_COMPILE(L"{} ({})"), displayString, std::wstring_view{ tagString }) };
displayString = til::hstring_format(FMT_COMPILE(L"{} ({})"), displayString, std::wstring_view{ tagString });
}
}
@ -204,7 +204,7 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
winrt::hstring FontKeyValuePair::AutomationName()
{
return hstring{ fmt::format(FMT_COMPILE(L"{}: {}"), KeyDisplayStringRef(), _value) };
return til::hstring_format(FMT_COMPILE(L"{}: {}"), KeyDisplayStringRef(), _value);
}
AppearanceViewModel::AppearanceViewModel(const Model::AppearanceConfig& appearance) :
@ -515,7 +515,7 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
const auto idx = getLocalizedStringIndex(names.get(), localeName, 0);
const auto localizedName = getLocalizedStringByIndex(names.get(), idx);
const auto tagString = tagToString(tag);
hstring displayString{ fmt::format(FMT_COMPILE(L"{} ({})"), localizedName, std::wstring_view{ tagString }) };
const auto displayString = til::hstring_format(FMT_COMPILE(L"{} ({})"), localizedName, std::wstring_view{ tagString });
const auto value = axesVector[i].value;
@ -1193,7 +1193,7 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
{
const auto prefix = fontSpecView.substr(0, idx);
const auto suffix = std::wstring_view{ fontName };
fontSpec = winrt::hstring{ fmt::format(FMT_COMPILE(L"{}, {}"), prefix, suffix) };
fontSpec = til::hstring_format(FMT_COMPILE(L"{}, {}"), prefix, suffix);
}
else
{

View File

@ -56,7 +56,7 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
{
if (IsDefaultScheme())
{
return hstring{ fmt::format(FMT_COMPILE(L"{} ({})"), Name(), RS_(L"ColorScheme_DefaultTag/Text")) };
return til::hstring_format(FMT_COMPILE(L"{} ({})"), Name(), RS_(L"ColorScheme_DefaultTag/Text"));
}
return Name();
}

View File

@ -66,12 +66,12 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
hstring AccessibleName() const
{
return hstring{ fmt::format(FMT_COMPILE(L"{} RGB({}, {}, {})"), _Name, _Color.R, _Color.G, _Color.B) };
return til::hstring_format(FMT_COMPILE(L"{} RGB({}, {}, {})"), _Name, _Color.R, _Color.G, _Color.B);
}
hstring NameWithHexCode() const
{
return hstring{ fmt::format(FMT_COMPILE(L"{} #{:02X}{:02X}{:02X}"), _Name, _Color.R, _Color.G, _Color.B) };
return til::hstring_format(FMT_COMPILE(L"{} #{:02X}{:02X}{:02X}"), _Name, _Color.R, _Color.G, _Color.B);
}
til::property_changed_event PropertyChanged;

View File

@ -479,7 +479,7 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
const auto source = _package.Source();
if (const auto displayName = _package.DisplayName(); !displayName.empty())
{
return hstring{ fmt::format(FMT_COMPILE(L"{}, {}"), displayName, source) };
return til::hstring_format(FMT_COMPILE(L"{}, {}"), displayName, source);
}
return source;
}
@ -491,7 +491,7 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
hstring FragmentProfileViewModel::AccessibleName() const noexcept
{
return hstring{ fmt::format(FMT_COMPILE(L"{}, {}"), Profile().Name(), SourceName()) };
return til::hstring_format(FMT_COMPILE(L"{}, {}"), Profile().Name(), SourceName());
}
bool FragmentColorSchemeViewModel::SortAscending(const Editor::FragmentColorSchemeViewModel& lhs, const Editor::FragmentColorSchemeViewModel& rhs)
@ -501,7 +501,7 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
hstring FragmentColorSchemeViewModel::AccessibleName() const noexcept
{
return hstring{ fmt::format(FMT_COMPILE(L"{}, {}"), ColorSchemeVM().Name(), SourceName()) };
return til::hstring_format(FMT_COMPILE(L"{}, {}"), ColorSchemeVM().Name(), SourceName());
}
DataTemplate ExtensionPackageTemplateSelector::SelectTemplateCore(const IInspectable& item, const DependencyObject& /*container*/)

View File

@ -203,7 +203,7 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
winrt::hstring LaunchViewModel::LaunchSizeCurrentValue() const
{
return winrt::hstring{ fmt::format(FMT_COMPILE(L"{} × {}"), InitialCols(), InitialRows()) };
return til::hstring_format(FMT_COMPILE(L"{} × {}"), InitialCols(), InitialRows());
}
winrt::hstring LaunchViewModel::LaunchParametersCurrentValue()
@ -225,7 +225,7 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
}
// Append the CenterOnLaunch part
result = CenterOnLaunch() ? winrt::hstring{ fmt::format(FMT_COMPILE(L"{}, {}"), result, RS_(L"Globals_CenterOnLaunchCentered")) } : result;
result = CenterOnLaunch() ? til::hstring_format(FMT_COMPILE(L"{}, {}"), result, RS_(L"Globals_CenterOnLaunchCentered")) : result;
return result;
}

View File

@ -555,7 +555,7 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
{
return action.Name();
}
return hstring{ fmt::format(L"{}: {}", RS_(L"NewTabMenu_ActionNotFound"), actionID) };
return til::hstring_format(FMT_COMPILE(L"{}: {}"), RS_(L"NewTabMenu_ActionNotFound"), actionID);
}
hstring ActionEntryViewModel::Icon() const

View File

@ -760,7 +760,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
{
return RS_switchable_(L"NewWindowCommandKey");
}
return winrt::hstring{ fmt::format(FMT_COMPILE(L"{}, {}"), RS_switchable_(L"NewWindowCommandKey"), newTerminalArgsStr) };
return til::hstring_format(FMT_COMPILE(L"{}, {}"), RS_switchable_(L"NewWindowCommandKey"), newTerminalArgsStr);
}
winrt::hstring PrevTabArgs::GenerateName(const winrt::WARC::ResourceContext& context) const
@ -771,7 +771,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
}
const auto mode = SwitcherMode().Value() == TabSwitcherMode::MostRecentlyUsed ? L"most recently used" : L"in order";
return winrt::hstring(fmt::format(FMT_COMPILE(L"{}, {}"), RS_switchable_(L"PrevTabCommandKey"), mode));
return til::hstring_format(FMT_COMPILE(L"{}, {}"), RS_switchable_(L"PrevTabCommandKey"), mode);
}
winrt::hstring NextTabArgs::GenerateName(const winrt::WARC::ResourceContext& context) const
@ -782,7 +782,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
}
const auto mode = SwitcherMode().Value() == TabSwitcherMode::MostRecentlyUsed ? L"most recently used" : L"in order";
return winrt::hstring(fmt::format(FMT_COMPILE(L"{}, {}"), RS_switchable_(L"NextTabCommandKey"), mode));
return til::hstring_format(FMT_COMPILE(L"{}, {}"), RS_switchable_(L"NextTabCommandKey"), mode);
}
winrt::hstring RenameWindowArgs::GenerateName(const winrt::WARC::ResourceContext& context) const

View File

@ -1114,11 +1114,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
const auto inArgs{ command.ActionAndArgs().Args().try_as<Model::SendInputArgs>() };
const auto inputString{ inArgs ? inArgs.Input() : L"" };
auto args = winrt::make_self<SendInputArgs>(
winrt::hstring{ fmt::format(FMT_COMPILE(L"{:\x7f^{}}{}"),
L"",
numBackspaces,
inputString) });
auto args = winrt::make_self<SendInputArgs>(til::hstring_format(FMT_COMPILE(L"{:\x7f^{}}{}"), L"", numBackspaces, inputString));
Model::ActionAndArgs actionAndArgs{ ShortcutAction::SendInput, *args };
auto copy = cmdImpl->Copy();

View File

@ -1122,11 +1122,11 @@ bool SettingsLoader::_addOrMergeUserColorScheme(const winrt::com_ptr<implementat
userSettings.fixupsAppliedDuringLoad = true; // Make sure we save the settings.
if (!existingScheme->IsEquivalentForSettingsMergePurposes(newScheme))
{
hstring newName{ fmt::format(FMT_COMPILE(L"{} (modified)"), existingScheme->Name()) };
auto newName = til::hstring_format(FMT_COMPILE(L"{} (modified)"), existingScheme->Name());
int differentiator = 2;
while (userSettings.colorSchemes.contains(newName))
{
newName = hstring{ fmt::format(FMT_COMPILE(L"{} (modified {})"), existingScheme->Name(), differentiator++) };
newName = til::hstring_format(FMT_COMPILE(L"{} (modified {})"), existingScheme->Name(), differentiator++);
}
// Rename the user's scheme.
existingScheme->Name(newName);

View File

@ -692,11 +692,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
JsonUtils::GetValueForKey(element, "ListItemText", listText);
JsonUtils::GetValueForKey(element, "ToolTip", tooltipText);
auto args = winrt::make_self<SendInputArgs>(
winrt::hstring{ fmt::format(FMT_COMPILE(L"{:\x7f^{}}{}"),
L"",
replaceLength,
completionText) });
auto args = winrt::make_self<SendInputArgs>(til::hstring_format(FMT_COMPILE(L"{:\x7f^{}}{}"), L"", replaceLength, completionText));
Model::ActionAndArgs actionAndArgs{ ShortcutAction::SendInput, *args };
@ -799,8 +795,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
{
continue;
}
auto args = winrt::make_self<SendInputArgs>(
winrt::hstring{ fmt::format(FMT_COMPILE(L"{}{}{}"), cdText, backspaces, line) });
auto args = winrt::make_self<SendInputArgs>(til::hstring_format(FMT_COMPILE(L"{}{}{}"), cdText, backspaces, line));
Model::ActionAndArgs actionAndArgs{ ShortcutAction::SendInput, *args };

View File

@ -35,7 +35,7 @@ winrt::hstring DefaultTerminal::Version() const
return winrt::hstring{};
}
return winrt::hstring{ fmt::format(FMT_COMPILE(L"{}.{}.{}.{}"), version.major, version.minor, version.build, version.revision) };
return til::hstring_format(FMT_COMPILE(L"{}.{}.{}.{}"), version.major, version.minor, version.build, version.revision);
}
winrt::hstring DefaultTerminal::Author() const

View File

@ -30,7 +30,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
hstring ToString()
{
return hstring{ fmt::format(FMT_COMPILE(L"{}, {}, {}"), Name(), Author(), Version()) };
return til::hstring_format(FMT_COMPILE(L"{}, {}, {}"), Name(), Author(), Version());
}
hstring Name() const;

View File

@ -36,12 +36,12 @@ using namespace winrt::Microsoft::Terminal::Settings::Model;
winrt::hstring _getProfileName(const std::wstring_view& hostName) noexcept
{
return winrt::hstring{ fmt::format(FMT_COMPILE(L"{0}{1}"), PROFILE_TITLE_PREFIX, hostName) };
return til::hstring_format(FMT_COMPILE(L"{0}{1}"), PROFILE_TITLE_PREFIX, hostName);
}
winrt::hstring _getProfileCommandLine(const std::wstring_view& sshExePath, const std::wstring_view& hostName) noexcept
{
return winrt::hstring{ fmt::format(FMT_COMPILE(LR"("{0}" {1})"), sshExePath, hostName) };
return til::hstring_format(FMT_COMPILE(LR"("{0}" {1})"), sshExePath, hostName);
}
/*static*/ bool SshHostGenerator::_tryFindSshExePath(std::wstring& sshExePath) noexcept

View File

@ -1,5 +1,8 @@
#include "pch.h"
#include "Converters.h"
#include <til/winrt.h>
#include "Converters.g.cpp"
#pragma warning(disable : 26497) // We will make these functions constexpr, as they are part of an ABI boundary.
@ -31,7 +34,7 @@ namespace winrt::Microsoft::Terminal::UI::implementation
winrt::hstring Converters::PercentageToPercentageString(double value)
{
return winrt::hstring{ fmt::format(FMT_COMPILE(L"{:.0f}%"), value * 100.0) };
return til::hstring_format(FMT_COMPILE(L"{:.0f}%"), value * 100.0);
}
// Strings

View File

@ -121,14 +121,18 @@ namespace til // Terminal Implementation Library. Also: "Today I Learned"
};
// fmt::format but for HSTRING.
template<typename... Args>
winrt::hstring hstring_format(Args&&... args)
//
// NOTE: This will fail to compile if you pass a string literal as the first argument (the format argument).
// This is because std::forwarding literals turns them from constant expressions into regular ones.
// It can be fixed by giving the first argument an explicit type. I intentionally didn't do that
// because if you pass a string literal, you really ought to pass a FMT_COMPILE() instead.
winrt::hstring hstring_format(auto&&... args)
{
// We could use fmt::formatted_size and winrt::impl::hstring_builder here,
// and this would make formatting of large strings a bit faster, and a bit slower
// for short strings. More importantly, I hit compilation issues so I dropped that.
fmt::basic_memory_buffer<wchar_t> buf;
fmt::format_to(std::back_inserter(buf), args...);
fmt::format_to(std::back_inserter(buf), std::forward<decltype(args)>(args)...);
return winrt::hstring{ buf.data(), gsl::narrow<uint32_t>(buf.size()) };
}
}