From 36111aa001625824241c264adc16b03357c1b1bf Mon Sep 17 00:00:00 2001 From: Michael Wirth Date: Sun, 15 Oct 2017 20:21:23 +0200 Subject: [PATCH 1/3] since Yamp is not maintained anymore, i switched it for Mages https://github.com/FlorianRappl/Mages fixes also #1022 --- .../Wox.Plugin.Calculator/Languages/de.xaml | 4 +- .../Wox.Plugin.Calculator/Languages/en.xaml | 4 +- Plugins/Wox.Plugin.Calculator/Main.cs | 57 ++++++++++--------- .../Wox.Plugin.Calculator.csproj | 8 +-- Plugins/Wox.Plugin.Calculator/packages.config | 2 +- 5 files changed, 40 insertions(+), 35 deletions(-) diff --git a/Plugins/Wox.Plugin.Calculator/Languages/de.xaml b/Plugins/Wox.Plugin.Calculator/Languages/de.xaml index 02c51e8c7..286e4bc9b 100644 --- a/Plugins/Wox.Plugin.Calculator/Languages/de.xaml +++ b/Plugins/Wox.Plugin.Calculator/Languages/de.xaml @@ -4,5 +4,7 @@ Rechner Stellt mathematische Berechnungen bereit.(Versuche 5*3-2 in Wox) - + Keine Zahl (NaN) + Ausdruck falsch oder nicht vollständig (Klammern vergessen?) + Diese Zahl in die Zwischenablage kopieren \ No newline at end of file diff --git a/Plugins/Wox.Plugin.Calculator/Languages/en.xaml b/Plugins/Wox.Plugin.Calculator/Languages/en.xaml index 833a0c14b..e354839e7 100644 --- a/Plugins/Wox.Plugin.Calculator/Languages/en.xaml +++ b/Plugins/Wox.Plugin.Calculator/Languages/en.xaml @@ -4,5 +4,7 @@ Calculator Allows to do mathematical calculations.(Try 5*3-2 in Wox) - + Not a Number (NaN) + Expression wrong or incomplete (Did you forget some Brackets?) + Copy this Number to the clipboard \ No newline at end of file diff --git a/Plugins/Wox.Plugin.Calculator/Main.cs b/Plugins/Wox.Plugin.Calculator/Main.cs index 42f592b18..cefb686b9 100644 --- a/Plugins/Wox.Plugin.Calculator/Main.cs +++ b/Plugins/Wox.Plugin.Calculator/Main.cs @@ -1,9 +1,9 @@ using System.Collections.Generic; -using System.Globalization; using System.Runtime.InteropServices; using System.Text.RegularExpressions; using System.Windows; -using YAMP; +using Mages; +using Mages.Core; namespace Wox.Plugin.Caculator { @@ -19,15 +19,12 @@ namespace Wox.Plugin.Caculator @"[ei]|[0-9]|[\+\-\*\/\^\., ""]|[\(\)\|\!\[\]]" + @")+$", RegexOptions.Compiled); private static Regex regBrackets = new Regex(@"[\(\)\[\]]", RegexOptions.Compiled); - private static ParseContext yampContext; + private static Mages.Core.Engine magesEngine; private PluginInitContext context { get; set; } - private NumberTranslator _numberTranslator; static Main() { - yampContext = Parser.PrimaryContext; - Parser.InteractiveMode = false; - Parser.UseScripting = false; + magesEngine = new Engine(); } public List Query(Query query) @@ -38,33 +35,38 @@ namespace Wox.Plugin.Caculator try { - var result = yampContext.Run(this._numberTranslator?.Translate(query.Search) ?? query.Search); - if (result.Output != null && !string.IsNullOrEmpty(result.Result)) + 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 (result != null && !string.IsNullOrEmpty(result.ToString())) { - string resultValue = this._numberTranslator?.TranslateBack(result.Result) ?? result.Result; return new List + { new Result { - new Result + Title = result.ToString(), + IcoPath = "Images/calculator.png", + Score = 300, + SubTitle = context.API.GetTranslation("wox_plugin_calculator_copy_number_to_clipboard"), + Action = c => { - Title = resultValue, - IcoPath = "Images/calculator.png", - Score = 300, - SubTitle = "Copy this number to the clipboard", - Action = c => + try { - try - { - Clipboard.SetText(resultValue); - return true; - } - catch (ExternalException e) - { - MessageBox.Show("Copy failed, please try later"); - return false; - } + Clipboard.SetText(result.ToString()); + return true; + } + catch (ExternalException e) + { + MessageBox.Show("Copy failed, please try later"); + return false; } } - }; + } }; } } catch @@ -95,7 +97,6 @@ namespace Wox.Plugin.Caculator public void Init(PluginInitContext context) { this.context = context; - this._numberTranslator = NumberTranslator.Create(CultureInfo.CurrentCulture, CultureInfo.InvariantCulture); } public string GetTranslatedPluginTitle() diff --git a/Plugins/Wox.Plugin.Calculator/Wox.Plugin.Calculator.csproj b/Plugins/Wox.Plugin.Calculator/Wox.Plugin.Calculator.csproj index c65590ea7..8fd3169a8 100644 --- a/Plugins/Wox.Plugin.Calculator/Wox.Plugin.Calculator.csproj +++ b/Plugins/Wox.Plugin.Calculator/Wox.Plugin.Calculator.csproj @@ -38,14 +38,14 @@ ..\..\packages\JetBrains.Annotations.10.3.0\lib\net\JetBrains.Annotations.dll True + + ..\..\packages\Mages.1.5.0\lib\net35\Mages.Core.dll + True + - - ..\..\packages\YAMP.1.4.0\lib\net35\YAMP.dll - True - diff --git a/Plugins/Wox.Plugin.Calculator/packages.config b/Plugins/Wox.Plugin.Calculator/packages.config index d1fce409f..c17f6726f 100644 --- a/Plugins/Wox.Plugin.Calculator/packages.config +++ b/Plugins/Wox.Plugin.Calculator/packages.config @@ -1,6 +1,6 @@  + - \ No newline at end of file From 742048f24e3413e31cd8a77bc51f2550229ea9da Mon Sep 17 00:00:00 2001 From: Michael Wirth Date: Mon, 16 Oct 2017 20:17:53 +0200 Subject: [PATCH 2/3] Fixed typos and swapped Brackets for parentheses, Corrections due to PR --- .../Wox.Plugin.Calculator/Languages/en.xaml | 6 +- Plugins/Wox.Plugin.Calculator/Main.cs | 67 ++++++++++--------- Wox.Infrastructure/Logger/Log.cs | 2 +- 3 files changed, 39 insertions(+), 36 deletions(-) diff --git a/Plugins/Wox.Plugin.Calculator/Languages/en.xaml b/Plugins/Wox.Plugin.Calculator/Languages/en.xaml index e354839e7..3b5fa32b3 100644 --- a/Plugins/Wox.Plugin.Calculator/Languages/en.xaml +++ b/Plugins/Wox.Plugin.Calculator/Languages/en.xaml @@ -4,7 +4,7 @@ Calculator Allows to do mathematical calculations.(Try 5*3-2 in Wox) - Not a Number (NaN) - Expression wrong or incomplete (Did you forget some Brackets?) - Copy this Number to the clipboard + Not a number (NaN) + Expression wrong or incomplete (Did you forget some parentheses?) + Copy this number to the clipboard \ No newline at end of file diff --git a/Plugins/Wox.Plugin.Calculator/Main.cs b/Plugins/Wox.Plugin.Calculator/Main.cs index cefb686b9..fab0b8e03 100644 --- a/Plugins/Wox.Plugin.Calculator/Main.cs +++ b/Plugins/Wox.Plugin.Calculator/Main.cs @@ -2,14 +2,13 @@ using System.Runtime.InteropServices; using System.Text.RegularExpressions; using System.Windows; -using Mages; using Mages.Core; namespace Wox.Plugin.Caculator { public class Main : IPlugin, IPluginI18n { - private static Regex regValidExpressChar = new Regex( + private static readonly Regex RegValidExpressChar = new Regex( @"^(" + @"ceil|floor|exp|pi|e|max|min|det|abs|log|ln|sqrt|" + @"sin|cos|tan|arcsin|arccos|arctan|" + @@ -18,66 +17,70 @@ namespace Wox.Plugin.Caculator @"==|~=|&&|\|\||" + @"[ei]|[0-9]|[\+\-\*\/\^\., ""]|[\(\)\|\!\[\]]" + @")+$", RegexOptions.Compiled); - private static Regex regBrackets = new Regex(@"[\(\)\[\]]", RegexOptions.Compiled); - private static Mages.Core.Engine magesEngine; - private PluginInitContext context { get; set; } + private static readonly Regex RegBrackets = new Regex(@"[\(\)\[\]]", RegexOptions.Compiled); + private static readonly Engine MagesEngine; + private PluginInitContext Context { get; set; } static Main() { - magesEngine = new Engine(); + MagesEngine = new Engine(); } public List Query(Query query) { if (query.Search.Length <= 2 // don't affect when user only input "e" or "i" keyword - || !regValidExpressChar.IsMatch(query.Search) + || !RegValidExpressChar.IsMatch(query.Search) || !IsBracketComplete(query.Search)) return new List(); try { - var result = magesEngine.Interpret(query.Search); + var result = MagesEngine.Interpret(query.Search); if (result.ToString() == "NaN") - result = context.API.GetTranslation("wox_plugin_calculator_not_a_number"); + result = Context.API.GetTranslation("wox_plugin_calculator_not_a_number"); if (result is Function) - result = context.API.GetTranslation("wox_plugin_calculator_expression_not_complete"); + result = Context.API.GetTranslation("wox_plugin_calculator_expression_not_complete"); - if (result != null && !string.IsNullOrEmpty(result.ToString())) + if (!string.IsNullOrEmpty(result?.ToString())) { return new List - { new Result { - Title = result.ToString(), - IcoPath = "Images/calculator.png", - Score = 300, - SubTitle = context.API.GetTranslation("wox_plugin_calculator_copy_number_to_clipboard"), - Action = c => + new Result { - try + Title = result.ToString(), + IcoPath = "Images/calculator.png", + Score = 300, + SubTitle = Context.API.GetTranslation("wox_plugin_calculator_copy_number_to_clipboard"), + Action = c => { - Clipboard.SetText(result.ToString()); - return true; + try + { + Clipboard.SetText(result.ToString()); + return true; + } + catch (ExternalException) + { + MessageBox.Show("Copy failed, please try later"); + return false; + } } - catch (ExternalException e) - { - MessageBox.Show("Copy failed, please try later"); - return false; - } - } - } }; + } + }; } } catch - { } + { + // ignored + } return new List(); } private bool IsBracketComplete(string query) { - var matchs = regBrackets.Matches(query); + var matchs = RegBrackets.Matches(query); var leftBracketCount = 0; foreach (Match match in matchs) { @@ -96,17 +99,17 @@ namespace Wox.Plugin.Caculator public void Init(PluginInitContext context) { - this.context = context; + Context = context; } public string GetTranslatedPluginTitle() { - return context.API.GetTranslation("wox_plugin_caculator_plugin_name"); + return Context.API.GetTranslation("wox_plugin_caculator_plugin_name"); } public string GetTranslatedPluginDescription() { - return context.API.GetTranslation("wox_plugin_caculator_plugin_description"); + return Context.API.GetTranslation("wox_plugin_caculator_plugin_description"); } } } diff --git a/Wox.Infrastructure/Logger/Log.cs b/Wox.Infrastructure/Logger/Log.cs index f7ea74b31..d02654978 100644 --- a/Wox.Infrastructure/Logger/Log.cs +++ b/Wox.Infrastructure/Logger/Log.cs @@ -72,7 +72,7 @@ namespace Wox.Infrastructure.Logger public static void Exception(string message, System.Exception e) { #if DEBUG - throw e; + //throw e; #else if (FormatValid(message)) { From cfe923b8c4447060bcde23b4283ff8f4851f4469 Mon Sep 17 00:00:00 2001 From: Michael Wirth Date: Tue, 17 Oct 2017 20:01:41 +0200 Subject: [PATCH 3/3] re-enabled exception during debug --- Wox.Infrastructure/Logger/Log.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Wox.Infrastructure/Logger/Log.cs b/Wox.Infrastructure/Logger/Log.cs index d02654978..f7ea74b31 100644 --- a/Wox.Infrastructure/Logger/Log.cs +++ b/Wox.Infrastructure/Logger/Log.cs @@ -72,7 +72,7 @@ namespace Wox.Infrastructure.Logger public static void Exception(string message, System.Exception e) { #if DEBUG - //throw e; + throw e; #else if (FormatValid(message)) {