EverythingPowerToys/Everything.cs

133 lines
5.1 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using Community.PowerToys.Run.Plugin.Everything.Properties;
using Wox.Plugin;
using static Community.PowerToys.Run.Plugin.Everything.Interop.NativeMethods;
2022-12-11 02:20:09 +08:00
2023-01-07 02:20:22 +08:00
namespace Community.PowerToys.Run.Plugin.Everything
{
2023-03-30 00:22:49 +08:00
internal sealed class Everything
2022-12-11 02:20:09 +08:00
{
internal Everything(Settings setting)
{
2024-05-25 21:11:41 +08:00
Everything_SetRequestFlags(Request.FULL_PATH_AND_FILE_NAME);
UpdateSettings(setting);
}
internal void UpdateSettings(Settings setting)
{
Everything_SetSort(setting.Sort);
2022-12-11 02:20:09 +08:00
Everything_SetMax(setting.Max);
Everything_SetMatchPath(setting.MatchPath);
Everything_SetRegex(setting.RegEx);
2022-12-11 02:20:09 +08:00
}
internal IEnumerable<Result> Query(string query, Settings setting)
{
2024-05-25 21:11:41 +08:00
if (setting.Log > LogLevel.None)
{
Debugger.Write($"\r\n\r\nNew Query: {query}\r\n" +
$"Sort {(int)setting.Sort}_{Everything_GetSort()} | " +
$"Max {setting.Max}_{Everything_GetMax()} | " +
$"Match Path {setting.MatchPath}_{Everything_GetMatchPath()} | " +
$"Regex {setting.RegEx}_{Everything_GetRegex()}");
}
2022-12-11 02:20:09 +08:00
string orgqry = query;
if (setting.EnvVar && orgqry.Contains('%'))
{
query = Environment.ExpandEnvironmentVariables(query).Replace(';', '|');
2024-05-25 21:11:41 +08:00
if (setting.Log > LogLevel.None)
Debugger.Write($"EnvVariable\r\n{query}");
}
2022-12-11 02:20:09 +08:00
if (orgqry.Contains(':'))
{
foreach (var kv in setting.Filters)
{
if (query.Contains(kv.Key, StringComparison.OrdinalIgnoreCase))
{
query = query.Replace(kv.Key, kv.Value);
2024-05-25 21:11:41 +08:00
if (setting.Log > LogLevel.None)
Debugger.Write($"Contains Filter: {kv.Key}\r\n{query}");
}
}
2022-12-11 02:20:09 +08:00
}
2024-04-18 17:04:25 +08:00
Everything_SetSearchW(query);
2022-12-11 02:20:09 +08:00
if (!Everything_QueryW(true))
{
2024-05-25 21:11:41 +08:00
if (setting.Log > LogLevel.None)
Debugger.Write("\r\nUnable to Query\r\n");
2022-12-11 02:20:09 +08:00
throw new Win32Exception("Unable to Query");
}
uint resultCount = Everything_GetNumResults();
2024-05-25 21:11:41 +08:00
if (setting.Log > LogLevel.None)
Debugger.Write($"Results: {resultCount}");
2022-12-11 02:20:09 +08:00
for (uint i = 0; i < resultCount; i++)
{
2024-05-25 21:11:41 +08:00
if (setting.Log > LogLevel.None)
Debugger.Write($"\r\n===== RESULT #{i} =====");
char[] buffer = new char[32767];
uint length = Everything_GetResultFullPathNameW(i, buffer, (uint)buffer.Length);
2024-04-18 17:04:25 +08:00
string fullPath = new(buffer, 0, (int)length);
2024-05-25 21:11:41 +08:00
if (setting.Log > LogLevel.None)
Debugger.Write($"{length} {(setting.Log == LogLevel.Verbose ? fullPath : string.Empty)}");
2022-12-11 02:20:09 +08:00
string name = Path.GetFileName(fullPath);
bool isFolder = Everything_IsFolderResult(i);
2024-05-25 21:11:41 +08:00
2022-12-11 02:20:09 +08:00
string path = isFolder ? fullPath : Path.GetDirectoryName(fullPath);
string ext = Path.GetExtension(fullPath.Replace(".lnk", string.Empty));
2024-05-25 21:11:41 +08:00
if (setting.Log > LogLevel.None)
Debugger.Write($"Folder: {isFolder}\r\nFile Path {(setting.Log == LogLevel.Verbose ? path : path.Length)}\r\nFile Name {(setting.Log == LogLevel.Verbose ? name : name.Length)}\r\nExt: {ext}");
2022-12-11 02:20:09 +08:00
var r = new Result()
{
Title = name,
ToolTipData = new ToolTipData(name, fullPath),
2022-12-11 02:20:09 +08:00
SubTitle = Resources.plugin_name + ": " + fullPath,
IcoPath = isFolder ? "Images/folder.png" : (setting.Preview ?
2024-04-18 17:04:25 +08:00
fullPath : "Images/file.png"),
2022-12-11 02:20:09 +08:00
ContextData = new SearchResult()
{
Path = fullPath,
Title = name,
File = !isFolder,
},
Action = e =>
{
using var process = new Process();
process.StartInfo.FileName = fullPath;
process.StartInfo.WorkingDirectory = path;
process.StartInfo.UseShellExecute = true;
try
{
process.Start();
2024-05-25 21:11:41 +08:00
_ = Everything_IncRunCountFromFileNameW(fullPath);
2022-12-11 02:20:09 +08:00
return true;
}
catch (Win32Exception)
{
return false;
}
},
QueryTextDisplay = setting.QueryText ? (isFolder ? path : name) : orgqry,
};
yield return r;
}
}
}
}