Repository: microsoft/terminal
File: src/cascadia/TerminalApp/TerminalPage.cpp (C++)

Code before:
    void TerminalPage::_ShowWindowChangedHandler(const IInspectable /*sender*/, const Microsoft::Terminal::Control::ShowWindowArgs args)
    {
        ShowWindowChanged.raise(*this, args);
    }

    Windows::Foundation::IAsyncOperation<IVectorView<MatchResult>> TerminalPage::_FindPackagesInCatalogAsync(PackageCatalog catalog, PackageMatchField field, PackageFieldMatchOption matchOption, hstring query)
    {
        FindPackagesOptions findPackagesOptions = WindowsPackageManagerFactory::CreateFindPackagesOptions();
        PackageMatchFilter filter = WindowsPackageManagerFactory::CreatePackageMatchFilter();
        filter.Field(field);
        filter.Option(matchOption);
        filter.Value(query);
        findPackagesOptions.Filters().Append(filter);
        findPackagesOptions.ResultLimit(20);
        FindPackagesResult findPackagesResult{ co_await catalog.FindPackagesAsync(findPackagesOptions) };

        co_return findPackagesResult.Matches();
    }

    Windows::Foundation::IAsyncOperation<IVectorView<MatchResult>> TerminalPage::_FindPackageAsync(hstring query)
    {
        PackageManager packageManager = WindowsPackageManagerFactory::CreatePackageManager();
        PackageCatalogReference catalogRef{
            packageManager.GetPredefinedPackageCatalog(PredefinedPackageCatalog::OpenWindowsCatalog)
        };
        ConnectResult connectResult = catalogRef.Connect();
        if (connectResult.Status() != ConnectResultStatus::Ok)
        {
            co_return nullptr;
        }
        PackageCatalog catalog = connectResult.PackageCatalog();

        auto pkgList = co_await _FindPackagesInCatalogAsync(catalog, PackageMatchField::Command, PackageFieldMatchOption::StartsWithCaseInsensitive, query);
        if (pkgList.Size() > 0)
        {
            co_return pkgList;
        }
        pkgList = co_await _FindPackagesInCatalogAsync(catalog, PackageMatchField::Name, PackageFieldMatchOption::ContainsCaseInsensitive, query);
        if (pkgList.Size() > 0)
        {
            co_return pkgList;
        }
        pkgList = co_await _FindPackagesInCatalogAsync(catalog, PackageMatchField::Moniker, PackageFieldMatchOption::ContainsCaseInsensitive, query);
        co_return pkgList;
    }

    Windows::Foundation::IAsyncAction TerminalPage::_SearchMissingCommandHandler(const IInspectable /*sender*/, const Microsoft::Terminal::Control::SearchMissingCommandEventArgs args)
    {
        assert(!Dispatcher().HasThreadAccess());

        if (!Feature_QuickFix::IsEnabled())

Review comment (performance):
We have a setting called `PackageCatalogBackgroundUpdateInterval` (on the `PackageCatalogReference`) that can be set here to control "freshness" of the data.  It will also affect the performance, as a background update does block the `Connect` call and thus can impact how quickly you can get results to the user.  There is also data transfer involved (not much, but also not 0).

The default is value is intended for interactive use, but this isn't directly interactive use.  I would suggest setting it to at least 24 hours, but more likely won't hurt.  The impacts are:
1. If the package containing the command was added less than `interval` ago, you may not find it
2. If a new version of the package was added less than `interval` ago, you may install that previously latest version (N-1)

Code after:
    void TerminalPage::_ShowWindowChangedHandler(const IInspectable /*sender*/, const Microsoft::Terminal::Control::ShowWindowArgs args)
    {
        ShowWindowChanged.raise(*this, args);
    }

    Windows::Foundation::IAsyncOperation<IVectorView<MatchResult>> TerminalPage::_FindPackageAsync(hstring query)
    {
        const PackageManager packageManager = WindowsPackageManagerFactory::CreatePackageManager();
        PackageCatalogReference catalogRef{
            packageManager.GetPredefinedPackageCatalog(PredefinedPackageCatalog::OpenWindowsCatalog)
        };
        catalogRef.PackageCatalogBackgroundUpdateInterval(std::chrono::hours(24));

        ConnectResult connectResult{ nullptr };
        for (int retries = 0;;)
        {
            connectResult = catalogRef.Connect();
            if (connectResult.Status() == ConnectResultStatus::Ok)
            {
                break;
            }

            if (++retries == 3)
            {
                co_return nullptr;
            }
        }

        PackageCatalog catalog = connectResult.PackageCatalog();
        // clang-format off
        static constexpr std::array<WinGetSearchParams, 3> searches{ {
            { .Field = PackageMatchField::Command, .MatchOption = PackageFieldMatchOption::StartsWithCaseInsensitive },
            { .Field = PackageMatchField::Name, .MatchOption = PackageFieldMatchOption::ContainsCaseInsensitive },
            { .Field = PackageMatchField::Moniker, .MatchOption = PackageFieldMatchOption::ContainsCaseInsensitive } } };
        // clang-format on

        PackageMatchFilter filter = WindowsPackageManagerFactory::CreatePackageMatchFilter();
        filter.Value(query);

        FindPackagesOptions options = WindowsPackageManagerFactory::CreateFindPackagesOptions();
        options.Filters().Append(filter);
        options.ResultLimit(20);

        IVectorView<MatchResult> pkgList;
        for (const auto& search : searches)
        {
            filter.Field(search.Field);
            filter.Option(search.MatchOption);

            const auto result = co_await catalog.FindPackagesAsync(options);
            pkgList = result.Matches();