mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-16 03:37:59 +01:00
add Calculator system plugin
This commit is contained in:
91
Wox.Plugin.System/Calculator.cs
Normal file
91
Wox.Plugin.System/Calculator.cs
Normal file
@@ -0,0 +1,91 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Windows.Forms;
|
||||
using YAMP;
|
||||
|
||||
namespace Wox.Plugin.System
|
||||
{
|
||||
public class Calculator : BaseSystemPlugin
|
||||
{
|
||||
private static Regex regValidExpressChar = new Regex(
|
||||
@"^(" +
|
||||
@"sin|cos|ceil|floor|exp|pi|max|min|det|arccos|abs|" +
|
||||
@"eigval|eigvec|eig|sum|polar|plot|round|sort|real|zeta|" +
|
||||
@"bin2dec|hex2dec|oct2dec|" +
|
||||
@"==|~=|&&|\|\||" +
|
||||
@"[ei]|[0-9]|[\+\-\*\/\^\., ""]|[\(\)\|\!\[\]]" +
|
||||
@")+$", RegexOptions.Compiled);
|
||||
private static Regex regBrackets = new Regex(@"[\(\)\[\]]", RegexOptions.Compiled);
|
||||
private static ParseContext yampContext = null;
|
||||
private PluginInitContext context { get; set; }
|
||||
|
||||
static Calculator()
|
||||
{
|
||||
yampContext = Parser.PrimaryContext;
|
||||
Parser.InteractiveMode = false;
|
||||
Parser.UseScripting = false;
|
||||
}
|
||||
|
||||
protected override List<Result> QueryInternal(Query query)
|
||||
{
|
||||
if (string.IsNullOrEmpty(query.RawQuery)
|
||||
|| query.RawQuery.Length < 2 // don't affect when user only input "e" or "i" keyword
|
||||
|| !regValidExpressChar.IsMatch(query.RawQuery)
|
||||
|| !IsBracketComplete(query.RawQuery)) return new List<Result>();
|
||||
|
||||
try
|
||||
{
|
||||
var result = yampContext.Run(query.RawQuery);
|
||||
if (result.Output != null && !string.IsNullOrEmpty(result.Result))
|
||||
{
|
||||
return new List<Result>() { new Result() {
|
||||
Title = result.Result,
|
||||
IcoPath = "Images/calculator.png",
|
||||
Score = 300,
|
||||
SubTitle = "Copy this number to the clipboard",
|
||||
Action = (c) =>
|
||||
{
|
||||
Clipboard.SetText(result.Result);
|
||||
return true;
|
||||
}
|
||||
} };
|
||||
}
|
||||
}
|
||||
catch
|
||||
{}
|
||||
|
||||
return new List<Result>();
|
||||
}
|
||||
|
||||
private bool IsBracketComplete(string query)
|
||||
{
|
||||
var matchs = regBrackets.Matches(query);
|
||||
var leftBracketCount = 0;
|
||||
foreach (Match match in matchs)
|
||||
{
|
||||
if (match.Value == "(" || match.Value == "[")
|
||||
{
|
||||
leftBracketCount++;
|
||||
}
|
||||
else
|
||||
{
|
||||
leftBracketCount--;
|
||||
}
|
||||
}
|
||||
|
||||
return leftBracketCount == 0;
|
||||
}
|
||||
|
||||
protected override void InitInternal(PluginInitContext context)
|
||||
{
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -35,6 +35,9 @@
|
||||
<Reference Include="Newtonsoft.Json">
|
||||
<HintPath>..\packages\Newtonsoft.Json.5.0.8\lib\net35\Newtonsoft.Json.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="YAMP">
|
||||
<HintPath>..\packages\YAMP.1.3.0\lib\net35\YAMP.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Drawing" />
|
||||
@@ -47,6 +50,7 @@
|
||||
<ItemGroup>
|
||||
<Compile Include="BaseSystemPlugin.cs" />
|
||||
<Compile Include="BrowserBookmarks.cs" />
|
||||
<Compile Include="Calculator.cs" />
|
||||
<Compile Include="WebSearchPlugin.cs" />
|
||||
<Compile Include="WindowsShellRun.cs" />
|
||||
<Compile Include="CMD.cs" />
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="Newtonsoft.Json" version="5.0.8" targetFramework="net35" />
|
||||
<package id="YAMP" version="1.3.0" targetFramework="net35" />
|
||||
</packages>
|
||||
BIN
Wox/Images/calculator.png
Normal file
BIN
Wox/Images/calculator.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.6 KiB |
@@ -307,6 +307,9 @@
|
||||
<Resource Include="Images\work.png">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Resource>
|
||||
<Resource Include="Images\calculator.png">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Resource>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<COMReference Include="IWshRuntimeLibrary">
|
||||
|
||||
Reference in New Issue
Block a user