[PT Run] [Folder Plugin] Environment Variables With Autocomplete (#13811)

* search environment variables folders with autocomplete

* refactoring and tests

* fix
This commit is contained in:
Davide Giacometti
2021-10-20 15:27:15 +02:00
committed by GitHub
parent 15f3c2ff66
commit 64cc6b7af7
10 changed files with 296 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
// 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.Collections;
namespace Microsoft.Plugin.Folder.Sources
{
public class EnvironmentHelper : IEnvironmentHelper
{
public bool IsEnvironmentVariable(string search)
{
if (search == null)
{
throw new ArgumentNullException(paramName: nameof(search));
}
return search.StartsWith('%');
}
public IDictionary GetEnvironmentVariables()
{
return Environment.GetEnvironmentVariables();
}
}
}

View File

@@ -0,0 +1,15 @@
// 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.Collections;
namespace Microsoft.Plugin.Folder.Sources
{
public interface IEnvironmentHelper
{
bool IsEnvironmentVariable(string search);
IDictionary GetEnvironmentVariables();
}
}

View File

@@ -0,0 +1,14 @@
// 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.Collections.Generic;
using Microsoft.Plugin.Folder.Sources.Result;
namespace Microsoft.Plugin.Folder.Sources
{
public interface IQueryEnvironmentVariable
{
IEnumerable<IItemResult> Query(string querySearch);
}
}

View File

@@ -0,0 +1,56 @@
// 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.Collections;
using System.Collections.Generic;
using System.IO.Abstractions;
using System.Linq;
using Microsoft.Plugin.Folder.Sources.Result;
namespace Microsoft.Plugin.Folder.Sources
{
public class QueryEnvironmentVariable : IQueryEnvironmentVariable
{
private readonly IDirectory _directory;
private readonly IEnvironmentHelper _environmentHelper;
public QueryEnvironmentVariable(IDirectory directory, IEnvironmentHelper environmentHelper)
{
_directory = directory;
_environmentHelper = environmentHelper;
}
public IEnumerable<IItemResult> Query(string querySearch)
{
if (querySearch == null)
{
throw new ArgumentNullException(nameof(querySearch));
}
return GetEnvironmentVariables(querySearch)
.OrderBy(v => v.Title)
.Where(v => v.Title.StartsWith(querySearch, StringComparison.InvariantCultureIgnoreCase));
}
public IEnumerable<EnvironmentVariableResult> GetEnvironmentVariables(string querySearch)
{
foreach (DictionaryEntry variable in _environmentHelper.GetEnvironmentVariables())
{
if (variable.Value == null)
{
continue;
}
var name = "%" + (string)variable.Key + "%";
var path = (string)variable.Value;
if (_directory.Exists(path))
{
yield return new EnvironmentVariableResult(querySearch, name, path);
}
}
}
}
}

View File

@@ -0,0 +1,45 @@
// 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.Globalization;
using Wox.Infrastructure;
using Wox.Plugin;
namespace Microsoft.Plugin.Folder.Sources.Result
{
public class EnvironmentVariableResult : IItemResult
{
private readonly IShellAction _shellAction = new ShellAction();
public string Search { get; set; }
public string Title { get; private set; }
public string Subtitle { get; private set; }
public string Path { get; private set; }
public EnvironmentVariableResult(string search, string title, string path)
{
Search = search;
Title = title;
Path = path;
}
public Wox.Plugin.Result Create(IPublicAPI contextApi)
{
return new Wox.Plugin.Result(StringMatcher.FuzzySearch(Search, Title).MatchData)
{
Title = Title,
IcoPath = Path,
// Using CurrentCulture since this is user facing
SubTitle = string.Format(CultureInfo.CurrentCulture, Properties.Resources.wox_plugin_folder_select_folder_result_subtitle, Path),
QueryTextDisplay = Path,
ContextData = new SearchResult { Type = ResultType.Folder, FullPath = Path },
Action = c => _shellAction.Execute(Path, contextApi),
};
}
}
}