Skip to content

[cmdpal] Support invoke command in Bookmarks extension #39059

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

Open
wants to merge 39 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
e5f9d4b
init
Apr 23, 2025
311b8ac
Change structure
Apr 23, 2025
e6abca9
Fix icon issues
Apr 23, 2025
9f8a238
Fix cmd issue
Apr 24, 2025
923e33a
Fix i18n issue
Apr 24, 2025
84727c8
Remove unused field.
Apr 24, 2025
038ace0
Folder not web need directoryPage and OpenInShellCommand
Apr 24, 2025
681d9a2
Add subtitle for shell command
Apr 24, 2025
3db59a3
Fix typo issue
Apr 24, 2025
d220051
fix
Apr 24, 2025
c39bda9
Add error msg as return
Apr 25, 2025
8d2b3f2
Merge main
Apr 27, 2025
df252db
Ok, fine. We can detect the bookmark type by ourself. No need to expl…
Apr 27, 2025
0efb642
Fix typo
Apr 27, 2025
a0827b0
Fix typo
Apr 27, 2025
d7dbe0d
Fix typo
Apr 27, 2025
dc69a83
Merge branch 'main' into yuleng/cmdpal/bookmarks
Apr 29, 2025
9b25427
merge main and add settings
Apr 29, 2025
962cb43
store
Apr 30, 2025
9ee9824
Merge branch 'main' into yuleng/cmdpal/bookmarks
May 6, 2025
ae0da4e
simplify the logic. Just call them directly.
May 6, 2025
242dff4
Remove setting page
May 6, 2025
b825f7a
Remove unused todo
May 6, 2025
48184d6
Remove unused resource
May 6, 2025
a51b222
Merge folder and file
May 7, 2025
0aeeebc
Fetch icon
May 7, 2025
dfea4c2
Remove unused string
May 7, 2025
4a2cb80
fix typo
May 7, 2025
b17d53c
Merge urlCommand into shellCommand
May 7, 2025
56c8b57
Add folder context menu back
May 7, 2025
425b8c2
update comments
May 7, 2025
44b677b
Merge branch 'main' into yuleng/cmdpal/bookmarks
May 7, 2025
897a71e
Remove unused code
May 8, 2025
7bbe1f6
Remove unused code
May 8, 2025
b631615
Fix bug
May 8, 2025
d5e01fc
merge main
Jun 5, 2025
5323dea
Remove unused code
Jun 5, 2025
553c7eb
Remove unused code
Jun 5, 2025
5164a1f
Improve robust
Jun 5, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System.Collections.Generic;
using System.Text.Json.Serialization;
using Microsoft.CmdPal.Ext.Bookmarks.Models;

namespace Microsoft.CmdPal.Ext.Bookmarks;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using Microsoft.CmdPal.Ext.Bookmarks.Models;

namespace Microsoft.CmdPal.Ext.Bookmarks;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using Microsoft.CmdPal.Ext.Bookmarks.Helpers;
using Microsoft.CmdPal.Ext.Bookmarks.Models;
using Microsoft.CmdPal.Ext.Bookmarks.Properties;
using Microsoft.CmdPal.Ext.Indexer;
using Microsoft.CommandPalette.Extensions;
using Microsoft.CommandPalette.Extensions.Toolkit;
using Microsoft.Diagnostics.Utilities;
using Windows.Foundation;

namespace Microsoft.CmdPal.Ext.Bookmarks;

Expand All @@ -22,10 +26,6 @@ public partial class BookmarksCommandProvider : CommandProvider

private Bookmarks? _bookmarks;

public static IconInfo DeleteIcon { get; private set; } = new("\uE74D"); // Delete

public static IconInfo EditIcon { get; private set; } = new("\uE70F"); // Edit

