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

https://github.com/FlorianRappl/Mages
fixes also #1022
This commit is contained in:
Michael Wirth
2017-10-15 20:21:23 +02:00
parent e20247d055
commit 36111aa001
5 changed files with 40 additions and 35 deletions

View File

@@ -4,5 +4,7 @@
<system:String x:Key="wox_plugin_caculator_plugin_name">Rechner</system:String> <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_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> </ResourceDictionary>

View File

@@ -4,5 +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_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>
</ResourceDictionary> </ResourceDictionary>

View File

@@ -1,9 +1,9 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Windows; using System.Windows;
using YAMP; using Mages;
using Mages.Core;
namespace Wox.Plugin.Caculator namespace Wox.Plugin.Caculator
{ {
@@ -19,15 +19,12 @@ namespace Wox.Plugin.Caculator
@"[ei]|[0-9]|[\+\-\*\/\^\., ""]|[\(\)\|\!\[\]]" + @"[ei]|[0-9]|[\+\-\*\/\^\., ""]|[\(\)\|\!\[\]]" +
@")+$", RegexOptions.Compiled); @")+$", RegexOptions.Compiled);
private static Regex regBrackets = new Regex(@"[\(\)\[\]]", 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 PluginInitContext context { get; set; }
private NumberTranslator _numberTranslator;
static Main() static Main()
{ {
yampContext = Parser.PrimaryContext; magesEngine = new Engine();
Parser.InteractiveMode = false;
Parser.UseScripting = false;
} }
public List<Result> Query(Query query) public List<Result> Query(Query query)
@@ -38,33 +35,38 @@ namespace Wox.Plugin.Caculator
try try
{ {
var result = yampContext.Run(this._numberTranslator?.Translate(query.Search) ?? query.Search); var result = magesEngine.Interpret(query.Search);
if (result.Output != null && !string.IsNullOrEmpty(result.Result))
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<Result> return new List<Result>
{ 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, try
IcoPath = "Images/calculator.png",
Score = 300,
SubTitle = "Copy this number to the clipboard",
Action = c =>
{ {
try Clipboard.SetText(result.ToString());
{ return true;
Clipboard.SetText(resultValue); }
return true; catch (ExternalException e)
} {
catch (ExternalException e) MessageBox.Show("Copy failed, please try later");
{ return false;
MessageBox.Show("Copy failed, please try later");
return false;
}
} }
} }
}; } };
} }
} }
catch catch
@@ -95,7 +97,6 @@ namespace Wox.Plugin.Caculator
public void Init(PluginInitContext context) public void Init(PluginInitContext context)
{ {
this.context = context; this.context = context;
this._numberTranslator = NumberTranslator.Create(CultureInfo.CurrentCulture, CultureInfo.InvariantCulture);
} }
public string GetTranslatedPluginTitle() public string GetTranslatedPluginTitle()

View File

@@ -38,14 +38,14 @@
<HintPath>..\..\packages\JetBrains.Annotations.10.3.0\lib\net\JetBrains.Annotations.dll</HintPath> <HintPath>..\..\packages\JetBrains.Annotations.10.3.0\lib\net\JetBrains.Annotations.dll</HintPath>
<Private>True</Private> <Private>True</Private>
</Reference> </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="PresentationCore" />
<Reference Include="PresentationFramework" /> <Reference Include="PresentationFramework" />
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <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>
<ItemGroup> <ItemGroup>
<Compile Include="..\..\SolutionAssemblyInfo.cs"> <Compile Include="..\..\SolutionAssemblyInfo.cs">

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="JetBrains.Annotations" version="10.3.0" targetFramework="net452" /> <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="System.Runtime" version="4.0.0" targetFramework="net452" />
<package id="YAMP" version="1.4.0" targetFramework="net452" />
</packages> </packages>