diff --git a/Wox.Plugin.System/Calculator.cs b/Wox.Plugin.System/Calculator.cs new file mode 100644 index 0000000000..a1863507a2 --- /dev/null +++ b/Wox.Plugin.System/Calculator.cs @@ -0,0 +1,91 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Text.RegularExpressions; +using System.Windows.Forms; +using YAMP; + +namespace Wox.Plugin.System +{ + public class Calculator : BaseSystemPlugin + { + private static Regex regValidExpressChar = new Regex( + @"^(" + + @"sin|cos|ceil|floor|exp|pi|max|min|det|arccos|abs|" + + @"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); + private static ParseContext yampContext = null; + private PluginInitContext context { get; set; } + + static Calculator() + { + yampContext = Parser.PrimaryContext; + Parser.InteractiveMode = false; + Parser.UseScripting = false; + } + + protected override List QueryInternal(Query query) + { + if (string.IsNullOrEmpty(query.RawQuery) + || query.RawQuery.Length < 2 // don't affect when user only input "e" or "i" keyword + || !regValidExpressChar.IsMatch(query.RawQuery) + || !IsBracketComplete(query.RawQuery)) return new List(); + + try + { + var result = yampContext.Run(query.RawQuery); + if (result.Output != null && !string.IsNullOrEmpty(result.Result)) + { + return new List() { new Result() { + Title = result.Result, + IcoPath = "Images/calculator.png", + Score = 300, + SubTitle = "Copy this number to the clipboard", + Action = (c) => + { + Clipboard.SetText(result.Result); + return true; + } + } }; + } + } + catch + {} + + return new List(); + } + + 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; + } + + protected override void InitInternal(PluginInitContext context) + { + this.context = context; + } + + + } +} diff --git a/Wox.Plugin.System/Wox.Plugin.System.csproj b/Wox.Plugin.System/Wox.Plugin.System.csproj index ff6072634f..6defda050e 100644 --- a/Wox.Plugin.System/Wox.Plugin.System.csproj +++ b/Wox.Plugin.System/Wox.Plugin.System.csproj @@ -35,6 +35,9 @@ ..\packages\Newtonsoft.Json.5.0.8\lib\net35\Newtonsoft.Json.dll + + ..\packages\YAMP.1.3.0\lib\net35\YAMP.dll + @@ -47,6 +50,7 @@ + diff --git a/Wox.Plugin.System/packages.config b/Wox.Plugin.System/packages.config index 9520f36d83..0fa426d977 100644 --- a/Wox.Plugin.System/packages.config +++ b/Wox.Plugin.System/packages.config @@ -1,4 +1,5 @@  + \ No newline at end of file diff --git a/Wox/Images/calculator.png b/Wox/Images/calculator.png new file mode 100644 index 0000000000..7be787f9f3 Binary files /dev/null and b/Wox/Images/calculator.png differ diff --git a/Wox/Wox.csproj b/Wox/Wox.csproj index 7a230469de..50a48365a6 100644 --- a/Wox/Wox.csproj +++ b/Wox/Wox.csproj @@ -307,6 +307,9 @@ PreserveNewest + + PreserveNewest +