mirror of
https://github.com/Flow-Launcher/Flow.Launcher.git
synced 2026-03-11 08:54:32 +00:00
Add EnvironmentVariablesPath handling
This commit is contained in:
parent
9039848078
commit
9b416bf75f
2 changed files with 104 additions and 2 deletions
|
|
@ -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<string, string> LoadEnvironmentStringPaths()
|
||||
{
|
||||
var envStringPaths = new Dictionary<string, string>();
|
||||
|
||||
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<Result> GetEnvironmentStringPathSuggestions(string querySearch, Query query)
|
||||
{
|
||||
var results = new List<Result>();
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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<Result> 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,
|
||||
|
|
|
|||
Loading…
Reference in a new issue