-
Notifications
You must be signed in to change notification settings - Fork 7.8k
[cmdpal] Add Open URL fallback command for WebSearch ext #38685
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.WebSearch/Commands/OpenURLCommand.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| // Copyright (c) Microsoft Corporation | ||
| // The Microsoft Corporation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using Microsoft.CmdPal.Ext.WebSearch.Helpers; | ||
| using Microsoft.CommandPalette.Extensions.Toolkit; | ||
|
|
||
| using BrowserInfo = Microsoft.CmdPal.Ext.WebSearch.Helpers.DefaultBrowserInfo; | ||
|
|
||
| namespace Microsoft.CmdPal.Ext.WebSearch.Commands; | ||
|
|
||
| internal sealed partial class OpenURLCommand : InvokableCommand | ||
| { | ||
| private readonly SettingsManager _settingsManager; | ||
|
|
||
| public string Url { get; internal set; } = string.Empty; | ||
|
|
||
| internal OpenURLCommand(string url, SettingsManager settingsManager) | ||
| { | ||
| Url = url; | ||
| BrowserInfo.UpdateIfTimePassed(); | ||
| Icon = IconHelpers.FromRelativePath("Assets\\WebSearch.png"); | ||
| Name = string.Empty; | ||
| _settingsManager = settingsManager; | ||
| } | ||
|
|
||
| public override CommandResult Invoke() | ||
| { | ||
| if (!ShellHelpers.OpenCommandInShell(BrowserInfo.Path, BrowserInfo.ArgumentsPattern, $"{Url}")) | ||
| { | ||
| // TODO GH# 138 --> actually display feedback from the extension somewhere. | ||
| return CommandResult.KeepOpen(); | ||
| } | ||
|
|
||
| return CommandResult.Dismiss(); | ||
| } | ||
| } | ||
88 changes: 88 additions & 0 deletions
88
src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.WebSearch/FallbackOpenURLItem.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| // Copyright (c) Microsoft Corporation | ||
| // The Microsoft Corporation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System; | ||
| using System.Globalization; | ||
| using System.Text; | ||
| using Microsoft.CmdPal.Ext.WebSearch.Commands; | ||
| using Microsoft.CmdPal.Ext.WebSearch.Helpers; | ||
| using Microsoft.CmdPal.Ext.WebSearch.Properties; | ||
| using Microsoft.CommandPalette.Extensions.Toolkit; | ||
| using BrowserInfo = Microsoft.CmdPal.Ext.WebSearch.Helpers.DefaultBrowserInfo; | ||
|
|
||
| namespace Microsoft.CmdPal.Ext.WebSearch; | ||
|
|
||
| internal sealed partial class FallbackOpenURLItem : FallbackCommandItem | ||
| { | ||
| private readonly OpenURLCommand _executeItem; | ||
| private static readonly CompositeFormat PluginOpenURL = System.Text.CompositeFormat.Parse(Properties.Resources.plugin_open_url); | ||
| private static readonly CompositeFormat PluginOpenUrlInBrowser = System.Text.CompositeFormat.Parse(Properties.Resources.plugin_open_url_in_browser); | ||
|
|
||
| public FallbackOpenURLItem(SettingsManager settings) | ||
| : base(new OpenURLCommand(string.Empty, settings), string.Empty) | ||
| { | ||
| _executeItem = (OpenURLCommand)this.Command!; | ||
| Title = string.Empty; | ||
| _executeItem.Name = string.Empty; | ||
| Subtitle = string.Empty; | ||
| Icon = IconHelpers.FromRelativePath("Assets\\WebSearch.png"); | ||
| } | ||
|
|
||
| public override void UpdateQuery(string query) | ||
| { | ||
| if (!IsValidUrl(query)) | ||
| { | ||
| Title = string.Empty; | ||
| Subtitle = string.Empty; | ||
| return; | ||
| } | ||
|
|
||
| var success = Uri.TryCreate(query, UriKind.Absolute, out var uri); | ||
|
|
||
| // if url not contain schema, add http:// by default. | ||
| if (!success) | ||
moooyo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| query = "https://" + query; | ||
| } | ||
|
|
||
| _executeItem.Url = query; | ||
| _executeItem.Name = string.IsNullOrEmpty(query) ? string.Empty : Properties.Resources.open_in_default_browser; | ||
|
|
||
| Title = string.Format(CultureInfo.CurrentCulture, PluginOpenURL, query); | ||
| Subtitle = string.Format(CultureInfo.CurrentCulture, PluginOpenUrlInBrowser, BrowserInfo.Name ?? BrowserInfo.MSEdgeName); | ||
| } | ||
|
|
||
| public static bool IsValidUrl(string url) | ||
| { | ||
| if (string.IsNullOrWhiteSpace(url)) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| if (!url.Contains('.', StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| // eg: 'com', 'org'. We don't think it's a valid url. | ||
| // This can simplify the logic of checking if the url is valid. | ||
| return false; | ||
| } | ||
|
|
||
| if (Uri.IsWellFormedUriString(url, UriKind.Absolute)) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| if (!url.StartsWith("http://", StringComparison.OrdinalIgnoreCase) && | ||
| !url.StartsWith("https://", StringComparison.OrdinalIgnoreCase) && | ||
| !url.StartsWith("ftp://", StringComparison.OrdinalIgnoreCase) && | ||
| !url.StartsWith("file://", StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| if (Uri.IsWellFormedUriString("https://" + url, UriKind.Absolute)) | ||
| { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
18 changes: 18 additions & 0 deletions
18
src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.WebSearch/Properties/Resources.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@niels9001 do we have a better icon we can use to differentiate between "search the web" and "open this website"? That might help clarify the difference between these two commands