Merge branch 'dev' into highlight-how-results-matched

This commit is contained in:
SysC0mp
2019-12-11 16:50:17 +01:00
12 changed files with 239 additions and 185 deletions

View File

@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
@@ -22,7 +22,6 @@ namespace Wox.Plugin.Folder
{
_storage = new PluginJsonStorage<Settings>();
_settings = _storage.Load();
InitialDriverList();
}
public void Save()
@@ -38,55 +37,20 @@ namespace Wox.Plugin.Folder
public void Init(PluginInitContext context)
{
_context = context;
InitialDriverList();
}
public List<Result> Query(Query query)
{
var results = GetUserFolderResults(query);
string search = query.Search.ToLower();
List<FolderLink> userFolderLinks = _settings.FolderLinks.Where(
x => x.Nickname.StartsWith(search, StringComparison.OrdinalIgnoreCase)).ToList();
List<Result> results =
userFolderLinks.Select(
item => new Result()
{
Title = item.Nickname,
IcoPath = item.Path,
SubTitle = "Ctrl + Enter to open the directory",
TitleHighlightData = StringMatcher.FuzzySearch(item.Nickname, search).MatchData,
Action = c =>
{
if (c.SpecialKeyState.CtrlPressed)
{
try
{
Process.Start(item.Path);
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Could not start " + item.Path);
return false;
}
}
_context.API.ChangeQuery($"{query.ActionKeyword} {item.Path}{(item.Path.EndsWith("\\") ? "" : "\\")}");
return false;
},
ContextData = item,
}).ToList();
if (_driverNames != null && !_driverNames.Any(search.StartsWith))
return results;
//if (!input.EndsWith("\\"))
//{
// //"c:" means "the current directory on the C drive" whereas @"c:\" means "root of the C drive"
// input = input + "\\";
//}
results.AddRange(QueryInternal_Directory_Exists(query));
// todo temp hack for scores
// todo why was this hack here?
foreach (var result in results)
{
result.Score += 10;
@@ -94,12 +58,56 @@ namespace Wox.Plugin.Folder
return results;
}
private Result CreateFolderResult(string title, string path, Query query)
{
return new Result
{
Title = title,
IcoPath = path,
SubTitle = "Ctrl + Enter to open the directory",
TitleHighlightData = StringMatcher.FuzzySearch(query.Search, title).MatchData,
Action = c =>
{
if (c.SpecialKeyState.CtrlPressed)
{
try
{
Process.Start(path);
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Could not start " + path);
return false;
}
}
string changeTo = path.EndsWith("\\") ? path : path + "\\";
_context.API.ChangeQuery(string.IsNullOrEmpty(query.ActionKeyword) ?
changeTo :
query.ActionKeyword + " " + changeTo);
return false;
}
};
}
private List<Result> GetUserFolderResults(Query query)
{
string search = query.Search.ToLower();
var userFolderLinks = _settings.FolderLinks.Where(
x => x.Nickname.StartsWith(search, StringComparison.OrdinalIgnoreCase));
var results = userFolderLinks.Select(item =>
CreateFolderResult(item.Nickname, item.Path, query)).ToList();
return results;
}
private void InitialDriverList()
{
if (_driverNames == null)
{
_driverNames = new List<string>();
DriveInfo[] allDrives = DriveInfo.GetDrives();
var allDrives = DriveInfo.GetDrives();
foreach (DriveInfo driver in allDrives)
{
_driverNames.Add(driver.Name.ToLower().TrimEnd('\\'));
@@ -107,121 +115,135 @@ namespace Wox.Plugin.Folder
}
}
private static readonly char[] _specialSearchChars = new char[]
{
'?', '*', '>'
};
private List<Result> QueryInternal_Directory_Exists(Query query)
{
var search = query.Search.ToLower();
var search = query.Search;
var results = new List<Result>();
var hasSpecial = search.IndexOfAny(_specialSearchChars) >= 0;
string incompleteName = "";
if (!Directory.Exists(search + "\\"))
if (hasSpecial || !Directory.Exists(search + "\\"))
{
//if the last component of the path is incomplete,
//then make auto complete for it.
// if folder doesn't exist, we want to take the last part and use it afterwards to help the user
// find the right folder.
int index = search.LastIndexOf('\\');
if (index > 0 && index < (search.Length - 1))
{
incompleteName = search.Substring(index + 1);
incompleteName = incompleteName.ToLower();
incompleteName = search.Substring(index + 1).ToLower();
search = search.Substring(0, index + 1);
if (!Directory.Exists(search))
{
return results;
}
}
else
{
return results;
}
}
else
{
// folder exist, add \ at the end of doesn't exist
if (!search.EndsWith("\\"))
{
search += "\\";
}
}
string firstResult = "Open current directory";
results.Add(CreateOpenCurrentFolderResult(incompleteName, search));
var searchOption = SearchOption.TopDirectoryOnly;
incompleteName += "*";
// give the ability to search all folder when starting with >
if (incompleteName.StartsWith(">"))
{
searchOption = SearchOption.AllDirectories;
incompleteName = incompleteName.Substring(1);
}
string searchNickname = new FolderLink { Path = query.Search }.Nickname;
try
{
// search folder and add results
var directoryInfo = new DirectoryInfo(search);
var fileSystemInfos = directoryInfo.GetFileSystemInfos(incompleteName, searchOption);
foreach (var fileSystemInfo in fileSystemInfos)
{
if ((fileSystemInfo.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) continue;
var result =
fileSystemInfo is DirectoryInfo
? CreateFolderResult(fileSystemInfo.Name, fileSystemInfo.FullName, query)
: CreateFileResult(fileSystemInfo.FullName, query);
results.Add(result);
}
}
catch (Exception e)
{
if (e is UnauthorizedAccessException || e is ArgumentException)
{
results.Add(new Result { Title = e.Message, Score = 501 });
return results;
}
throw;
}
return results;
}
private static Result CreateFileResult(string filePath, Query query)
{
var result = new Result
{
Title = Path.GetFileName(filePath),
IcoPath = filePath,
TitleHighlightData = StringMatcher.FuzzySearch(query.Search, Path.GetFileName(filePath)).MatchData,
Action = c =>
{
try
{
Process.Start(filePath);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Could not start " + filePath);
}
return true;
}
};
return result;
}
private static Result CreateOpenCurrentFolderResult(string incompleteName, string search)
{
var firstResult = "Open current directory";
if (incompleteName.Length > 0)
firstResult = "Open " + search;
results.Add(new Result
var folderName = search.TrimEnd('\\').Split(new[] { Path.DirectorySeparatorChar }, StringSplitOptions.None).Last();
return new Result
{
Title = firstResult,
SubTitle = $"Use > to search files and subfolders within {folderName}, " +
$"* to search for file extensions in {folderName} or both >* to combine the search",
IcoPath = search,
Score = 10000,
Score = 500,
Action = c =>
{
Process.Start(search);
return true;
}
});
string searchNickname = new FolderLink { Path = query.Search }.Nickname;
//Add children directories
DirectoryInfo[] dirs = new DirectoryInfo(search).GetDirectories();
foreach (DirectoryInfo dir in dirs)
{
if ((dir.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) continue;
if (incompleteName.Length != 0 && !dir.Name.ToLower().StartsWith(incompleteName))
continue;
DirectoryInfo dirCopy = dir;
var result = new Result
{
Title = dir.Name,
IcoPath = dir.FullName,
SubTitle = "Ctrl + Enter to open the directory",
TitleHighlightData = StringMatcher.FuzzySearch(dir.Name, searchNickname).MatchData,
Action = c =>
{
if (c.SpecialKeyState.CtrlPressed)
{
try
{
Process.Start(dirCopy.FullName);
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Could not start " + dirCopy.FullName);
return false;
}
}
_context.API.ChangeQuery($"{query.ActionKeyword} {dirCopy.FullName}\\");
return false;
}
};
results.Add(result);
}
//Add children files
FileInfo[] files = new DirectoryInfo(search).GetFiles();
foreach (FileInfo file in files)
{
if ((file.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) continue;
if (incompleteName.Length != 0 && !file.Name.ToLower().StartsWith(incompleteName))
continue;
string filePath = file.FullName;
var result = new Result
{
Title = Path.GetFileName(filePath),
IcoPath = filePath,
TitleHighlightData = StringMatcher.FuzzySearch(file.Name, searchNickname).MatchData,
Action = c =>
{
try
{
Process.Start(filePath);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Could not start " + filePath);
}
return true;
}
};
results.Add(result);
}
return results;
};
}
public string GetTranslatedPluginTitle()

View File

@@ -24,7 +24,7 @@ namespace Wox.Plugin.Program
private static PluginInitContext _context;
private static BinaryStorage<Win32[]> _win32Storage;
private static BinaryStorage<UWP.Application[]> _uwpStorage;
private static BinaryStorage<UWP.Application[]> _uwpStorage;
private readonly PluginJsonStorage<Settings> _settingsStorage;
public Main()
@@ -41,7 +41,7 @@ namespace Wox.Plugin.Program
});
Log.Info($"|Wox.Plugin.Program.Main|Number of preload win32 programs <{_win32s.Length}>");
Log.Info($"|Wox.Plugin.Program.Main|Number of preload uwps <{_uwps.Length}>");
var a = Task.Run(() =>
{
if (IsStartupIndexProgramsRequired || !_win32s.Any())
@@ -68,19 +68,25 @@ namespace Wox.Plugin.Program
public List<Result> Query(Query query)
{
Win32[] win32;
UWP.Application[] uwps;
lock (IndexLock)
{
var results1 = _win32s.AsParallel()
.Where(p => p.Enabled)
.Select(p => p.Result(query.Search, _context.API));
var results2 = _uwps.AsParallel()
.Where(p => p.Enabled)
.Select(p => p.Result(query.Search, _context.API));
var result = results1.Concat(results2).Where(r => r.Score > 0).ToList();
return result;
{ // just take the reference inside the lock to eliminate query time issues.
win32 = _win32s;
uwps = _uwps;
}
var results1 = win32.AsParallel()
.Where(p => p.Enabled)
.Select(p => p.Result(query.Search, _context.API));
var results2 = uwps.AsParallel()
.Where(p => p.Enabled)
.Select(p => p.Result(query.Search, _context.API));
var result = results1.Concat(results2).Where(r => r != null && r.Score > 0).ToList();
return result;
}
public void Init(PluginInitContext context)
@@ -111,14 +117,14 @@ namespace Wox.Plugin.Program
public static void IndexPrograms()
{
var t1 = Task.Run(()=>IndexWin32Programs());
var t1 = Task.Run(() => IndexWin32Programs());
var t2 = Task.Run(()=>IndexUWPPrograms());
var t2 = Task.Run(() => IndexUWPPrograms());
Task.WaitAll(t1, t2);
_settings.LastIndexTime = DateTime.Today;
}
}
public Control CreateSettingPanel()
{
@@ -141,7 +147,7 @@ namespace Wox.Plugin.Program
var program = selectedResult.ContextData as IProgram;
if (program != null)
{
menuOptions = program.ContextMenus(_context.API);
menuOptions = program.ContextMenus(_context.API);
}
menuOptions.Add(
@@ -151,7 +157,7 @@ namespace Wox.Plugin.Program
Action = c =>
{
DisableProgram(program);
_context.API.ShowMsg(_context.API.GetTranslation("wox_plugin_program_disable_dlgtitle_success"),
_context.API.ShowMsg(_context.API.GetTranslation("wox_plugin_program_disable_dlgtitle_success"),
_context.API.GetTranslation("wox_plugin_program_disable_dlgtitle_success_message"));
return false;
},

View File

@@ -273,11 +273,17 @@ namespace Wox.Plugin.Program.Programs
public Result Result(string query, IPublicAPI api)
{
var score = Score(query);
if (score <= 0)
{ // no need to create result if score is 0
return null;
}
var result = new Result
{
SubTitle = Package.Location,
Icon = Logo,
Score = Score(query),
Score = score,
ContextData = this,
Action = e =>
{

View File

@@ -43,11 +43,17 @@ namespace Wox.Plugin.Program.Programs
public Result Result(string query, IPublicAPI api)
{
var score = Score(query);
if (score <= 0)
{ // no need to create result if this is zero
return null;
}
var result = new Result
{
SubTitle = FullPath,
IcoPath = IcoPath,
Score = Score(query),
Score = score,
ContextData = this,
Action = e =>
{
@@ -195,7 +201,7 @@ namespace Wox.Plugin.Program.Programs
catch (COMException e)
{
// C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\MiracastView.lnk always cause exception
ProgramLogger.LogException($"|Win32|LnkProgram|{path}"+
ProgramLogger.LogException($"|Win32|LnkProgram|{path}" +
"|Error caused likely due to trying to get the description of the program", e);
program.Valid = false;