Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

since Yamp is not maintained anymore, i switched it for Mages #1628

Merged
merged 3 commits into from
Oct 21, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Plugins/Wox.Plugin.Calculator/Languages/en.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<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_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_copy_number_to_clipboard">Copy this Number to the clipboard</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 parentheses?)</system:String>
<system:String x:Key="wox_plugin_calculator_copy_number_to_clipboard">Copy this number to the clipboard</system:String>
</ResourceDictionary>
67 changes: 35 additions & 32 deletions Plugins/Wox.Plugin.Calculator/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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|" +
Expand All @@ -18,66 +17,70 @@ public class Main : IPlugin, IPluginI18n
@"==|~=|&&|\|\||" +
@"[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<Result> 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<Result>();

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<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 =>
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<Result>();
}

private bool IsBracketComplete(string query)
{
var matchs = regBrackets.Matches(query);
var matchs = RegBrackets.Matches(query);
var leftBracketCount = 0;
foreach (Match match in matchs)
{
Expand All @@ -96,17 +99,17 @@ private bool IsBracketComplete(string query)

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");
}
}
}
2 changes: 1 addition & 1 deletion Wox.Infrastructure/Logger/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static void Error(string message)
public static void Exception(string message, System.Exception e)
{
#if DEBUG
throw e;
//throw e;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lack of necessary diligence :/

while debugging i got this exception:

0x80070005 - Unspecified error
'Microsoft Corporation' violates pattern constraint of '\bms-resource:.{1,256}'.

so i commented out, tested and forgot to re-enable it

#else
if (FormatValid(message))
{
Expand Down