public BookmarksCommandProvider()
{
Id = "Bookmarks";
Expand Down Expand Up @@ -109,57 +109,23 @@ private void LoadBookmarksFromFile()

private CommandItem BookmarkToCommandItem(BookmarkData bookmark)
{
ICommand command = bookmark.IsPlaceholder ?
new BookmarkPlaceholderPage(bookmark) :
new UrlCommand(bookmark);

var listItem = new CommandItem(command) { Icon = command.Icon };

List<CommandContextItem> contextMenu = [];

// Add commands for folder types
if (command is UrlCommand urlCommand)
var deleteAction = () =>
{
if (urlCommand.Type == "folder")
if (_bookmarks != null)
{
contextMenu.Add(
new CommandContextItem(new DirectoryPage(urlCommand.Url)));
ExtensionHost.LogMessage($"Deleting bookmark ({bookmark.Name},{bookmark.Bookmark})");

contextMenu.Add(
new CommandContextItem(new OpenInTerminalCommand(urlCommand.Url)));
_bookmarks.Data.Remove(bookmark);
SaveAndUpdateCommands();
}

listItem.Subtitle = urlCommand.Url;
}

var edit = new AddBookmarkPage(bookmark) { Icon = EditIcon };
edit.AddedCommand += Edit_AddedCommand;
contextMenu.Add(new CommandContextItem(edit));

var delete = new CommandContextItem(
title: Resources.bookmarks_delete_title,
name: Resources.bookmarks_delete_name,
action: () =>
{
if (_bookmarks != null)
{
ExtensionHost.LogMessage($"Deleting bookmark ({bookmark.Name},{bookmark.Bookmark})");

_bookmarks.Data.Remove(bookmark);

SaveAndUpdateCommands();
}
},
result: CommandResult.KeepOpen())
{
IsCritical = true,
Icon = DeleteIcon,
};
contextMenu.Add(delete);

listItem.MoreCommands = contextMenu.ToArray();
if (bookmark.IsPlaceholder)
{
return CreatePlaceholderCommand(bookmark, Edit_AddedCommand, deleteAction);
}

return listItem;
return CreateShellCommand(bookmark, Edit_AddedCommand, deleteAction);
}

public override ICommandItem[] TopLevelCommands()
Expand All @@ -180,4 +146,64 @@ internal static string StateJsonPath()
// now, the state is just next to the exe
return System.IO.Path.Combine(directory, "bookmarks.json");
}

private static CommandItem CreatePlaceholderCommand(BookmarkData bookmark, TypedEventHandler<object, BookmarkData> addBookmarkFunc, Action deleteAction)
{
var command = new BookmarkPlaceholderPage(bookmark);
var listItem = new CommandItem(command) { Icon = command.Icon };
List<CommandContextItem> contextMenu = [];

var edit = new AddBookmarkPage(bookmark) { Icon = IconHelper.EditIcon };
edit.AddedCommand += addBookmarkFunc;
contextMenu.Add(new CommandContextItem(edit));
var delete = new CommandContextItem(
title: Resources.bookmarks_delete_title,
name: Resources.bookmarks_delete_name,
action: deleteAction,
result: CommandResult.KeepOpen())
{
IsCritical = true,
Icon = IconHelper.DeleteIcon,
};
contextMenu.Add(delete);
listItem.MoreCommands = contextMenu.ToArray();
return listItem;
}

private static CommandItem CreateShellCommand(BookmarkData bookmark, TypedEventHandler<object, BookmarkData> addBookmarkFunc, Action deleteAction)
{
var invokableCommand = new Command.ShellCommand(bookmark);
var listItem = new CommandItem(invokableCommand) { Icon = invokableCommand.Icon };

List<CommandContextItem> contextMenu = [];

if (bookmark.Type == BookmarkType.Folder)
{
contextMenu.Add(
new CommandContextItem(new DirectoryPage(bookmark.Bookmark)));
contextMenu.Add(
new CommandContextItem(new OpenInTerminalCommand(bookmark.Bookmark)));
}

listItem.Subtitle = invokableCommand.BookmarkData.Bookmark;

var edit = new AddBookmarkPage(bookmark) { Icon = IconHelper.EditIcon };
edit.AddedCommand += addBookmarkFunc;
contextMenu.Add(new CommandContextItem(edit));

var delete = new CommandContextItem(
title: Resources.bookmarks_delete_title,
name: Resources.bookmarks_delete_name,
action: deleteAction,
result: CommandResult.KeepOpen())
{
IsCritical = true,
Icon = IconHelper.DeleteIcon,
};
contextMenu.Add(delete);

listItem.MoreCommands = contextMenu.ToArray();

return listItem;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System;
using Microsoft.CmdPal.Ext.Bookmarks.Helpers;
using Microsoft.CmdPal.Ext.Bookmarks.Properties;
using Microsoft.CommandPalette.Extensions;
using Microsoft.CommandPalette.Extensions.Toolkit;
Expand All @@ -16,6 +17,7 @@ internal sealed partial class OpenInTerminalCommand : InvokableCommand
public OpenInTerminalCommand(string folder)
{
Name = Resources.bookmarks_open_in_terminal_name;
Icon = IconHelper.CommandIcon;
_folder = folder;
}

Expand All @@ -34,7 +36,7 @@ public override ICommandResult Invoke()
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Error launching Windows Terminal: {ex.Message}");
ExtensionHost.LogMessage(new LogMessage() { Message = $"Error launching Windows Terminal: {ex.Message}" });
}

