From fd1de0ef858b31abfab9cc62f6f9bab0b8a69d2c Mon Sep 17 00:00:00 2001 From: cxfksword Date: Tue, 18 Mar 2014 00:43:20 +0800 Subject: [PATCH] add Calculator system plugin --- Wox.Plugin.System/Calculator.cs | 91 +++++++++++++++++++++ Wox.Plugin.System/Wox.Plugin.System.csproj | 4 + Wox.Plugin.System/packages.config | 1 + Wox/Images/calculator.png | Bin 0 -> 2638 bytes Wox/Wox.csproj | 3 + 5 files changed, 99 insertions(+) create mode 100644 Wox.Plugin.System/Calculator.cs create mode 100644 Wox/Images/calculator.png 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 0000000000000000000000000000000000000000..7be787f9f32a4c29789b64dd7e9e0fa89c70a2f9 GIT binary patch literal 2638 zcmd^B`BzhC8qVOcW23fqJWj`jF%2$I3rSExGT;IU1;-$=REltFrG1MS?&GOCSUg5CpOiNJ2v?KKIrVY>+_av$r+IE6?573o%84M&c0N5!q%?m&{Qi@z9cI$^2Bk3H46(7PyvvLU{SC7F|jNgYG=DRXM1w*ljbQH!6 ztxsv)kU;p~rc~;CYbNLd{5#(NO3ZW%q5~)wfJqBrkf6q0HqyJIW9=9K5u`EPXtb;A zRdn{Dfi$KMjgGK$wLlm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