Fixed typos and swapped Brackets for parentheses, Corrections due to PR

This commit is contained in:
Michael Wirth
2017-10-16 20:17:53 +02:00
parent 36111aa001
commit 742048f24e
3 changed files with 39 additions and 36 deletions

View File

@@ -4,7 +4,7 @@
<system:String x:Key="wox_plugin_caculator_plugin_name">Calculator</system:String> <system:String x:Key="wox_plugin_caculator_plugin_name">Calculator</system:String>
<system:String x:Key="wox_plugin_caculator_plugin_description">Allows to do mathematical calculations.(Try 5*3-2 in Wox)</system:String> <system:String x:Key="wox_plugin_caculator_plugin_description">Allows to do mathematical calculations.(Try 5*3-2 in Wox)</system:String>
<system:String x:Key="wox_plugin_calculator_not_a_number">Not a Number (NaN)</system:String> <system:String x:Key="wox_plugin_calculator_not_a_number">Not a number (NaN)</system:String>
<system:String x:Key="wox_plugin_calculator_expression_not_complete">Expression wrong or incomplete (Did you forget some Brackets?)</system:String> <system:String x:Key="wox_plugin_calculator_expression_not_complete">Expression wrong or incomplete (Did you forget some parentheses?)</system:String>
<system:String x:Key="wox_plugin_calculator_copy_number_to_clipboard">Copy this Number to the clipboard</system:String> <system:String x:Key="wox_plugin_calculator_copy_number_to_clipboard">Copy this number to the clipboard</system:String>
</ResourceDictionary> </ResourceDictionary>

View File

@@ -2,14 +2,13 @@
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Windows; using System.Windows;
using Mages;
using Mages.Core; using Mages.Core;
namespace Wox.Plugin.Caculator namespace Wox.Plugin.Caculator
{ {
public class Main : IPlugin, IPluginI18n 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|" + @"ceil|floor|exp|pi|e|max|min|det|abs|log|ln|sqrt|" +
@"sin|cos|tan|arcsin|arccos|arctan|" + @"sin|cos|tan|arcsin|arccos|arctan|" +
@@ -18,66 +17,70 @@ namespace Wox.Plugin.Caculator
@"==|~=|&&|\|\||" + @"==|~=|&&|\|\||" +
@"[ei]|[0-9]|[\+\-\*\/\^\., ""]|[\(\)\|\!\[\]]" + @"[ei]|[0-9]|[\+\-\*\/\^\., ""]|[\(\)\|\!\[\]]" +
@")+$", RegexOptions.Compiled); @")+$", RegexOptions.Compiled);
private static Regex regBrackets = new Regex(@"[\(\)\[\]]", RegexOptions.Compiled); private static readonly Regex RegBrackets = new Regex(@"[\(\)\[\]]", RegexOptions.Compiled);
private static Mages.Core.Engine magesEngine; private static readonly Engine MagesEngine;
private PluginInitContext context { get; set; } private PluginInitContext Context { get; set; }
static Main() static Main()
{ {
magesEngine = new Engine(); MagesEngine = new Engine();
} }
public List<Result> Query(Query query) public List<Result> Query(Query query)
{ {
if (query.Search.Length <= 2 // don't affect when user only input "e" or "i" keyword 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<Result>(); || !IsBracketComplete(query.Search)) return new List<Result>();
try try
{ {
var result = magesEngine.Interpret(query.Search); var result = MagesEngine.Interpret(query.Search);
if (result.ToString() == "NaN") 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) 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<Result> return new List<Result>
{ new Result
{ {
Title = result.ToString(), new Result
IcoPath = "Images/calculator.png",
Score = 300,
SubTitle = context.API.GetTranslation("wox_plugin_calculator_copy_number_to_clipboard"),
Action = c =>
{ {
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()); try
return true; {
} Clipboard.SetText(result.ToString());
catch (ExternalException e) return true;
{ }
MessageBox.Show("Copy failed, please try later"); catch (ExternalException)
return false; {
MessageBox.Show("Copy failed, please try later");
return false;
}
} }
} }
} }; };
} }
} }
catch catch
{ } {
// ignored
}
return new List<Result>(); return new List<Result>();
} }
private bool IsBracketComplete(string query) private bool IsBracketComplete(string query)
{ {
var matchs = regBrackets.Matches(query); var matchs = RegBrackets.Matches(query);
var leftBracketCount = 0; var leftBracketCount = 0;
foreach (Match match in matchs) foreach (Match match in matchs)
{ {
@@ -96,17 +99,17 @@ namespace Wox.Plugin.Caculator
public void Init(PluginInitContext context) public void Init(PluginInitContext context)
{ {
this.context = context; Context = context;
} }
public string GetTranslatedPluginTitle() public string GetTranslatedPluginTitle()
{ {
return context.API.GetTranslation("wox_plugin_caculator_plugin_name"); return Context.API.GetTranslation("wox_plugin_caculator_plugin_name");
} }
public string GetTranslatedPluginDescription() public string GetTranslatedPluginDescription()
{ {
return context.API.GetTranslation("wox_plugin_caculator_plugin_description"); return Context.API.GetTranslation("wox_plugin_caculator_plugin_description");
} }
} }
} }

View File

@@ -72,7 +72,7 @@ namespace Wox.Infrastructure.Logger
public static void Exception(string message, System.Exception e) public static void Exception(string message, System.Exception e)
{ {
#if DEBUG #if DEBUG
throw e; //throw e;
#else #else
if (FormatValid(message)) if (FormatValid(message))
{ {