Skip to content

Commit

Permalink
Merge pull request #30 from mantasjasikenas/dev
Browse files Browse the repository at this point in the history
Release v1.1.9
  • Loading branch information
mantasjasikenas authored Sep 16, 2024
2 parents 9e9852a + f5d8bdd commit ff6ac83
Show file tree
Hide file tree
Showing 19 changed files with 235 additions and 258 deletions.
11 changes: 8 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

## [1.1.8] - 2024-09-xx
## [1.1.9] - 2024-09-16

- QuickLook support to preview folders and files
- Improved code quality and performance
- Enchanted config command. Now shows icon of the application which will be used to open configuration files

## [1.1.8] - 2024-09-12

- Add autocomplete support for commands with arguments
- After typing the full shortcut, for example `q dev`, pressing `Ctr+Tab` (autocomplete) will replace current query with
the
shortcut path
the shortcut path
- Alias support for shortcuts. Add `Alias` field in the shortcut JSON file. Provide the alias name. Example:

```json
Expand Down
64 changes: 43 additions & 21 deletions Flow.Launcher.Plugin.ShortcutPlugin/ContextMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Flow.Launcher.Plugin.ShortcutPlugin.Extensions;
using Flow.Launcher.Plugin.ShortcutPlugin.Models.Shortcuts;
using Flow.Launcher.Plugin.ShortcutPlugin.Services.Interfaces;
Expand Down Expand Up @@ -53,11 +54,14 @@ private void AddShortcutDetails(Result selectedResult, List<Result> contextMenu)
return;
}

contextMenu.Add(ResultExtensions.Result(
"Key",
shortcut.Key,
() => { _context.API.CopyToClipboard(shortcut.Key, showDefaultNotification: false); }
));
if (!string.IsNullOrWhiteSpace(shortcut.Key))
{
contextMenu.Add(ResultExtensions.Result(
"Key",
shortcut.Key,
() => { _context.API.CopyToClipboard(shortcut.Key, showDefaultNotification: false); }
));
}

if (shortcut.Alias is {Count: > 0})
{
Expand Down Expand Up @@ -121,6 +125,7 @@ private void GetFileShortcutContextMenu(ICollection<Result> contextMenu, FileSho

contextMenu.Add(ResultExtensions.Result(
"Open containing folder",
Path.GetDirectoryName(filePath),
action: () =>
{
var path = Path.GetDirectoryName(filePath);
Expand All @@ -142,6 +147,7 @@ private void GetFileShortcutContextMenu(ICollection<Result> contextMenu, FileSho

contextMenu.Add(ResultExtensions.Result(
"Copy path",
filePath,
action: () => { _context.API.CopyToClipboard(filePath, showDefaultNotification: false); },
iconPath: Icons.Copy
));
Expand All @@ -151,6 +157,22 @@ private void GetFileShortcutContextMenu(ICollection<Result> contextMenu, FileSho
action: () => { _context.API.CopyToClipboard(filePath, true, false); },
iconPath: Icons.Copy
));

var codeEditors = GetCodeEditors();

foreach (var (title, cmd, icon) in codeEditors)
{
contextMenu.Add(ResultExtensions.Result(
$"Open in {title}",
action: () =>
{
CliWrap.Cli.Wrap(cmd)
.WithArguments(filePath)
.ExecuteAsync();
},
iconPath: icon
));
}
}

private void GetDirectoryContextMenu(ICollection<Result> contextMenu, DirectoryShortcut directoryShortcut)
Expand All @@ -172,9 +194,9 @@ private void GetDirectoryContextMenu(ICollection<Result> contextMenu, DirectoryS
iconPath: Icons.FolderOpen
));

var visualCodeVersions = GetVisualCodeVersions();
var codeEditors = GetCodeEditors();

foreach (var (title, cmd, icon) in visualCodeVersions)
foreach (var (title, cmd, icon) in codeEditors)
{
contextMenu.Add(ResultExtensions.Result(
$"Open in {title}",
Expand Down Expand Up @@ -261,7 +283,7 @@ private void GetDirectoryContextMenu(ICollection<Result> contextMenu, DirectoryS
}
}

private static List<(string title, string executable, string icon)> GetVisualCodeVersions()
private static List<(string title, string executable, string icon)> GetCodeEditors()
{
var path = Environment.GetEnvironmentVariable("PATH");
var versions = new List<(string, string, string)>();
Expand All @@ -271,20 +293,20 @@ private void GetDirectoryContextMenu(ICollection<Result> contextMenu, DirectoryS
return versions;
}

var folders = path.Split(';');

foreach (var folder in folders)
var editors = new Dictionary<string, (string title, string executable, string icon)>
{
if (File.Exists(Path.Combine(folder, "code.cmd")))
{
versions.Add(("Visual Studio Code", "code", Icons.VisualCode));
}

if (File.Exists(Path.Combine(folder, "code-insiders.cmd")))
{
versions.Add(("Visual Studio Code - Insiders", "code-insiders", Icons.VisualCodeInsiders));
}
}
{"code.cmd", ("Visual Studio Code", "code", Icons.VisualCode)},
{"code-insiders.cmd", ("Visual Studio Code - Insiders", "code-insiders", Icons.VisualCodeInsiders)},
{"zed.exe", ("Zed", "zed", Icons.Zed)}
};

versions.AddRange(
path
.Split(';')
.SelectMany(_ => editors, (folder, editor) => new {folder, editor})
.Where(t => File.Exists(Path.Combine(t.folder, t.editor.Key)))
.Select(t => t.editor.Value)
);

return versions;
}
Expand Down
14 changes: 12 additions & 2 deletions Flow.Launcher.Plugin.ShortcutPlugin/Extensions/ResultExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ namespace Flow.Launcher.Plugin.ShortcutPlugin.Extensions;

public static class ResultExtensions
{
public static List<Result> ToList(this Result result)
{
return new List<Result> {result};
}

public static List<Result> EmptyResult()
{
return SingleResult(Resources.Shortcuts_Query_No_results_found);
Expand Down Expand Up @@ -53,7 +58,8 @@ public static Result Result(
object contextData = default,
string autoCompleteText = null,
IList<int> titleHighlightData = default,
int score = default
int score = default,
string previewFilePath = null
)
{
return new Result
Expand All @@ -69,7 +75,11 @@ public static Result Result(
action?.Invoke();
return hideAfterAction;
},
AutoCompleteText = autoCompleteText ?? title
AutoCompleteText = autoCompleteText ?? title,
Preview = new Result.PreviewInfo
{
FilePath = previewFilePath
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<PackageTags>flow-launcher flow-plugin</PackageTags>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<GenerateDocumentationFile>false</GenerateDocumentationFile>
<LangVersion>11</LangVersion>
<LangVersion>latest</LangVersion>
<UseWpf>true</UseWpf>
</PropertyGroup>

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.IO;
using CliWrap;
using Flow.Launcher.Plugin.ShortcutPlugin.Extensions;
using Flow.Launcher.Plugin.ShortcutPlugin.Models.Shortcuts;
using Flow.Launcher.Plugin.ShortcutPlugin.Services.Interfaces;

namespace Flow.Launcher.Plugin.ShortcutPlugin.Models.Commands;
Expand Down Expand Up @@ -37,25 +38,30 @@ private List<Result> ConfigCommandHandler(ActionContext context, List<string> ar

return new List<Result>
{
ResultExtensions.Result("Open shortcuts config", shortcutsPath, () =>
{
if (!File.Exists(shortcutsPath))
{
CreateConfigFile(shortcutsPath);
}
CreateConfigResult("Open shortcuts config", shortcutsPath),
CreateConfigResult("Open variables config", variablesPath)
};
}

OpenConfig(shortcutsPath);
}),
ResultExtensions.Result("Open variables config", variablesPath, () =>
private Result CreateConfigResult(string title, string path)
{
return ResultExtensions.Result(title, path, () =>
{
if (!File.Exists(variablesPath))
if (!File.Exists(path))
{
CreateConfigFile(variablesPath);
CreateConfigFile(path);
}
OpenConfig(variablesPath);
})
};
OpenConfig(path);
},
iconPath: File.Exists(path) ? path : null,
autoCompleteText: path,
previewFilePath: path,
contextData: new FileShortcut
{
Path = path
}
);
}

private static void OpenConfig(string path)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,24 @@ private List<Result> HelpCommandHandler(ActionContext context, List<string> argu
"Open the plugin's documentation",
Constants.ReadmeUrl,
() => { ShortcutUtilities.OpenUrl(Constants.ReadmeUrl); },
iconPath: Icons.Info
iconPath: Icons.Info,
autoCompleteText: Constants.ReadmeUrl
);

var reportIssueResult = ResultExtensions.Result(
"Report an issue on GitHub",
Constants.GithubIssues,
() => { ShortcutUtilities.OpenUrl(Constants.GithubIssues); },
iconPath: Icons.Github
iconPath: Icons.Github,
autoCompleteText: Constants.GithubIssues
);

var discordResult = ResultExtensions.Result(
"Contact the developer on Discord",
"Username: " + Constants.DiscordUsername,
() => { _context.API.CopyToClipboard(Constants.DiscordUsername); },
iconPath: Icons.Discord
iconPath: Icons.Discord,
autoCompleteText: Constants.DiscordUsername
);

return new List<Result> {readmeResult, reportIssueResult, discordResult};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Linq;
using Flow.Launcher.Plugin.ShortcutPlugin.models;
using Flow.Launcher.Plugin.ShortcutPlugin.Services.Interfaces;

Expand Down Expand Up @@ -25,7 +26,7 @@ private Command CreateListCommand()
.WithResponseFailure(("Failed to show all shortcuts", "Something went wrong"))
.WithResponseSuccess(("List", "List all shortcuts"))
.WithMultipleValuesForSingleArgument()
.WithHandler((_, arguments) => _shortcutsService.GetShortcuts(arguments))
.WithHandler((_, arguments) => _shortcutsService.GetShortcuts(arguments.Skip(1).ToList()))
.Build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private Command CreateReloadCommand()

private List<Result> ReloadCommandHandler(ActionContext context, List<string> arguments)
{
return ResultExtensions.SingleResult("Reload plugin data", "", () =>
return ResultExtensions.SingleResult("Reload plugin data", "This action will reload all plugin data", () =>
{
_settingsService.Reload();
_shortcutsService.Reload();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,14 @@ public string GetDerivedType()

public string GetTitle()
{
return $"{Key}{GetAlias()}";
var title = $"{Key}{GetAlias()}";

return string.IsNullOrWhiteSpace(title) ? GetDerivedType() : title;
}

public string GetSubTitle()
{
return ToString();
}

private string GetAlias()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ public List<Result> ResolveCommand(List<string> arguments, Query query)
var argsWithoutKey = arguments.Skip(1).ToList();

// In case this is a shortcut command, let's open shortcut
if (_shortcutsRepository.GetShortcuts(key) is not null)
if (_shortcutsRepository.TryGetShortcuts(key, out var shortcuts))
{
return _shortcutsService.OpenShortcuts(key, argsWithoutKey);
return _shortcutsService.OpenShortcuts(shortcuts, argsWithoutKey, true);
}

// If command was not found
Expand All @@ -60,7 +60,7 @@ public List<Result> ResolveCommand(List<string> arguments, Query query)
// Show possible shortcuts
var possibleShortcuts = _shortcutsRepository
.GetPossibleShortcuts(key)
.SelectMany(s => _shortcutsService.OpenShortcut(s, argsWithoutKey));
.SelectMany(s => _shortcutsService.OpenShortcut(s, argsWithoutKey, false));

// Return possible command matches
var possibleCommands = GetPossibleCommands(key, query.ActionKeyword);
Expand Down Expand Up @@ -107,10 +107,11 @@ public List<Result> ResolveCommand(List<string> arguments, Query query)
.Arguments.Cast<Argument>()
.Select(a =>
ResultExtensions.Result(
a.ResponseInfo.Item1,
a.ResponseInfo.Item2,
title: a.ResponseInfo.Item1,
subtitle: a.ResponseInfo.Item2,
() => { _context.API.ChangeQuery($"{query.ActionKeyword} {a.ResponseInfo.Item1}"); },
hideAfterAction: false
hideAfterAction: false,
autoCompleteText: $"{query.ActionKeyword} {a.ResponseInfo.Item1}"
)
)
.ToList();
Expand All @@ -122,34 +123,34 @@ public List<Result> ResolveCommand(List<string> arguments, Query query)
private List<Result> ShowAvailableCommands(string actionKeyword)
{
return _commands
.Values.Select(c => new Result
{
Title = c.ResponseInfo.Item1 + " ", // FIXME: Wrong order without space
SubTitle = c.ResponseInfo.Item2,
IcoPath = Icons.Logo,
Score = 1000 - _commands.Count,
Action = _ =>
{
_context.API.ChangeQuery($"{actionKeyword} {c.Key}");
return false;
}
})
.Values.Select(c =>
ResultExtensions.Result(
title: c.ResponseInfo.Item1,
subtitle: c.ResponseInfo.Item2,
score: 1000 - _commands.Count,
hideAfterAction: false,
action: () => { _context.API.ChangeQuery($"{actionKeyword} {c.Key}"); },
autoCompleteText: $"{actionKeyword} {c.Key}"
)
)
.ToList();
}

private IEnumerable<Result> GetPossibleCommands(string query, string actionKeyword)
{
return _commands
.Values.Where(c =>
.Values
.Where(c =>
c.Key.StartsWith(query, StringComparison.InvariantCultureIgnoreCase)
)
.Select(c =>
ResultExtensions.Result(
c.ResponseInfo.Item1,
c.ResponseInfo.Item2,
title: c.ResponseInfo.Item1,
subtitle: c.ResponseInfo.Item2,
score: 1000,
hideAfterAction: false,
action: () => { _context.API.ChangeQuery($"{actionKeyword} {c.Key}"); }
action: () => { _context.API.ChangeQuery($"{actionKeyword} {c.Key}"); },
autoCompleteText: $"{actionKeyword} {c.Key}"
)
)
.ToList();
Expand Down
Loading

0 comments on commit ff6ac83

Please sign in to comment.