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 all commits
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
4 changes: 3 additions & 1 deletion Plugins/Wox.Plugin.Calculator/Languages/de.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@

<system:String x:Key="wox_plugin_caculator_plugin_name">Rechner</system:String>
<system:String x:Key="wox_plugin_caculator_plugin_description">Stellt mathematische Berechnungen bereit.(Versuche 5*3-2 in Wox)</system:String>

<system:String x:Key="wox_plugin_calculator_not_a_number">Keine Zahl (NaN)</system:String>
<system:String x:Key="wox_plugin_calculator_expression_not_complete">Ausdruck falsch oder nicht vollständig (Klammern vergessen?)</system:String>
<system:String x:Key="wox_plugin_calculator_copy_number_to_clipboard">Diese Zahl in die Zwischenablage kopieren</system:String>
</ResourceDictionary>
4 changes: 3 additions & 1 deletion Plugins/Wox.Plugin.Calculator/Languages/en.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +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 parentheses?)</system:String>
<system:String x:Key="wox_plugin_calculator_copy_number_to_clipboard">Copy this number to the clipboard</system:String>
</ResourceDictionary>
54 changes: 29 additions & 25 deletions Plugins/Wox.Plugin.Calculator/Main.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
using System.Collections.Generic;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Windows;
using YAMP;
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,64 +17,70 @@ public class Main : IPlugin, IPluginI18n
@"==|~=|&&|\|\||" +
@"[ei]|[0-9]|[\+\-\*\/\^\., ""]|[\(\)\|\!\[\]]" +
@")+$", RegexOptions.Compiled);
private static Regex regBrackets = new Regex(@"[\(\)\[\]]", RegexOptions.Compiled);
private static ParseContext yampContext;
private PluginInitContext context { get; set; }
private NumberTranslator _numberTranslator;
private static readonly Regex RegBrackets = new Regex(@"[\(\)\[\]]", RegexOptions.Compiled);
private static readonly Engine MagesEngine;
private PluginInitContext Context { get; set; }

static Main()
{
yampContext = Parser.PrimaryContext;
Parser.InteractiveMode = false;
Parser.UseScripting = false;
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 = 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 (!string.IsNullOrEmpty(result?.ToString()))
{
string resultValue = this._numberTranslator?.TranslateBack(result.Result) ?? result.Result;
return new List<Result>
{
new Result
{
Title = resultValue,
Title = result.ToString(),
IcoPath = "Images/calculator.png",
Score = 300,
SubTitle = "Copy this number to the clipboard",
SubTitle = Context.API.GetTranslation("wox_plugin_calculator_copy_number_to_clipboard"),
Action = c =>
{
try
{
Clipboard.SetText(resultValue);
Clipboard.SetText(result.ToString());
return true;
}
catch (ExternalException e)
catch (ExternalException)
{
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 @@ -94,18 +99,17 @@ private bool IsBracketComplete(string query)

public void Init(PluginInitContext context)
{
this.context = context;
this._numberTranslator = NumberTranslator.Create(CultureInfo.CurrentCulture, CultureInfo.InvariantCulture);
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");
}
}
}
8 changes: 4 additions & 4 deletions Plugins/Wox.Plugin.Calculator/Wox.Plugin.Calculator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@
<HintPath>..\..\packages\JetBrains.Annotations.10.3.0\lib\net\JetBrains.Annotations.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Mages.Core, Version=1.5.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\Mages.1.5.0\lib\net35\Mages.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="YAMP, Version=1.4.0.22422, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\YAMP.1.4.0\lib\net35\YAMP.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\SolutionAssemblyInfo.cs">
Expand Down
2 changes: 1 addition & 1 deletion Plugins/Wox.Plugin.Calculator/packages.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="JetBrains.Annotations" version="10.3.0" targetFramework="net452" />
<package id="Mages" version="1.5.0" targetFramework="net452" />
<package id="System.Runtime" version="4.0.0" targetFramework="net452" />
<package id="YAMP" version="1.4.0" targetFramework="net452" />
</packages>