return CommandResult.Dismiss();
Expand Down
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.Data;
using Microsoft.CmdPal.Ext.Bookmarks.Helpers;
using Microsoft.CmdPal.Ext.Bookmarks.Models;
using Microsoft.CmdPal.Ext.Bookmarks.Properties;
using Microsoft.CommandPalette.Extensions.Toolkit;

namespace Microsoft.CmdPal.Ext.Bookmarks.Command;

public sealed partial class ShellCommand : InvokableCommand
{
public BookmarkData BookmarkData { get; }

public ShellCommand(BookmarkData data)
{
BookmarkData = data;
Name = data.Name;
Icon = IconHelper.CreateIcon(data.Bookmark, data.Type, false);
}

public override CommandResult Invoke()
{
return ShellCommand.Invoke(BookmarkData.Type, BookmarkData.Bookmark);
}

public static CommandResult Invoke(BookmarkType bookmarkType, string bookmarkValue)
{
if (bookmarkType == BookmarkType.Web)
{
var uri = OpenInShellHelper.GetUri(bookmarkValue);
if (uri != null)
{
if (!OpenInShellHelper.OpenInShell(uri.ToString(), null, null, OpenInShellHelper.ShellRunAsType.None, false, out var errMsg))
{
ExtensionHost.LogMessage($"Failed to open {bookmarkValue} in shell. Ex: {errMsg}");
return CommandResult.ShowToast(new ToastArgs() { Message = Resources.bookmarks_command_invoke_failed_message });
}

return CommandResult.Dismiss();
}
else
{
return CommandResult.ShowToast(new ToastArgs() { Message = Resources.bookmarks_command_invoke_failed_message });
}
}

// if it's a file or folder bookmark, call them directly.
if (bookmarkType == BookmarkType.File || bookmarkType == BookmarkType.Folder)
{
if (!OpenInShellHelper.OpenInShell(bookmarkValue, null, null, OpenInShellHelper.ShellRunAsType.None, false, out var errMsg))
{
ExtensionHost.LogMessage($"Failed to open {bookmarkValue} in shell. Ex: {errMsg}");
return CommandResult.ShowToast(new ToastArgs() { Message = Resources.bookmarks_command_invoke_failed_message });
}

return CommandResult.Dismiss();
}

// We assume all command bookmarks will follow the same format.
// For example: "python test.py" or "pwsh test.ps1"
// So, we can split the command and get the first part as the command name.
var splittedBookmarkValue = bookmarkValue.Split(" ");
if (splittedBookmarkValue.Length <= 1)
{
// directly call. Because it maybe a command with no args. eg: haproxy.exe or cmd.exe
if (!OpenInShellHelper.OpenInShell(splittedBookmarkValue[0], null, null, OpenInShellHelper.ShellRunAsType.None, false, out var errMsg))
{
ExtensionHost.LogMessage($"Failed to open {bookmarkValue} in shell. Ex: {errMsg}");
return CommandResult.ShowToast(new ToastArgs() { Message = Resources.bookmarks_command_invoke_failed_message });
}
}

// args = without the first part and join with space
var args = splittedBookmarkValue[1..];

if (!OpenInShellHelper.OpenInShell(splittedBookmarkValue[0], string.Join(" ", args), null, OpenInShellHelper.ShellRunAsType.None, false, out var errorMessage))
{
ExtensionHost.LogMessage($"Failed to open {bookmarkValue} in shell. Ex: {errorMessage}");
return CommandResult.ShowToast(new ToastArgs() { Message = Resources.bookmarks_command_invoke_failed_message });
}

return CommandResult.Dismiss();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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 Microsoft.CmdPal.Ext.Bookmarks.Models;

namespace Microsoft.CmdPal.Ext.Bookmarks.Helpers;

public static partial class BookmarkTypeHelper
{
/*
* Summary:
* If it's a valid uri, we assume it's a Web Link.
* Otherwise, we check if it's a existing folder or file.
* By default, we assume it's a command type.
*/

public static BookmarkType GetBookmarkTypeFromValue(string bookmark)
{
// judge if the bookmark is a url
if (Uri.TryCreate(bookmark, UriKind.Absolute, out var uriResult))
{
if (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps)
{
return BookmarkType.Web;
}
}

// judge if the bookmark is a existing folder
if (System.IO.Directory.Exists(bookmark))
{
return BookmarkType.Folder;
}

// ok, fine. Actually, it's also have the possibility to be a shell command.
// Such as 'test.cmd' or 'test.ps1'. Try to catch this case.
if (System.IO.File.Exists(bookmark))
{
return BookmarkType.File;
}

// by default. we assume it's a command type
return BookmarkType.Command;
}
}
Loading
Loading