Skip to content

Commit

Permalink
add a lil polish
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos-zamora committed Mar 26, 2024
1 parent 68ed03d commit 222e334
Show file tree
Hide file tree
Showing 9 changed files with 7 additions and 209 deletions.
3 changes: 2 additions & 1 deletion src/cascadia/TerminalApp/AppActionHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1351,7 +1351,8 @@ namespace winrt::TerminalApp::implementation
if (WI_IsFlagSet(source, SuggestionsSource::WinGetCommandNotFound) &&
context != nullptr)
{
const auto recentCommands = Command::ToSendInputCommands(context.WinGetSuggestions());
// use OEM icon
const auto recentCommands = Command::ToSendInputCommands(context.WinGetSuggestions(), hstring{ L"\ue74c" });
for (const auto& t : recentCommands)
{
commandsCollection.push_back(t);
Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalApp/Resources/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -898,4 +898,4 @@
<data name="RestartConnectionToolTip" xml:space="preserve">
<value>Restart the active pane connection</value>
</data>
</root>
</root>
187 changes: 0 additions & 187 deletions src/cascadia/TerminalApp/TerminalPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1672,8 +1672,6 @@ namespace winrt::TerminalApp::implementation

term.ShowWindowChanged({ get_weak(), &TerminalPage::_ShowWindowChangedHandler });

//term.SearchMissingCommand({ get_weak(), &TerminalPage::_SearchMissingCommandHandler });

// Don't even register for the event if the feature is compiled off.
if constexpr (Feature_ShellCompletions::IsEnabled())
{
Expand Down Expand Up @@ -2956,191 +2954,6 @@ namespace winrt::TerminalApp::implementation
ShowWindowChanged.raise(*this, args);
}

void TerminalPage::_SearchMissingCommandHandler(const IInspectable sender, const winrt::Microsoft::Terminal::Control::SearchMissingCommandEventArgs args)
{
#if 0
static constexpr CLSID CLSID_PackageManager = { 0xC53A4F16, 0x787E, 0x42A4, 0xB3, 0x04, 0x29, 0xEF, 0xFB, 0x4B, 0xF5, 0x97 }; //C53A4F16-787E-42A4-B304-29EFFB4BF597
static constexpr CLSID CLSID_FindPackagesOptions = { 0x572DED96, 0x9C60, 0x4526, { 0x8F, 0x92, 0xEE, 0x7D, 0x91, 0xD3, 0x8C, 0x1A } }; //572DED96-9C60-4526-8F92-EE7D91D38C1A
static constexpr CLSID CLSID_PackageMatchFilter = { 0xD02C9DAF, 0x99DC, 0x429C, { 0xB5, 0x03, 0x4E, 0x50, 0x4E, 0x4A, 0xB0, 0x00 } }; //D02C9DAF-99DC-429C-B503-4E504E4AB000

static constexpr unsigned int maxSuggestions = 5;
bool tooManySuggestions = false;

// TODO CARLOS: this is where we fail! "Class not registered" error
PackageManager pkgManager = winrt::create_instance<PackageManager>(CLSID_PackageManager, CLSCTX_ALL);
auto catalogRef = pkgManager.GetPredefinedPackageCatalog(PredefinedPackageCatalog::OpenWindowsCatalog);
auto connectResult = catalogRef.Connect();
int retryCount = 0;
while (connectResult.Status() != ConnectResultStatus::Ok && retryCount < 3)
{
connectResult = catalogRef.Connect();
++retryCount;
}
if (connectResult.Status() != ConnectResultStatus::Ok)
{
return;
}
auto catalog = connectResult.PackageCatalog();

// Perform the query (search by command)
auto packageMatchFilter = winrt::create_instance<PackageMatchFilter>(CLSID_PackageMatchFilter, CLSCTX_ALL);
auto findPackagesOptions = winrt::create_instance<FindPackagesOptions>(CLSID_FindPackagesOptions, CLSCTX_ALL);

// Helper lambda to apply a filter to the query
auto applyPackageMatchFilter = [&packageMatchFilter, &findPackagesOptions](PackageMatchField field, PackageFieldMatchOption matchOption, hstring query) {
// Configure filter
packageMatchFilter.Field(field);
packageMatchFilter.Option(matchOption);
packageMatchFilter.Value(query);

// Apply filter
findPackagesOptions.ResultLimit(maxSuggestions + 1u);
findPackagesOptions.Filters().Clear();
findPackagesOptions.Filters().Append(packageMatchFilter);
};

// Helper lambda to retrieve the best matching package(s) from the query's result
auto tryGetBestMatchingPackage = [&tooManySuggestions](IVectorView<MatchResult> matches) {
std::vector<CatalogPackage> results;
results.reserve(std::min(matches.Size(), maxSuggestions));
if (matches.Size() == 1)
{
// One match --> return the package
results.emplace_back(matches.GetAt(0).CatalogPackage());
}
else if (matches.Size() > 1)
{
// Multiple matches --> display top 5 matches (prioritize best matches first)
std::queue<CatalogPackage> bestExactMatches, secondaryMatches, tertiaryMatches;
for (auto match : matches)
{
switch (match.MatchCriteria().Option())
{
case PackageFieldMatchOption::EqualsCaseInsensitive:
case PackageFieldMatchOption::Equals:
bestExactMatches.push(match.CatalogPackage());
break;
case PackageFieldMatchOption::StartsWithCaseInsensitive:
secondaryMatches.push(match.CatalogPackage());
break;
case PackageFieldMatchOption::ContainsCaseInsensitive:
tertiaryMatches.push(match.CatalogPackage());
break;
}
}

// Now return the top maxSuggestions
while (results.size() < maxSuggestions)
{
if (bestExactMatches.size() > 0)
{
results.emplace_back(bestExactMatches.front());
bestExactMatches.pop();
}
else if (secondaryMatches.size() > 0)
{
results.emplace_back(secondaryMatches.front());
secondaryMatches.pop();
}
else if (tertiaryMatches.size() > 0)
{
results.emplace_back(tertiaryMatches.front());
tertiaryMatches.pop();
}
else
{
break;
}
}
}
tooManySuggestions = matches.Size() > maxSuggestions;
return results;
};

// Search by command
auto missingCmd = args.MissingCommand();
std::wstring searchOption = L"command";
applyPackageMatchFilter(PackageMatchField::Command, PackageFieldMatchOption::StartsWithCaseInsensitive, missingCmd);
auto findPackagesResult = catalog.FindPackages(findPackagesOptions);
auto matches = findPackagesResult.Matches();
auto pkgList = tryGetBestMatchingPackage(matches);
if (pkgList.empty())
{
// No matches found --> search by name
applyPackageMatchFilter(PackageMatchField::Name, PackageFieldMatchOption::ContainsCaseInsensitive, missingCmd);

findPackagesResult = catalog.FindPackages(findPackagesOptions);
matches = findPackagesResult.Matches();
pkgList = tryGetBestMatchingPackage(matches);
searchOption = L"name";

if (pkgList.empty())
{
// No matches found --> search by moniker
applyPackageMatchFilter(PackageMatchField::Moniker, PackageFieldMatchOption::ContainsCaseInsensitive, missingCmd);

// Perform the query (search by name)
findPackagesResult = catalog.FindPackages(findPackagesOptions);
matches = findPackagesResult.Matches();
pkgList = tryGetBestMatchingPackage(matches);
searchOption = L"moniker";
}
}

// Display packages in UI
if (!pkgList.empty())
{
std::vector<std::wstring> suggestions;
suggestions.reserve(pkgList.size());
for (auto pkg : pkgList)
{
suggestions.emplace_back(fmt::format(L"winget install --id {}", pkg.Id()));
}

std::wstring footer = tooManySuggestions ?
fmt::format(L"winget search --{} {}", searchOption, missingCmd) :
L"";

// TODO CARLOS: no more info bar; replace!
//ShowCommandNotFoundInfoBar(suggestions, footer);
}
#elif defined(DEBUG) || defined(_DEBUG) || defined(DBG)
//const bool tooManySuggestions = false;
//const std::wstring searchOption = L"command";
const std::wstring missingCmd = args.MissingCommand().data();
std::vector<std::wstring> pkgList = { L"pkg1", L"pkg2", L"pkg3" };
std::vector<hstring> suggestions;
suggestions.reserve(pkgList.size());
for (auto pkg : pkgList)
{
suggestions.emplace_back(fmt::format(L"winget install --id {}", pkg));
}

// This will come in on a background (not-UI, not output) thread.

// Parse the json string into a collection of actions
try
{
//auto commandsCollection = Command::ParsePowerShellMenuComplete(args.MenuJson(),
// args.ReplacementLength());

auto suggestionsWinRT = winrt::single_threaded_vector<hstring>(std::move(suggestions));
auto commandsCollection = Command::ToSendInputCommands(suggestionsWinRT);

//auto weakThis{ get_weak() };
//Dispatcher().RunAsync(CoreDispatcherPriority::Normal, [weakThis, commandsCollection, sender]() {
// // On the UI thread...
// if (const auto& page{ weakThis.get() })
// {
// // Open the Suggestions UI with the commands from the control
// page->_OpenSuggestions(sender.try_as<TermControl>(), commandsCollection, SuggestionsMode::Menu, L"");
// }
//});
}
CATCH_LOG();
#endif
}

// Method Description:
// - Paste text from the Windows Clipboard to the focused terminal
void TerminalPage::_PasteText()
Expand Down
1 change: 0 additions & 1 deletion src/cascadia/TerminalControl/ControlCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,6 @@ namespace winrt::Microsoft::Terminal::Control::implementation
til::typed_event<IInspectable, Control::UpdateSelectionMarkersEventArgs> UpdateSelectionMarkers;
til::typed_event<IInspectable, Control::OpenHyperlinkEventArgs> OpenHyperlink;
til::typed_event<IInspectable, Control::CompletionsChangedEventArgs> CompletionsChanged;
til::typed_event<IInspectable, Control::SearchMissingCommandEventArgs> SearchMissingCommand;

til::typed_event<> CloseTerminalRequested;
til::typed_event<> RestartTerminalRequested;
Expand Down
10 changes: 0 additions & 10 deletions src/cascadia/TerminalControl/EventArgs.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include "KeySentEventArgs.g.h"
#include "CharSentEventArgs.g.h"
#include "StringSentEventArgs.g.h"
#include "SearchMissingCommandEventArgs.g.h"

namespace winrt::Microsoft::Terminal::Control::implementation
{
Expand Down Expand Up @@ -252,15 +251,6 @@ namespace winrt::Microsoft::Terminal::Control::implementation

WINRT_PROPERTY(winrt::hstring, Text);
};

struct SearchMissingCommandEventArgs : public SearchMissingCommandEventArgsT<SearchMissingCommandEventArgs>
{
public:
SearchMissingCommandEventArgs(const winrt::hstring& missingCommand) :
_MissingCommand(missingCommand) {}

WINRT_PROPERTY(winrt::hstring, MissingCommand);
};
}

namespace winrt::Microsoft::Terminal::Control::factory_implementation
Expand Down
5 changes: 0 additions & 5 deletions src/cascadia/TerminalControl/EventArgs.idl
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,4 @@ namespace Microsoft.Terminal.Control
{
String Text { get; };
}

runtimeclass SearchMissingCommandEventArgs
{
String MissingCommand { get; };
}
}
4 changes: 2 additions & 2 deletions src/cascadia/TerminalSettingsModel/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
return winrt::single_threaded_vector<Model::Command>(std::move(result));
}

