mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-09 20:57:22 +02:00
[PTRun][Calculator]Replace input with result with '=' key (#31391)
* replace input with result * fix and add test * add options and use result property
This commit is contained in:
committed by
GitHub
parent
e573b7a1b1
commit
92c85630a9
@@ -17,6 +17,10 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator
|
||||
{
|
||||
public class Main : IPlugin, IPluginI18n, IDisposable, ISettingProvider
|
||||
{
|
||||
private const string InputUseEnglishFormat = nameof(InputUseEnglishFormat);
|
||||
private const string OutputUseEnglishFormat = nameof(OutputUseEnglishFormat);
|
||||
private const string ReplaceInput = nameof(ReplaceInput);
|
||||
|
||||
private static readonly CalculateEngine CalculateEngine = new CalculateEngine();
|
||||
|
||||
private PluginInitContext Context { get; set; }
|
||||
@@ -25,6 +29,7 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator
|
||||
|
||||
private bool _inputUseEnglishFormat;
|
||||
private bool _outputUseEnglishFormat;
|
||||
private bool _replaceInput;
|
||||
|
||||
public string Name => Resources.wox_plugin_calculator_plugin_name;
|
||||
|
||||
@@ -40,20 +45,27 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator
|
||||
public IEnumerable<PluginAdditionalOption> AdditionalOptions => new List<PluginAdditionalOption>()
|
||||
{
|
||||
// The number examples has to be created at runtime to prevent translation.
|
||||
new PluginAdditionalOption()
|
||||
new PluginAdditionalOption
|
||||
{
|
||||
Key = "InputUseEnglishFormat",
|
||||
Key = InputUseEnglishFormat,
|
||||
DisplayLabel = Resources.wox_plugin_calculator_in_en_format,
|
||||
DisplayDescription = string.Format(CultureInfo.CurrentCulture, WoxPluginCalculatorInEnFormatDescription, 1000.55.ToString("N2", new CultureInfo("en-us"))),
|
||||
Value = false,
|
||||
},
|
||||
new PluginAdditionalOption()
|
||||
new PluginAdditionalOption
|
||||
{
|
||||
Key = "OutputUseEnglishFormat",
|
||||
Key = OutputUseEnglishFormat,
|
||||
DisplayLabel = Resources.wox_plugin_calculator_out_en_format,
|
||||
DisplayDescription = string.Format(CultureInfo.CurrentCulture, WoxPluginCalculatorOutEnFormatDescription, 1000.55.ToString("G", new CultureInfo("en-us"))),
|
||||
Value = false,
|
||||
},
|
||||
new PluginAdditionalOption
|
||||
{
|
||||
Key = ReplaceInput,
|
||||
DisplayLabel = Resources.wox_plugin_calculator_replace_input,
|
||||
DisplayDescription = Resources.wox_plugin_calculator_replace_input_description,
|
||||
Value = true,
|
||||
},
|
||||
};
|
||||
|
||||
public List<Result> Query(Query query)
|
||||
@@ -61,6 +73,7 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator
|
||||
ArgumentNullException.ThrowIfNull(query);
|
||||
|
||||
bool isGlobalQuery = string.IsNullOrEmpty(query.ActionKeyword);
|
||||
bool replaceInput = _replaceInput && !isGlobalQuery && query.Search.EndsWith('=');
|
||||
CultureInfo inputCulture = _inputUseEnglishFormat ? new CultureInfo("en-us") : CultureInfo.CurrentCulture;
|
||||
CultureInfo outputCulture = _outputUseEnglishFormat ? new CultureInfo("en-us") : CultureInfo.CurrentCulture;
|
||||
|
||||
@@ -73,6 +86,11 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator
|
||||
NumberTranslator translator = NumberTranslator.Create(inputCulture, new CultureInfo("en-US"));
|
||||
var input = translator.Translate(query.Search.Normalize(NormalizationForm.FormKC));
|
||||
|
||||
if (replaceInput)
|
||||
{
|
||||
input = input[..^1];
|
||||
}
|
||||
|
||||
if (!CalculateHelper.InputValid(input))
|
||||
{
|
||||
return new List<Result>();
|
||||
@@ -89,10 +107,16 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator
|
||||
// If errorMessage is not default then do error handling
|
||||
return errorMessage == default ? new List<Result>() : ErrorHandler.OnError(IconPath, isGlobalQuery, query.RawQuery, errorMessage);
|
||||
}
|
||||
else if (replaceInput)
|
||||
{
|
||||
var pluginResult = ResultHelper.CreateResult(result.RoundedResult, IconPath, inputCulture, outputCulture);
|
||||
Context.API.ChangeQuery($"{query.ActionKeyword} {pluginResult.QueryTextDisplay}");
|
||||
return new List<Result>();
|
||||
}
|
||||
|
||||
return new List<Result>
|
||||
{
|
||||
ResultHelper.CreateResult(result.RoundedResult, IconPath, outputCulture),
|
||||
ResultHelper.CreateResult(result.RoundedResult, IconPath, inputCulture, outputCulture),
|
||||
};
|
||||
}
|
||||
catch (Mages.Core.ParseException)
|
||||
@@ -157,18 +181,23 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator
|
||||
{
|
||||
var inputUseEnglishFormat = false;
|
||||
var outputUseEnglishFormat = false;
|
||||
var replaceInput = true;
|
||||
|
||||
if (settings != null && settings.AdditionalOptions != null)
|
||||
{
|
||||
var optionInputEn = settings.AdditionalOptions.FirstOrDefault(x => x.Key == "InputUseEnglishFormat");
|
||||
var optionInputEn = settings.AdditionalOptions.FirstOrDefault(x => x.Key == InputUseEnglishFormat);
|
||||
inputUseEnglishFormat = optionInputEn?.Value ?? inputUseEnglishFormat;
|
||||
|
||||
var optionOutputEn = settings.AdditionalOptions.FirstOrDefault(x => x.Key == "OutputUseEnglishFormat");
|
||||
var optionOutputEn = settings.AdditionalOptions.FirstOrDefault(x => x.Key == OutputUseEnglishFormat);
|
||||
outputUseEnglishFormat = optionOutputEn?.Value ?? outputUseEnglishFormat;
|
||||
|
||||
var optionReplaceInput = settings.AdditionalOptions.FirstOrDefault(x => x.Key == ReplaceInput);
|
||||
replaceInput = optionReplaceInput?.Value ?? replaceInput;
|
||||
}
|
||||
|
||||
_inputUseEnglishFormat = inputUseEnglishFormat;
|
||||
_outputUseEnglishFormat = outputUseEnglishFormat;
|
||||
_replaceInput = replaceInput;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
|
||||
Reference in New Issue
Block a user