From 5e9b035c769f3365bf9c5b04fd7d408395f66f9c Mon Sep 17 00:00:00 2001 From: "Lin Yu-Chieh (Victor)" Date: Wed, 18 May 2022 15:19:39 +0800 Subject: [PATCH] Added filter macros, adjustments for StyleCop..etc Resolves #14 --- ...ity.PowerToys.Run.Plugin.Everything.csproj | 27 ++--- Community.PowerToys.Run.Plugin.Everything.xml | 99 +++++++++++++++++++ ContextMenuLoader.cs | 16 +-- GlobalSuppressions.cs | 14 +++ Main.cs | 46 ++++----- NativeMethods.cs | 59 ++++++----- plugin.json | 2 +- settings.toml | 9 ++ 8 files changed, 196 insertions(+), 76 deletions(-) create mode 100644 Community.PowerToys.Run.Plugin.Everything.xml create mode 100644 GlobalSuppressions.cs diff --git a/Community.PowerToys.Run.Plugin.Everything.csproj b/Community.PowerToys.Run.Plugin.Everything.csproj index abd9576..a3da138 100644 --- a/Community.PowerToys.Run.Plugin.Everything.csproj +++ b/Community.PowerToys.Run.Plugin.Everything.csproj @@ -1,5 +1,5 @@  - + net6.0-windows @@ -17,6 +17,8 @@ https://github.com/lin-ycv/EverythingPowerToys README.md Everything.light.png + True + $(NoWarn),1573,1591,1712 @@ -46,23 +48,6 @@ true - - - GlobalSuppressions.cs - - - StyleCop.json - - - - - - false - False - False - - - PreserveNewest @@ -82,6 +67,10 @@ + + + + True @@ -131,7 +120,7 @@ - + diff --git a/Community.PowerToys.Run.Plugin.Everything.xml b/Community.PowerToys.Run.Plugin.Everything.xml new file mode 100644 index 0000000..1cfd981 --- /dev/null +++ b/Community.PowerToys.Run.Plugin.Everything.xml @@ -0,0 +1,99 @@ + + + + Community.PowerToys.Run.Plugin.Everything + + + + + A strongly-typed resource class, for looking up localized strings, etc. + + + + + Returns the cached ResourceManager instance used by this class. + + + + + Overrides the current thread's CurrentUICulture property for all + resource lookups using this strongly typed resource class. + + + + + Looks up a localized string similar to Fail to set text in clipboard. + + + + + Looks up a localized string similar to Copy path (Ctrl+C). + + + + + Looks up a localized string similar to Enable Wait setting under PowerToys Run > Everything. + + + + + Looks up a localized string similar to Install Everything if not installed. + + + + + Looks up a localized string similar to Everything is not running. + + + + + Looks up a localized string similar to Fail to open folder at. + + + + + Looks up a localized string similar to Open containing folder (Ctrl+Shift+E). + + + + + Looks up a localized string similar to Open path in console (Ctrl+Shift+C). + + + + + Looks up a localized string similar to Get search results from Everything. + + + + + Looks up a localized string similar to Everything. + + + + + Looks up a localized string similar to Preview - Preview file content as icon, may cause freezing if file is not local.. + + + + + Looks up a localized string similar to Run as administrator (Ctrl+Shift+Enter). + + + + + Looks up a localized string similar to Timed out before finishing the query. + + + + + Looks up a localized string similar to Top - Insert result at the top of the list, may cause pre-selection issue.. + + + + + Looks up a localized string similar to Wait - Wait longer for the query to finish, enable only if prompted to.. + + + + diff --git a/ContextMenuLoader.cs b/ContextMenuLoader.cs index f94a705..cbb86b3 100644 --- a/ContextMenuLoader.cs +++ b/ContextMenuLoader.cs @@ -19,14 +19,14 @@ namespace Community.PowerToys.Run.Plugin.Everything { internal class ContextMenuLoader : IContextMenu { - private readonly PluginInitContext _context; + private readonly PluginInitContext context; // Extensions for adding run as admin context menu item for applications private readonly string[] appExtensions = { ".exe", ".bat", ".appref-ms", ".lnk" }; public ContextMenuLoader(PluginInitContext context) { - _context = context; + this.context = context; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "We want to keep the process alive, and instead log and show an error message")] @@ -43,7 +43,7 @@ namespace Community.PowerToys.Run.Plugin.Everything } // Test to check if File can be Run as admin, if yes, we add a 'run as admin' context menu item - if (CanFileBeRunAsAdmin(record.Path)) + if (this.CanFileBeRunAsAdmin(record.Path)) { contextMenus.Add(CreateRunAsAdminContextMenu(record)); } @@ -67,9 +67,9 @@ namespace Community.PowerToys.Run.Plugin.Everything catch (Exception e) { var message = Properties.Resources.clipboard_failed; - Log.Exception(message, e, GetType()); + Log.Exception(message, e, this.GetType()); - _context.API.ShowMsg(message); + this.context.API.ShowMsg(message); return false; } }, @@ -100,7 +100,7 @@ namespace Community.PowerToys.Run.Plugin.Everything } catch (Exception e) { - Log.Exception($"Failed to open {record.Path} in console, {e.Message}", e, GetType()); + Log.Exception($"Failed to open {record.Path} in console, {e.Message}", e, this.GetType()); return false; } }, @@ -142,7 +142,7 @@ namespace Community.PowerToys.Run.Plugin.Everything private bool CanFileBeRunAsAdmin(string path) { string fileExtension = Path.GetExtension(path); - foreach (string extension in appExtensions) + foreach (string extension in this.appExtensions) { // Using OrdinalIgnoreCase since this is internal if (extension.Equals(fileExtension, StringComparison.OrdinalIgnoreCase)) @@ -155,7 +155,7 @@ namespace Community.PowerToys.Run.Plugin.Everything } [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "We want to keep the process alive, and instead log and show an error message")] - private ContextMenuResult CreateOpenContainingFolderResult(SearchResult record) + private static ContextMenuResult CreateOpenContainingFolderResult(SearchResult record) { return new ContextMenuResult { diff --git a/GlobalSuppressions.cs b/GlobalSuppressions.cs new file mode 100644 index 0000000..7b3ae10 --- /dev/null +++ b/GlobalSuppressions.cs @@ -0,0 +1,14 @@ +// This file is used by Code Analysis to maintain SuppressMessage +// attributes that are applied to this project. +// Project-level suppressions either have no target or are given +// a specific target and scoped to a namespace, type, member, etc. + +using System.Diagnostics.CodeAnalysis; + +[assembly: SuppressMessage("StyleCop.CSharp.OrderingRules", "SA1200:Using directives should be placed correctly", Justification = "don't care")] +[assembly: SuppressMessage("StyleCop.CSharp.OrderingRules", "SA1202:Elements should be ordered by access", Justification = "dont care")] +[assembly: SuppressMessage("StyleCop.CSharp.OrderingRules", "SA1204:Static elements should appear before instance elements", Justification = "don't care")] +[assembly: SuppressMessage("StyleCop.CSharp.LayoutRules", "SA1501:Statement should not be on a single line", Justification = "don't care")] +[assembly: SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1600:Elements should be documented", Justification = "don't care")] +[assembly: SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1602:Enumeration items should be documented", Justification = "don't care")] +[assembly: SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1633:File should have header", Justification = "don't care")] \ No newline at end of file diff --git a/Main.cs b/Main.cs index 4cf23b5..262ec15 100644 --- a/Main.cs +++ b/Main.cs @@ -28,8 +28,8 @@ namespace Community.PowerToys.Run.Plugin.Everything private const string Top = nameof(Top); private const string NoPreview = nameof(NoPreview); private readonly string reservedStringPattern = @"^[\/\\\$\%]+$|^.*[<>].*$"; - private bool _top; - private bool _preview; + private bool top; + private bool preview; public string Name => Resources.plugin_name; @@ -51,25 +51,25 @@ namespace Community.PowerToys.Run.Plugin.Everything }, }; - private IContextMenu _contextMenuLoader; - private PluginInitContext _context; + private IContextMenu contextMenuLoader; + private PluginInitContext context; private bool disposed; - private static string _warningIconPath; + private static string warningIconPath; internal static string WarningIcon { get { - return _warningIconPath; + return warningIconPath; } } public void Init(PluginInitContext context) { - _context = context; - _contextMenuLoader = new ContextMenuLoader(context); - _context.API.ThemeChanged += OnThemeChanged; - UpdateIconPath(_context.API.GetCurrentTheme()); + this.context = context; + this.contextMenuLoader = new ContextMenuLoader(context); + this.context.API.ThemeChanged += this.OnThemeChanged; + UpdateIconPath(this.context.API.GetCurrentTheme()); EverythingSetup(); } @@ -88,13 +88,13 @@ namespace Community.PowerToys.Run.Plugin.Everything { var searchQuery = query.Search; - var regexMatch = Regex.Match(searchQuery, reservedStringPattern); + var regexMatch = Regex.Match(searchQuery, this.reservedStringPattern); if (!regexMatch.Success) { try { - results.AddRange(EverythingSearch(searchQuery, _top, _preview)); + results.AddRange(EverythingSearch(searchQuery, this.top, this.preview)); } catch (OperationCanceledException) { @@ -102,7 +102,7 @@ namespace Community.PowerToys.Run.Plugin.Everything { Title = Resources.timeout, SubTitle = Resources.enable_wait, - IcoPath = _warningIconPath, + IcoPath = warningIconPath, Score = int.MaxValue, }); } @@ -112,14 +112,14 @@ namespace Community.PowerToys.Run.Plugin.Everything { Title = Resources.Everything_not_running, SubTitle = Resources.Everything_ini, - IcoPath = _warningIconPath, + IcoPath = warningIconPath, QueryTextDisplay = '.' + Resources.plugin_name, Score = int.MaxValue, }); } catch (Exception e) { - Log.Exception("Everything Exception", e, GetType()); + Log.Exception("Everything Exception", e, this.GetType()); } } } @@ -136,17 +136,17 @@ namespace Community.PowerToys.Run.Plugin.Everything { if (theme == Theme.Light || theme == Theme.HighContrastWhite) { - _warningIconPath = "Images/Warning.light.png"; + warningIconPath = "Images/Warning.light.png"; } else { - _warningIconPath = "Images/Warning.dark.png"; + warningIconPath = "Images/Warning.dark.png"; } } public List LoadContextMenus(Result selectedResult) { - return _contextMenuLoader.LoadContextMenus(selectedResult); + return this.contextMenuLoader.LoadContextMenus(selectedResult); } public Control CreateSettingPanel() @@ -164,26 +164,26 @@ namespace Community.PowerToys.Run.Plugin.Everything nopreview = settings.AdditionalOptions.FirstOrDefault(x => x.Key == NoPreview)?.Value ?? false; } - _top = top; - _preview = nopreview; + this.top = top; + this.preview = nopreview; } protected virtual void Dispose(bool disposing) { - if (!disposed) + if (!this.disposed) { if (disposing) { } - disposed = true; + this.disposed = true; } } public void Dispose() { // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method - Dispose(disposing: true); + this.Dispose(disposing: true); GC.SuppressFinalize(this); } } diff --git a/NativeMethods.cs b/NativeMethods.cs index 8a58d49..24e4540 100644 --- a/NativeMethods.cs +++ b/NativeMethods.cs @@ -1,8 +1,4 @@ -// 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; using System.Collections; using System.Collections.Generic; using System.ComponentModel; @@ -80,7 +76,7 @@ namespace Community.PowerToys.Run.Plugin.Everything [DllImport(dllName)] public static extern uint Everything_GetNumResults(); [DllImport(dllName, CharSet = CharSet.Unicode)] - public static extern void Everything_GetResultFullPathName(uint nIndex, StringBuilder lpString, uint nMaxCount); + public static extern void Everything_GetResultFullPathName(uint nIndex, char[] lpString, uint nMaxCount); [DllImport(dllName)] public static extern bool Everything_QueryW(bool bWait); [DllImport(dllName)] @@ -94,6 +90,7 @@ namespace Community.PowerToys.Run.Plugin.Everything private static uint max = 20; private static Sort sort = Sort.DATE_MODIFIED_DESCENDING; + private static Dictionary filters = new Dictionary(); #pragma warning disable SA1503 // Braces should not be omitted #if DEBUG private static StringBuilder log = new StringBuilder(); @@ -108,11 +105,8 @@ namespace Community.PowerToys.Run.Plugin.Everything Everything_SetRequestFlags(Request.FULL_PATH_AND_FILE_NAME); GetCustomSettings(); Everything_SetSort(sort); - Everything_SetMax(max); } - [SuppressMessage("StyleCop.CSharp.LayoutRules", "SA1503:Braces should not be omitted", Justification = "stop wasting lines")] - [SuppressMessage("StyleCop.CSharp.LayoutRules", "SA1501:Statement should not be on a single line", Justification = "stop wasting lines")] private static void GetCustomSettings() { string[] strArr; @@ -124,24 +118,37 @@ namespace Community.PowerToys.Run.Plugin.Everything if (str.Length == 0 || str[0] == '#') continue; string[] kv = str.Split('='); if (kv.Length != 2) continue; - switch (kv[0].Trim()) + string key = kv[0].Trim(); + if (key == "max") { - case "max": - try { max = uint.Parse(kv[1].Trim(), culture.NumberFormat); } - catch { } - break; - case "sort": - try { sort = (Sort)int.Parse(kv[1].Trim(), culture.NumberFormat); } - catch { } - break; - default: - continue; + try { max = uint.Parse(kv[1].Trim(), culture.NumberFormat); } + catch { } + } + else if (key == "sort") + { + try { sort = (Sort)int.Parse(kv[1].Trim(), culture.NumberFormat); } + catch { } + } + else if (key.Contains(':')) + { + filters.TryAdd(key.Split(':')[0].ToLowerInvariant(), kv[1].Trim()); } } } public static IEnumerable EverythingSearch(string qry, bool top, bool preview) { + Everything_SetMax(max); + if (qry.Contains(':')) + { + string[] nqry = qry.Split(':'); + if (filters.ContainsKey(nqry[0].ToLowerInvariant())) + { + Everything_SetMax(0xffffffff); + qry = nqry[1].Trim() + " ext:" + filters[nqry[0].Trim()]; + } + } + _ = Everything_SetSearchW(qry); if (!Everything_QueryW(true)) { @@ -151,9 +158,9 @@ namespace Community.PowerToys.Run.Plugin.Everything uint resultCount = Everything_GetNumResults(); for (uint i = 0; i < resultCount; i++) { - StringBuilder sb = new StringBuilder(260); - Everything_GetResultFullPathName(i, sb, 260); - string fullPath = sb.ToString(); + char[] buffer = new char[260]; + Everything_GetResultFullPathName(i, buffer, 260); + string fullPath = new string(buffer); string name = Path.GetFileName(fullPath); string path; bool isFolder = Path.HasExtension(fullPath.Replace(".lnk", string.Empty)); @@ -253,8 +260,9 @@ namespace Community.PowerToys.Run.Plugin.Everything catch (Exception) { #endif - // If exceptions occurs for a key despite condition checks, just move onto the next key, plugin will still work, just without that icon info - continue; + + // If exceptions occurs for a key despite condition checks, just move onto the next key, plugin will still work, just without that icon info + continue; } } } @@ -268,6 +276,7 @@ namespace Community.PowerToys.Run.Plugin.Everything catch (Exception) { #endif + // User privillege probably too low to access Registry Keys, plugin will still work, just without icon info } diff --git a/plugin.json b/plugin.json index de51213..5ae58dd 100644 --- a/plugin.json +++ b/plugin.json @@ -4,7 +4,7 @@ "IsGlobal": true, "Name": "Everything", "Author": "Yu Chieh (Victor) Lin", - "Version": "0.58.0.1", + "Version": "0.58.0.2", "Language": "csharp", "Website": "https://github.com/lin-ycv/EverythingPowerToys", "ExecuteFileName": "Community.PowerToys.Run.Plugin.Everything.dll", diff --git a/settings.toml b/settings.toml index 3a00228..b4fe8a5 100644 --- a/settings.toml +++ b/settings.toml @@ -9,5 +9,14 @@ # https://www.voidtools.com/support/everything/sdk/everything_getsort/ # sort = 14 +# Search filters, filters result with extension type +Audio: = aac;ac3;aif;aifc;aiff;amr;ape;au;cda;dts;fla;flac;it;m1a;m2a;m3u;m4a;m4b;m4p;mid;midi;mka;mod;mp2;mp3;mpa;ogg;opus;ra;rmi;spc;rmi;snd;umx;voc;wav;weba;wma;xm +Compressed: = 7z;ace;arj;bz2;cab;gz;gzip;jar;r00;r01;r02;r03;r04;r05;r06;r07;r08;r09;r10;r11;r12;r13;r14;r15;r16;r17;r18;r19;r20;r21;r22;r23;r24;r25;r26;r27;r28;r29;rar;tar;tgz;z;zip +Document: = c;cc;chm;cpp;cs;css;csv;cxx;doc;docm;docx;dot;dotm;dotx;epub;h;hpp;htm;html;hxx;ini;java;js;json;lua;mht;mhtml;mobi;odp;ods;odt;pdf;php;potx;potm;ppam;ppsm;ppsx;pps;ppt;pptm;pptx;pub;py;rtf;sldm;sldx;thmx;txt;vsd;wpd;wps;wri;xlam;xls;xlsb;xlsm;xlsx;xltm;xltx;xml;vb +Executable: = bat;cmd;exe;msi;msp;msu;ps1;scr +Folder: = +Picture: = ani;apng;bmp;bpg;cur;gif;ico;jfi;jfif;jif;jpe;jpeg;jpg;pcx;png;psb;psd;rle;svg;tga;tif;tiff;webp;wmf +Video: = 3g2;3gp;3gp2;3gpp;amv;asf;asx;avi;bdmv;bik;d2v;divx;drc;dsa;dsm;dss;dsv;evo;f4v;flc;fli;flic;flv;hdmov;ifo;ivf;m1v;m2p;m2t;m2ts;m2v;m4v;mkv;mp2v;mp4;mp4v;mpe;mpeg;mpg;mpls;mpv2;mpv4;mov;mts;ogm;ogv;pss;pva;qt;ram;ratdvd;rm;rmm;rmvb;roq;rpm;smil;smk;swf;tp;tpr;ts;vob;vp6;webm;wm;wmp;wmv + # There are no more override options # Restart of powertoys is needed if values are changed \ No newline at end of file