IVector<Model::Command> Command::ToSendInputCommands(IVector<hstring> commands)
IVector<Model::Command> Command::ToSendInputCommands(IVector<hstring> commands, hstring iconPath)
{
if (!commands)
{
Expand All @@ -814,7 +814,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
auto c = winrt::make_self<Command>();
c->_ActionAndArgs = actionAndArgs;
c->_name = command;
c->_iconPath = L"\ue74c"; // OEM icon
c->_iconPath = iconPath;
result.push_back(*c);
}

Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalSettingsModel/Command.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
static Windows::Foundation::Collections::IVector<Model::Command> HistoryToCommands(Windows::Foundation::Collections::IVector<winrt::hstring> history,
winrt::hstring currentCommandline,
bool directories);
static Windows::Foundation::Collections::IVector<Model::Command> ToSendInputCommands(Windows::Foundation::Collections::IVector<winrt::hstring> commands);
static Windows::Foundation::Collections::IVector<Model::Command> ToSendInputCommands(Windows::Foundation::Collections::IVector<winrt::hstring> commands, hstring iconPath);

WINRT_PROPERTY(ExpandCommandType, IterateOn, ExpandCommandType::None);
WINRT_PROPERTY(Model::ActionAndArgs, ActionAndArgs);
Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalSettingsModel/Command.idl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ namespace Microsoft.Terminal.Settings.Model

static IVector<Command> ParsePowerShellMenuComplete(String json, Int32 replaceLength);
static IVector<Command> HistoryToCommands(IVector<String> commandHistory, String commandline, Boolean directories);
static IVector<Command> ToSendInputCommands(IVector<String> commands);
static IVector<Command> ToSendInputCommands(IVector<String> commands, String iconPath);

}
}

0 comments on commit 222e334

Please sign in to comment.