From 9b416bf75fd8f96a821552fb28b07b7ee656a23a Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Sun, 24 May 2020 19:09:44 +1000 Subject: [PATCH] Add EnvironmentVariablesPath handling --- .../Search/EnvironmentVariables.cs | 90 +++++++++++++++++++ .../Search/SearchManager.cs | 16 +++- 2 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 Plugins/Flow.Launcher.Plugin.Explorer/Search/EnvironmentVariables.cs diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Search/EnvironmentVariables.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Search/EnvironmentVariables.cs new file mode 100644 index 000000000..dc48512aa --- /dev/null +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Search/EnvironmentVariables.cs @@ -0,0 +1,90 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.IO; +using System.Text; + +namespace Flow.Launcher.Plugin.Explorer.Search +{ + public static class EnvironmentVariables + { + internal static bool IsEnvironmentVariableSearch(string search) + { + return LoadEnvironmentStringPaths().Count > 0 && search.StartsWith("%") && !search.Substring(1).Contains("%"); + } + + internal static Dictionary LoadEnvironmentStringPaths() + { + var envStringPaths = new Dictionary(); + + foreach (DictionaryEntry special in Environment.GetEnvironmentVariables()) + { + if (Directory.Exists(special.Value.ToString())) + { + envStringPaths.Add(special.Key.ToString().ToLower(), special.Value.ToString()); + } + } + + return envStringPaths; + } + + internal static string TranslateEnvironmentVariablePath(string environmentVariablePath) + { + var envStringPaths = LoadEnvironmentStringPaths(); + var splitSearch = environmentVariablePath.Substring(1).Split("%"); + var exactEnvStringPath = splitSearch[0]; + + // if there are more than 2 % characters in the query, don't bother + if (splitSearch.Length == 2 && envStringPaths.ContainsKey(exactEnvStringPath)) + { + var queryPartToReplace = $"%{exactEnvStringPath}%"; + var expandedPath = envStringPaths[exactEnvStringPath]; + // replace the %envstring% part of the query with its expanded equivalent + return environmentVariablePath.Replace(queryPartToReplace, expandedPath); + } + + return environmentVariablePath; + } + + internal static List GetEnvironmentStringPathSuggestions(string querySearch, Query query) + { + var results = new List(); + + var environmentVariables = LoadEnvironmentStringPaths(); + var search = querySearch; + + if (querySearch.EndsWith("%") && search.Length > 1) + { + // query starts and ends with a %, find an exact match from env-string paths + search = querySearch.Substring(1, search.Length - 2); + + if (environmentVariables.ContainsKey(search)) + { + var expandedPath = environmentVariables[search]; + + results.Add(new ResultManager().CreateFolderResult($"%{search}%", expandedPath, expandedPath, query)); + + return results; + } + } + + if (querySearch == "%") + { + search = ""; // Get all paths + } + else + { + search = search.Substring(1); + } + + foreach (var p in environmentVariables) + { + if (p.Key.StartsWith(search)) + { + results.Add(new ResultManager().CreateFolderResult($"%{p.Key}%", p.Value, p.Value, query)); + } + } + return results; + } + } +} diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Search/SearchManager.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Search/SearchManager.cs index 6e8a61095..e2e195f4d 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Search/SearchManager.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Search/SearchManager.cs @@ -1,4 +1,4 @@ -using Flow.Launcher.Plugin.Explorer.Search.WindowsIndex; +using Flow.Launcher.Plugin.Explorer.Search.WindowsIndex; using System; using System.Collections.Generic; @@ -20,8 +20,20 @@ namespace Flow.Launcher.Plugin.Explorer.Search public List Search(string querySearchString) { - if (IsLocationPathString(querySearchString)) + var querySearch = query.Search; + + if (EnvironmentVariables.IsEnvironmentVariableSearch(querySearch)) { + return EnvironmentVariables.GetEnvironmentStringPathSuggestions(querySearch, query); + } + + // Query is a location path with a full environment variable- starts with a % + // and contains another % somewhere before the end of the path + if (querySearch.Substring(1).Contains("%")) + { + querySearch = EnvironmentVariables.TranslateEnvironmentVariablePath(querySearch); + } + return TopLevelFolderSearchBehaviour(WindowsIndexTopLevelFolderSearch, DirectoryInfoClassSearch, WindowsIndexExists,