mirror of
https://github.com/Flow-Launcher/Flow.Launcher.git
synced 2026-03-11 08:54:32 +00:00
Merge pull request #32 from JohnTheGr8/folder_plugin_env_strings
Folder plugin: handle environment string paths
This commit is contained in:
commit
484eb9cdff
1 changed files with 99 additions and 4 deletions
|
|
@ -27,6 +27,8 @@ namespace Flow.Launcher.Plugin.Folder
|
|||
private readonly PluginJsonStorage<Settings> _storage;
|
||||
private IContextMenu _contextMenuLoader;
|
||||
|
||||
private static Dictionary<string, string> _envStringPaths;
|
||||
|
||||
public Main()
|
||||
{
|
||||
_storage = new PluginJsonStorage<Settings>();
|
||||
|
|
@ -48,6 +50,7 @@ namespace Flow.Launcher.Plugin.Folder
|
|||
_context = context;
|
||||
_contextMenuLoader = new ContextMenuLoader(context);
|
||||
InitialDriverList();
|
||||
LoadEnvironmentStringPaths();
|
||||
}
|
||||
|
||||
public List<Result> Query(Query query)
|
||||
|
|
@ -58,7 +61,14 @@ namespace Flow.Launcher.Plugin.Folder
|
|||
if (!IsDriveOrSharedFolder(search))
|
||||
return results;
|
||||
|
||||
results.AddRange(QueryInternal_Directory_Exists(query));
|
||||
if (search.StartsWith("%"))
|
||||
{
|
||||
results.AddRange(GetEnvironmentStringPathResults(search, query));
|
||||
}
|
||||
else
|
||||
{
|
||||
results.AddRange(QueryInternal_Directory_Exists(query.Search, query));
|
||||
}
|
||||
|
||||
// todo why was this hack here?
|
||||
foreach (var result in results)
|
||||
|
|
@ -72,7 +82,12 @@ namespace Flow.Launcher.Plugin.Folder
|
|||
private static bool IsDriveOrSharedFolder(string search)
|
||||
{
|
||||
if (search.StartsWith(@"\\"))
|
||||
{ // share folder
|
||||
{ // shared folder
|
||||
return true;
|
||||
}
|
||||
|
||||
if (_envStringPaths != null && search.StartsWith("%"))
|
||||
{ // environment string formatted folder
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -146,14 +161,37 @@ namespace Flow.Launcher.Plugin.Folder
|
|||
}
|
||||
}
|
||||
|
||||
private void LoadEnvironmentStringPaths()
|
||||
{
|
||||
_envStringPaths = new Dictionary<string, string>();
|
||||
|
||||
var specialPaths =
|
||||
new Dictionary<string,Environment.SpecialFolder> {
|
||||
{ "appdata", Environment.SpecialFolder.ApplicationData },
|
||||
{ "localappdata", Environment.SpecialFolder.LocalApplicationData },
|
||||
{ "programfiles", Environment.SpecialFolder.ProgramFiles },
|
||||
{ "programfiles(x86)", Environment.SpecialFolder.ProgramFilesX86 },
|
||||
{ "programdata", Environment.SpecialFolder.CommonApplicationData },
|
||||
{ "userprofile", Environment.SpecialFolder.UserProfile }
|
||||
};
|
||||
|
||||
foreach (var special in specialPaths)
|
||||
{
|
||||
_envStringPaths.Add(special.Key, Environment.GetFolderPath(special.Value));
|
||||
}
|
||||
|
||||
var tempDirectoryPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Temp");
|
||||
|
||||
_envStringPaths.Add("temp", tempDirectoryPath);
|
||||
}
|
||||
|
||||
private static readonly char[] _specialSearchChars = new char[]
|
||||
{
|
||||
'?', '*', '>'
|
||||
};
|
||||
|
||||
private List<Result> QueryInternal_Directory_Exists(Query query)
|
||||
private List<Result> QueryInternal_Directory_Exists(string search, Query query)
|
||||
{
|
||||
var search = query.Search;
|
||||
var results = new List<Result>();
|
||||
var hasSpecial = search.IndexOfAny(_specialSearchChars) >= 0;
|
||||
string incompleteName = "";
|
||||
|
|
@ -243,6 +281,63 @@ namespace Flow.Launcher.Plugin.Folder
|
|||
return results.Concat(folderList.OrderBy(x => x.Title)).Concat(fileList.OrderBy(x => x.Title)).ToList();
|
||||
}
|
||||
|
||||
private List<Result> GetEnvironmentStringPathSuggestions(string search, Query query)
|
||||
{
|
||||
var results = new List<Result>();
|
||||
foreach (var p in _envStringPaths)
|
||||
{
|
||||
if (p.Key.StartsWith(search))
|
||||
{
|
||||
results.Add(CreateFolderResult($"%{p.Key}%", p.Value, p.Value, query));
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
private List<Result> GetEnvironmentStringPathResults(string envStringSearch, Query query)
|
||||
{
|
||||
if (envStringSearch == "%")
|
||||
{ // return all environment string options as path suggestions
|
||||
return GetEnvironmentStringPathSuggestions("", query);
|
||||
}
|
||||
|
||||
var results = new List<Result>();
|
||||
var search = envStringSearch.Substring(1);
|
||||
|
||||
if (search.EndsWith("%") && search.Length > 1)
|
||||
{ // query starts and ends with a %, find an exact match from env-string paths
|
||||
var exactEnvStringPath = search.Substring(0, search.Length-1);
|
||||
|
||||
if (_envStringPaths.ContainsKey(exactEnvStringPath))
|
||||
{
|
||||
var expandedPath = _envStringPaths[exactEnvStringPath];
|
||||
results.Add(CreateFolderResult($"%{exactEnvStringPath}%", expandedPath, expandedPath, query));
|
||||
}
|
||||
}
|
||||
else if (search.Contains("%"))
|
||||
{ // query starts with a % and contains another % somewhere before the end
|
||||
var splitSearch = search.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
|
||||
var updatedSearch = envStringSearch.Replace(queryPartToReplace, expandedPath);
|
||||
|
||||
results.AddRange(QueryInternal_Directory_Exists(updatedSearch, query));
|
||||
}
|
||||
}
|
||||
else
|
||||
{ // query simply starts wtih a %, suggest env-string paths that match the rest of the search
|
||||
results.AddRange(GetEnvironmentStringPathSuggestions(search, query));
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
private static Result CreateFileResult(string filePath, Query query)
|
||||
{
|
||||
var result = new Result
|
||||
|
|
|
|||
Loading…
Reference in a new issue