2015-01-15 20:47:48 +08:00
|
|
|
|
using System.Collections.Generic;
|
2017-04-11 05:43:23 +08:00
|
|
|
|
using System.Globalization;
|
2016-01-07 05:34:42 +08:00
|
|
|
|
using System.Runtime.InteropServices;
|
2014-03-18 00:43:20 +08:00
|
|
|
|
using System.Text.RegularExpressions;
|
2015-01-03 15:20:34 +08:00
|
|
|
|
using System.Windows;
|
2014-03-18 00:43:20 +08:00
|
|
|
|
using YAMP;
|
|
|
|
|
|
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 Regex regValidExpressChar = new Regex(
|
|
|
|
|
@"^(" +
|
2016-08-05 08:52:11 +08:00
|
|
|
|
@"ceil|floor|exp|pi|e|max|min|det|abs|log|ln|sqrt|" +
|
2016-08-05 04:17:00 +08:00
|
|
|
|
@"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 Regex regBrackets = new Regex(@"[\(\)\[\]]", RegexOptions.Compiled);
|
2016-01-07 05:34:42 +08:00
|
|
|
|
private static ParseContext yampContext;
|
2014-03-18 00:43:20 +08:00
|
|
|
|
private PluginInitContext context { get; set; }
|
2017-04-11 05:43:23 +08:00
|
|
|
|
private NumberTranslator _numberTranslator;
|
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
|
|
|
|
{
|
|
|
|
|
yampContext = Parser.PrimaryContext;
|
|
|
|
|
Parser.InteractiveMode = false;
|
|
|
|
|
Parser.UseScripting = false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-03 15:20:34 +08:00
|
|
|
|
public List<Result> Query(Query query)
|
2014-03-18 00:43:20 +08:00
|
|
|
|
{
|
2015-01-26 17:46:55 +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
|
|
|
|
|
{
|
2017-04-11 05:43:23 +08:00
|
|
|
|
var result = yampContext.Run(this._numberTranslator?.Translate(query.Search) ?? query.Search);
|
2014-03-18 00:43:20 +08:00
|
|
|
|
if (result.Output != null && !string.IsNullOrEmpty(result.Result))
|
|
|
|
|
{
|
2017-04-11 05:43:23 +08:00
|
|
|
|
string resultValue = this._numberTranslator?.TranslateBack(result.Result) ?? result.Result;
|
2016-01-07 05:34:42 +08:00
|
|
|
|
return new List<Result>
|
2017-04-11 05:43:23 +08:00
|
|
|
|
{
|
|
|
|
|
new Result
|
2014-03-18 00:43:20 +08:00
|
|
|
|
{
|
2017-04-11 05:43:23 +08:00
|
|
|
|
Title = resultValue,
|
|
|
|
|
IcoPath = "Images/calculator.png",
|
|
|
|
|
Score = 300,
|
|
|
|
|
SubTitle = "Copy this number to the clipboard",
|
|
|
|
|
Action = c =>
|
2014-08-20 22:12:45 +08:00
|
|
|
|
{
|
2017-04-11 05:43:23 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Clipboard.SetText(resultValue);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (ExternalException e)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Copy failed, please try later");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2014-08-20 22:12:45 +08:00
|
|
|
|
}
|
2014-03-18 00:43:20 +08:00
|
|
|
|
}
|
2017-04-11 05:43:23 +08:00
|
|
|
|
};
|
2014-03-18 00:43:20 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch
|
2017-04-11 05:43:23 +08:00
|
|
|
|
{ }
|
2014-03-18 00:43:20 +08:00
|
|
|
|
|
|
|
|
|
return new List<Result>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool IsBracketComplete(string query)
|
|
|
|
|
{
|
|
|
|
|
var matchs = regBrackets.Matches(query);
|
|
|
|
|
var leftBracketCount = 0;
|
|
|
|
|
foreach (Match match in matchs)
|
|
|
|
|
{
|
|
|
|
|
if (match.Value == "(" || match.Value == "[")
|
|
|
|
|
{
|
|
|
|
|
leftBracketCount++;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
leftBracketCount--;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return leftBracketCount == 0;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-03 15:20:34 +08:00
|
|
|
|
public void Init(PluginInitContext context)
|
2014-03-18 00:43:20 +08:00
|
|
|
|
{
|
|
|
|
|
this.context = context;
|
2017-04-11 05:43:23 +08:00
|
|
|
|
this._numberTranslator = NumberTranslator.Create(CultureInfo.CurrentCulture, CultureInfo.InvariantCulture);
|
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");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetTranslatedPluginDescription()
|
|
|
|
|
{
|
|
|
|
|
return context.API.GetTranslation("wox_plugin_caculator_plugin_description");
|
|
|
|
|
}
|
2014-03-18 00:43:20 +08:00
|
|
|
|
}
|
|
|
|
|
}
|