PowerToys/Plugins/Wox.Plugin.Calculator/Main.cs

116 lines
3.9 KiB
C#
Raw Normal View History

2015-01-15 20:47:48 +08:00
using System.Collections.Generic;
2016-01-07 05:34:42 +08:00
using System.Runtime.InteropServices;
2014-03-18 00:43:20 +08:00
using System.Text.RegularExpressions;
using System.Windows;
using Mages.Core;
2014-03-18 00:43:20 +08:00
2015-01-15 20:47:48 +08:00
namespace Wox.Plugin.Caculator
2014-03-18 00:43:20 +08:00
{
2016-05-07 10:55:09 +08:00
public class Main : IPlugin, IPluginI18n
2014-03-18 00:43:20 +08:00
{
private static readonly Regex RegValidExpressChar = new Regex(
2014-03-18 00:43:20 +08:00
@"^(" +
@"ceil|floor|exp|pi|e|max|min|det|abs|log|ln|sqrt|" +
@"sin|cos|tan|arcsin|arccos|arctan|" +
2014-03-18 00:43:20 +08:00
@"eigval|eigvec|eig|sum|polar|plot|round|sort|real|zeta|" +
@"bin2dec|hex2dec|oct2dec|" +
@"==|~=|&&|\|\||" +
@"[ei]|[0-9]|[\+\-\*\/\^\., ""]|[\(\)\|\!\[\]]" +
@")+$", RegexOptions.Compiled);
private static readonly Regex RegBrackets = new Regex(@"[\(\)\[\]]", RegexOptions.Compiled);
private static readonly Engine MagesEngine;
private PluginInitContext Context { get; set; }
2014-03-18 00:43:20 +08:00
2016-05-07 10:55:09 +08:00
static Main()
2014-03-18 00:43:20 +08:00
{
MagesEngine = new Engine();
2014-03-18 00:43:20 +08:00
}
public List<Result> Query(Query query)
2014-03-18 00:43:20 +08:00
{
if (query.Search.Length <= 2 // don't affect when user only input "e" or "i" keyword
|| !RegValidExpressChar.IsMatch(query.Search)
|| !IsBracketComplete(query.Search)) return new List<Result>();
2014-03-18 00:43:20 +08:00
try
{
var result = MagesEngine.Interpret(query.Search);
if (result.ToString() == "NaN")
result = Context.API.GetTranslation("wox_plugin_calculator_not_a_number");
if (result is Function)
result = Context.API.GetTranslation("wox_plugin_calculator_expression_not_complete");
if (!string.IsNullOrEmpty(result?.ToString()))
2014-03-18 00:43:20 +08:00
{
2016-01-07 05:34:42 +08:00
return new List<Result>
{
new Result
2014-03-18 00:43:20 +08:00
{
Title = result.ToString(),
IcoPath = "Images/calculator.png",
Score = 300,
SubTitle = Context.API.GetTranslation("wox_plugin_calculator_copy_number_to_clipboard"),
Action = c =>
{
try
{
Clipboard.SetText(result.ToString());
return true;
}
catch (ExternalException)
{
MessageBox.Show("Copy failed, please try later");
return false;
}
}
}
};
2014-03-18 00:43:20 +08:00
}
}
catch
{
// ignored
}
2014-03-18 00:43:20 +08:00
return new List<Result>();
}
private bool IsBracketComplete(string query)
{
var matchs = RegBrackets.Matches(query);
2014-03-18 00:43:20 +08:00
var leftBracketCount = 0;
foreach (Match match in matchs)
{
if (match.Value == "(" || match.Value == "[")
{
leftBracketCount++;
}
else
{
leftBracketCount--;
}
}
return leftBracketCount == 0;
}
public void Init(PluginInitContext context)
2014-03-18 00:43:20 +08:00
{
Context = context;
2014-03-18 00:43:20 +08:00
}
2015-02-07 21:27:48 +08:00
public string GetTranslatedPluginTitle()
{
return Context.API.GetTranslation("wox_plugin_caculator_plugin_name");
2015-02-07 21:27:48 +08:00
}
public string GetTranslatedPluginDescription()
{
return Context.API.GetTranslation("wox_plugin_caculator_plugin_description");
2015-02-07 21:27:48 +08:00
}
2014-03-18 00:43:20 +08:00
}
}