Skip to content

Commit

Permalink
Update version and add shortcut name highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
mantasjasikenas committed Feb 4, 2024
1 parent e3213c3 commit 4567aa1
Show file tree
Hide file tree
Showing 6 changed files with 278 additions and 156 deletions.
15 changes: 14 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file.

## [1.1.4] - 2024-02-04

- Shorcut name highlighting in the search results
- Ability to set a specific application to open file or URL shortcuts. Add "App" field in the shortcut JSON file. Provide application name (works only for registered applications in the system) or full path to the executable file. If the application is not defined, the default application will be used to open the file or URL. Example:

```json
Expand All @@ -16,7 +17,19 @@ All notable changes to this project will be documented in this file.
"Url": "https://www.youtube.com/playlist?list=WL",
"App": "msedge.exe",
"Key": "wl2"
}
},
{
"Type": "File",
"App": "C:\\Program Files\\WindowsApps\\Microsoft.WindowsNotepad_11.2312.18.0_x64__8wekyb3d8bbwe\\Notepad\\Notepad.exe",
"Path": "C:\\Users\\tutta\\Storage\\Motion Picture\\labas.txt",
"Key": "t1"
},
{
"Type": "File",
"App": "notepad",
"Path": "C:\\Users\\tutta\\Storage\\Motion Picture\\labas.txt",
"Key": "t2"
},
```

## [1.1.3] - 2024-01-31
Expand Down
31 changes: 25 additions & 6 deletions Flow.Launcher.Plugin.ShortcutPlugin/Extensions/ResultExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using Flow.Launcher.Plugin.ShortcutPlugin.Utilities;
using FuzzySharp.SimilarityRatio.Scorer;

namespace Flow.Launcher.Plugin.ShortcutPlugin.Extensions;

Expand All @@ -26,8 +27,15 @@ public static List<Result> EmptyResult(string title, string subtitle = "")
return SingleResult(title, subtitle);
}

