Skip to content

Commit

Permalink
Added option to expand Enviroment Variables
Browse files Browse the repository at this point in the history
Resolves #70
Code refactoring
  • Loading branch information
lin-ycv committed Jan 10, 2024
1 parent a84d81c commit 455d28f
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 36 deletions.
14 changes: 4 additions & 10 deletions ContextMenuLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,21 @@

namespace Community.PowerToys.Run.Plugin.Everything
{
internal sealed class ContextMenuLoader : IContextMenu
internal sealed class ContextMenuLoader(PluginInitContext context, string options) : IContextMenu
{
private readonly PluginInitContext _context;
private readonly PluginInitContext _context = context;

// Extensions for adding run as admin context menu item for applications
private readonly string[] _appExtensions = { ".exe", ".bat", ".appref-ms", ".lnk" };
private readonly string[] _appExtensions = [".exe", ".bat", ".appref-ms", ".lnk"];

private bool _swapCopy;
private string _options;
private string _options = options;
internal void Update(Settings s)
{
_swapCopy = s.Copy;
_options = s.Context;
}

public ContextMenuLoader(PluginInitContext context, string options)
{
_context = context;
_options = options;
}

public List<ContextMenuResult> LoadContextMenus(Result selectedResult)
{
var contextMenus = new List<ContextMenuResult>();
Expand Down
13 changes: 9 additions & 4 deletions Everything.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
Expand Down Expand Up @@ -33,9 +34,14 @@ internal IEnumerable<Result> Query(string query, Settings setting)
Everything_SetMatchPath(true);
}

if (setting.EnvVar && orgqry.Contains('%'))
{
query = Environment.ExpandEnvironmentVariables(query).Replace(';', '|');
}

if (orgqry.Contains(':'))
{
string[] nqry = query.Split(':');
string[] nqry = query.Split(':', 2);
if (setting.Filters.TryGetValue(nqry[0].ToLowerInvariant(), out string value))
query = nqry[1].Trim() + " ext:" + value;
}
Expand All @@ -55,7 +61,7 @@ internal IEnumerable<Result> Query(string query, Settings setting)

for (uint i = 0; i < resultCount; i++)
{
StringBuilder buffer = new StringBuilder(260);
StringBuilder buffer = new(260);
Everything_GetResultFullPathName(i, buffer, 260);
string fullPath = buffer.ToString();
string name = Path.GetFileName(fullPath);
Expand Down Expand Up @@ -100,7 +106,6 @@ internal IEnumerable<Result> Query(string query, Settings setting)
};
yield return r;
}

}
}
}
3 changes: 2 additions & 1 deletion GlobalSuppressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
[assembly: SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1602:Enumeration items should be documented", Justification = "Reviewed")]
[assembly: SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1633:File should have header", Justification = "Reviewed")]
[assembly: SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1636:FileHeaderCopyrightTextMustMatch", Justification = "Reviewed.")]
[assembly: SuppressMessage("Performance", "CA1838:Avoid 'StringBuilder' parameters for P/Invokes", Justification = "breaks icon preview for some reason when using char[]", Scope = "member", Target = "~M:Community.PowerToys.Run.Plugin.Everything.NativeMethods.Everything_GetResultFullPathName(System.UInt32,System.Text.StringBuilder,System.UInt32)")]
[assembly: SuppressMessage("Performance", "CA1838:Avoid 'StringBuilder' parameters for P/Invokes", Justification = "breaks icon preview for some reason when using char[]")]
[assembly: SuppressMessage("StyleCop.CSharp.SpacingRules", "SA1010:Opening square brackets should be spaced correctly", Justification = "Reviewed")]
39 changes: 23 additions & 16 deletions Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,22 @@ public class Main : IPlugin, IDisposable, IDelayedExecutionPlugin, IContextMenu,

private readonly Settings _setting;
private readonly PluginJsonStorage<Settings> _storage;
private readonly Everything _everything;

private IContextMenu _contextMenuLoader;
private ContextMenuLoader _contextMenuLoader;
private bool _disposed;

private Everything _everything;

public IEnumerable<PluginAdditionalOption> AdditionalOptions => new List<PluginAdditionalOption>()
{
new PluginAdditionalOption()
new()
{
Key = nameof(Settings.Context),
DisplayLabel = Resources.Context,
DisplayDescription = Resources.Context_Description,
PluginOptionType = PluginAdditionalOption.AdditionalOptionType.Textbox,
TextValue = _setting.Context,
},
new PluginAdditionalOption()
new()
{
Key = nameof(Settings.Sort),
DisplayLabel = Resources.Sort,
Expand All @@ -52,50 +51,57 @@ public class Main : IPlugin, IDisposable, IDelayedExecutionPlugin, IContextMenu,
ComboBoxItems = Enum.GetValues(typeof(Sort)).Cast<int>().Select(v => new KeyValuePair<string, string>(((Sort)v).ToString(), v + string.Empty)).ToList(),
ComboBoxValue = (int)_setting.Sort,
},
new PluginAdditionalOption()
new()
{
Key = nameof(Settings.Max),
DisplayLabel = Resources.Max,
DisplayDescription = Resources.Max_Description,
PluginOptionType = PluginAdditionalOption.AdditionalOptionType.Numberbox,
NumberValue = _setting.Max,
},
new PluginAdditionalOption()
new()
{
Key = nameof(Settings.Copy),
DisplayLabel = Resources.SwapCopy,
DisplayDescription = Resources.SwapCopy_Description,
Value = _setting.Copy,
},
new PluginAdditionalOption()
new()
{
Key = nameof(Settings.MatchPath),
DisplayLabel = Resources.Match_path,
DisplayDescription = Resources.Match_path_Description,
Value = _setting.MatchPath,
},
new PluginAdditionalOption()
new()
{
Key = nameof(Settings.Preview),
DisplayLabel = Resources.Preview,
DisplayDescription = Resources.Preview_Description,
Value = _setting.Preview,
},
new PluginAdditionalOption()
new()
{
Key = nameof(Settings.QueryText),
DisplayLabel = Resources.QueryText,
DisplayDescription = Resources.QueryText_Description,
Value = _setting.QueryText,
},
new PluginAdditionalOption()
new()
{
Key = nameof(Settings.RegEx),
DisplayLabel = Resources.RegEx,
DisplayDescription = Resources.RegEx_Description,
Value = _setting.RegEx,
},
new PluginAdditionalOption()
new()
{
Key = nameof(Settings.EnvVar),
DisplayLabel = Resources.EnvVar,
DisplayDescription = Resources.EnvVar_Description,
Value = _setting.EnvVar,
},
new()
{
Key = nameof(Settings.Updates),
DisplayLabel = Resources.Updates,
Expand Down Expand Up @@ -126,11 +132,12 @@ public void UpdateSettings(PowerLauncherPluginSettings settings)
_setting.MatchPath = settings.AdditionalOptions.FirstOrDefault(x => x.Key == nameof(_setting.MatchPath)).Value;
_setting.Copy = settings.AdditionalOptions.FirstOrDefault(x => x.Key == nameof(_setting.Copy)).Value;
_setting.QueryText = settings.AdditionalOptions.FirstOrDefault(x => x.Key == nameof(_setting.QueryText)).Value;
_setting.EnvVar = settings.AdditionalOptions.FirstOrDefault(x => x.Key == nameof(_setting.EnvVar)).Value;
_setting.Updates = settings.AdditionalOptions.FirstOrDefault(x => x.Key == nameof(_setting.Updates)).Value;

_everything.UpdateSettings(_setting);

if (_contextMenuLoader != null) ((ContextMenuLoader)_contextMenuLoader).Update(_setting);
if (_contextMenuLoader != null) _contextMenuLoader.Update(_setting);

Save();
}
Expand All @@ -139,18 +146,18 @@ public void UpdateSettings(PowerLauncherPluginSettings settings)
public void Init(PluginInitContext context)
{
_contextMenuLoader = new ContextMenuLoader(context, _setting.Context);
((ContextMenuLoader)_contextMenuLoader).Update(_setting);
_contextMenuLoader.Update(_setting);
}

public List<Result> Query(Query query)
{
List<Result> results = new List<Result>();
List<Result> results = [];
return results;
}

public List<Result> Query(Query query, bool delayedExecution)
{
List<Result> results = new List<Result>();
List<Result> results = [];
if (!string.IsNullOrEmpty(query.Search))
{
var searchQuery = query.Search;
Expand Down
18 changes: 18 additions & 0 deletions Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@
<data name="copy_shortcutAlt" xml:space="preserve">
<value>(Ctrl+Alt+C)</value>
</data>
<data name="EnvVar" xml:space="preserve">
<value>Enviroment Variables</value>
</data>
<data name="EnvVar_Description" xml:space="preserve">
<value>Expanded enviroment variables, such as %appdata%, so that they work properly at the cost of query time.</value>
</data>
<data name="Everything_ini" xml:space="preserve">
<value>Install Everything if not installed</value>
</data>
Expand Down
3 changes: 3 additions & 0 deletions Properties/Resources.zh.resx
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@
<data name="copy_path" xml:space="preserve">
<value>複製路徑</value>
</data>
<data name="EnvVar_Description" xml:space="preserve">
<value>搜尋時可用環境變數,但會增加搜尋時間</value>
</data>
<data name="Everything_ini" xml:space="preserve">
<value>是否有安裝 Everything?</value>
</data>
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Dev instructions are in the wiki.
#### Install instructions
1. Download the latest release from [here](https://github.com/lin-ycv/EverythingPowerToys/releases/latest).
2. Unzip.
3. Copy the `Everything` folder to `C:\Program Files\PowerToys\RunPlugins` or `%LOCALAPPDATA%\Microsoft\PowerToys\PowerToys Run\Plugins\`.
3. Copy the `Everything` folder to `C:\Program Files\PowerToys\RunPlugins` or `%LOCALAPPDATA%\PowerToys\RunPlugins`.
4. Restart PowerToys.

#### Requirements
Expand Down
3 changes: 2 additions & 1 deletion SearchHelper/IconLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Community.PowerToys.Run.Plugin.Everything.SearchHelper
{
internal sealed class IconLoader
{
internal static readonly char[] Separator = ['\"', ','];
#pragma warning disable CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
internal static string? Icon(string doctype)
#pragma warning restore CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
Expand All @@ -14,7 +15,7 @@ internal sealed class IconLoader
_ = AssocQueryString(AssocF.NONE, AssocStr.DEFAULTICON, doctype, null, null, ref pcchOut);
char[] pszOut = new char[pcchOut];
if (AssocQueryString(AssocF.NONE, AssocStr.DEFAULTICON, doctype, null, pszOut, ref pcchOut) != 0) return null;
string doc = Environment.ExpandEnvironmentVariables(new string(pszOut).Split(new char[] { '\"', ',' }, StringSplitOptions.RemoveEmptyEntries)[0].Replace("\"", string.Empty, StringComparison.CurrentCulture).Trim());
string doc = Environment.ExpandEnvironmentVariables(new string(pszOut).Split(Separator, StringSplitOptions.RemoveEmptyEntries)[0].Replace("\"", string.Empty, StringComparison.CurrentCulture).Trim());

return File.Exists(doc) ? doc : null;
}
Expand Down
4 changes: 2 additions & 2 deletions Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ public class Settings
public bool Preview { get; set; } = true;
public bool QueryText { get; set; }
public bool RegEx { get; set; }
public bool EnvVar { get; set; }
public bool Updates { get; set; } = true;
public string Skip { get; set; }

// Get Filters from settings.toml
public Dictionary<string, string> Filters { get; } = new Dictionary<string, string>();
public Dictionary<string, string> Filters { get; } = [];
internal void Getfilters()
{
string[] strArr;
try { strArr = File.ReadAllLines(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "settings.toml")); }
catch { return; }
var culture = new System.Globalization.CultureInfo("en-US");
foreach (string str in strArr)
{
if (str.Length == 0 || str[0] == '#') continue;
Expand Down
2 changes: 1 addition & 1 deletion plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"IsGlobal": true,
"Name": "Everything",
"Author": "Yu Chieh (Victor) Lin",
"Version": "0.76.2",
"Version": "0.77.0",
"Language": "csharp",
"Website": "https://github.com/Lin-ycv/EverythingPowerToys",
"ExecuteFileName": "Community.PowerToys.Run.Plugin.Everything.dll",
Expand Down

0 comments on commit 455d28f

Please sign in to comment.