public static List<Result> SingleResult(string title, string subtitle = "", Action action = default,
bool hideAfterAction = true, string autocomplete = default, string iconPath = default)
public static List<Result> SingleResult(
string title,
string subtitle = "",
Action action = default,
bool hideAfterAction = true,
string autocomplete = default,
string iconPath = default,
IList<int> titleHighlightData = default
)
{
return new List<Result>
{
Expand All @@ -37,6 +45,7 @@ public static List<Result> SingleResult(string title, string subtitle = "", Acti
SubTitle = subtitle,
IcoPath = iconPath ?? Icons.Logo,
AutoCompleteText = autocomplete,
TitleHighlightData = titleHighlightData,
Action = _ =>
{
action?.Invoke();
Expand All @@ -46,16 +55,26 @@ public static List<Result> SingleResult(string title, string subtitle = "", Acti
};
}

public static Result Result(string title, string subtitle = "", Action action = default,
bool hideAfterAction = true, string iconPath = default, object contextData = default,
string autoCompleteText = null)
public static Result Result(
string title,
string subtitle = "",
Action action = default,
bool hideAfterAction = true,
string iconPath = default,
object contextData = default,
string autoCompleteText = null,
IList<int> titleHighlightData = default,
int score = default
)
{
return new Result
{
Title = title,
SubTitle = subtitle,
IcoPath = iconPath ?? Icons.Logo,
TitleHighlightData = titleHighlightData,
ContextData = contextData,
Score = score,
Action = _ =>
{
action?.Invoke();
Expand All @@ -64,4 +83,4 @@ public static Result Result(string title, string subtitle = "", Action action =
AutoCompleteText = autoCompleteText ?? title
};
}
}
}
129 changes: 75 additions & 54 deletions Flow.Launcher.Plugin.ShortcutPlugin/Repositories/CommandsRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ namespace Flow.Launcher.Plugin.ShortcutPlugin.Repositories;

public class CommandsRepository : ICommandsRepository
{
private readonly Dictionary<string, Command> _commands = new(StringComparer.InvariantCultureIgnoreCase);
private readonly Dictionary<string, Command> _commands =
new(StringComparer.InvariantCultureIgnoreCase);
private readonly PluginInitContext _context;
private readonly IShortcutsRepository _shortcutsRepository;
private readonly IShortcutsService _shortcutsService;


public CommandsRepository(IEnumerable<ICommand> commands, PluginInitContext context, IShortcutsRepository shortcutsRepository,
IShortcutsService shortcutsService)
public CommandsRepository(
IEnumerable<ICommand> commands,
PluginInitContext context,
IShortcutsRepository shortcutsRepository,
IShortcutsService shortcutsService
)
{
_context = context;
_shortcutsRepository = shortcutsRepository;
Expand All @@ -32,10 +36,7 @@ public CommandsRepository(IEnumerable<ICommand> commands, PluginInitContext cont

private void RegisterCommands(IEnumerable<ICommand> commands)
{
commands
.Select(c => c.Create())
.ToList()
.ForEach(c => _commands.Add(c.Key, c));
commands.Select(c => c.Create()).ToList().ForEach(c => _commands.Add(c.Key, c));
}

public List<Result> ResolveCommand(List<string> arguments, string query)
Expand All @@ -55,30 +56,32 @@ public List<Result> ResolveCommand(List<string> arguments, string query)
if (!_commands.TryGetValue(arguments[0], out var command))
{
// Show possible shortcuts
var possibleShortcuts = _shortcutsRepository.GetShortcuts()
.Where(s => Fuzz.PartialRatio(s.Key, arguments[0]) > 90)
.Select(s => s is GroupShortcut
? ResultExtensions.Result(s.Key, s.Key)
: _shortcutsService.OpenShortcut(s.Key,
arguments.Skip(1).ToList())
.First())
.ToList();
var possibleShortcuts = _shortcutsRepository
.GetShortcuts()
.Where(s => Fuzz.PartialRatio(s.Key, arguments[0]) > 90)
.Select(s =>
_shortcutsService.OpenShortcut(s.Key, arguments.Skip(1).ToList()).First()
)
.ToList();

// Return possible command matches
var possibleCommands = _commands.Values
.Where(c => c.Key.StartsWith(arguments[0],
StringComparison.InvariantCultureIgnoreCase))
.Select(c =>
ResultExtensions.Result(c.ResponseInfo.Item1, c.ResponseInfo.Item2))
.ToList();
var possibleCommands = _commands
.Values.Where(c =>
c.Key.StartsWith(arguments[0], StringComparison.InvariantCultureIgnoreCase)
)
.Select(c =>
ResultExtensions.Result(c.ResponseInfo.Item1, c.ResponseInfo.Item2, score: 1000)
)
.ToList();

var possibleResults = possibleShortcuts
.Concat(possibleCommands)
.ToList();
var possibleResults = possibleShortcuts.Concat(possibleCommands).ToList();

return possibleResults.Count != 0
? possibleResults
: ResultExtensions.SingleResult("Invalid input", "Please provide valid command or shortcut");
: ResultExtensions.SingleResult(
"Invalid input",
"Please provide valid command or shortcut"
);

// If user has already written some arguments and the first argument is invalid
}
Expand All @@ -90,7 +93,10 @@ public List<Result> ResolveCommand(List<string> arguments, string query)
// More arguments than needed and ...
if (level < arguments.Count - 1 && !executor.AllowsMultipleValuesForSingleArgument)
{
return ResultExtensions.SingleResult("Invalid command arguments", "Please provide valid command arguments");
return ResultExtensions.SingleResult(
"Invalid command arguments",
"Please provide valid command arguments"
);
}

// If command is valid and has a handler
Expand All @@ -99,7 +105,6 @@ public List<Result> ResolveCommand(List<string> arguments, string query)
return Map(executor, executor.ResponseSuccess, arguments);
}


// If command has more arguments
if (executor.Arguments.Count != 0)
{
Expand All @@ -109,39 +114,54 @@ public List<Result> ResolveCommand(List<string> arguments, string query)
return ResultExtensions.SingleResult(executor.ResponseInfo.Item1, executor.ResponseInfo.Item2);
} */

return executor.Arguments
.Cast<Argument>()
.Select(a =>
ResultExtensions.Result(a.ResponseInfo.Item1, a.ResponseInfo.Item2,
() => { _context.API.ChangeQuery($"{a.Key} "); }))
.ToList();
return executor
.Arguments.Cast<Argument>()
.Select(a =>
ResultExtensions.Result(
a.ResponseInfo.Item1,
a.ResponseInfo.Item2,
() =>
{
_context.API.ChangeQuery($"{a.Key} ");
}
)
)
.ToList();
}


return Map(executor, executor.ResponseFailure, arguments);
}

private List<Result> ShowAvailableCommands()
{
return _commands.Values
.Select(c => new Result
{
Title = c.ResponseInfo.Item1,
SubTitle = c.ResponseInfo.Item2,
IcoPath = Icons.Logo
})
.ToList();
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
})
.ToList();
}

private static List<Result> Map(IQueryExecutor executor, (string, string)? response, List<string> arguments)
private static List<Result> Map(
IQueryExecutor executor,
(string, string)? response,
List<string> arguments
)
{
return executor.Handler is null
? ResultExtensions.SingleResult(response?.Item1, response?.Item2)
: executor.Handler.Invoke(null, arguments);
}

private static (IQueryExecutor, IQueryExecutor) GetExecutors(IQueryExecutor executorOld, IQueryExecutor executorNew,
IReadOnlyList<string> arguments, ref int level)
private static (IQueryExecutor, IQueryExecutor) GetExecutors(
IQueryExecutor executorOld,
IQueryExecutor executorNew,
IReadOnlyList<string> arguments,
ref int level
)
{
while (true)
{
Expand All @@ -163,9 +183,11 @@ private static (IQueryExecutor, IQueryExecutor) GetExecutors(IQueryExecutor exec
}

[CanBeNull]
private static IQueryExecutor GetExecutorFromArguments(IReadOnlyCollection<IQueryExecutor> executors,
private static IQueryExecutor GetExecutorFromArguments(
IReadOnlyCollection<IQueryExecutor> executors,
IReadOnlyList<string> arguments,
int level)
int level
)
{
if (executors.Count == 0)
{
Expand All @@ -174,14 +196,13 @@ private static IQueryExecutor GetExecutorFromArguments(IReadOnlyCollection<IQuer

var argument = arguments[level];

var argumentsLiteral = executors
.OfType<ArgumentLiteral>()
.ToList();
var argumentsLiteral = executors.OfType<ArgumentLiteral>().ToList();

var argExecutor =
argumentsLiteral.FirstOrDefault(a => a.Key.Equals(argument, StringComparison.InvariantCultureIgnoreCase))
?? executors.Except(argumentsLiteral).FirstOrDefault();
argumentsLiteral.FirstOrDefault(a =>
a.Key.Equals(argument, StringComparison.InvariantCultureIgnoreCase)
) ?? executors.Except(argumentsLiteral).FirstOrDefault();

return argExecutor;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public class CommandsService : ICommandsService
private readonly IVariablesService _variablesService;
private readonly ICommandsRepository _commandsRepository;


public CommandsService(
IShortcutsService shortcutsService,
ISettingsService settingsService,
Expand All @@ -37,7 +36,6 @@ public List<Result> ResolveCommand(List<string> arguments, Query query)
: $"{query.ActionKeyword} {result.AutoCompleteText}";
});


return results;
}

Expand All @@ -47,4 +45,4 @@ public void ReloadPluginData()
_shortcutsService.Reload();
_variablesService.Reload();
}
}
}
Loading

0 comments on commit 4567aa1

Please sign in to comment.