Skip to content
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
26 changes: 15 additions & 11 deletions StreamDeckSimHub.Plugin/PropertyLogic/NCalcFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// LGPL-3.0-or-later (see file COPYING and COPYING.LESSER)

using System.Globalization;
using NCalc;
using NCalc.Exceptions;
using NCalc.Handlers;

namespace StreamDeckSimHub.Plugin.PropertyLogic;

Expand All @@ -13,39 +13,43 @@ namespace StreamDeckSimHub.Plugin.PropertyLogic;
public abstract class NCalcFunctions
{

public static object? StrFunction(ExpressionFunctionData args)
public static object? StrFunction(FunctionData args)
{
if (args.Count() != 1)
if (args.Count != 1)
{
throw new NCalcParserException("Error parsing the expression.",
new NCalcParserException("The 'str' function requires exactly one argument."));
}

return args[0].Evaluate()?.ToString() ?? string.Empty;
return args.Evaluate(0)?.ToString() ?? string.Empty;
}

public static object? IntFunction(ExpressionFunctionData args)
public static object? IntFunction(FunctionData args)
{
if (args.Count() != 1)
if (args.Count != 1)
{
throw new NCalcParserException("Error parsing the expression.",
new NCalcParserException("The 'int' function requires exactly one argument."));
}

var value = args[0].Evaluate();
var value = args.Evaluate(0);
return value is int intValue ? intValue : Convert.ToInt32(value);
}

public static object? FormatFunction(ExpressionFunctionData args)
public static object? FormatFunction(FunctionData args)
{
if (args.Count() < 2)
if (args.Count < 2)
{
throw new NCalcParserException("Error parsing the expression.",
new NCalcParserException("The 'format' function requires at least two arguments."));
}

var format = args[0].Evaluate()?.ToString() ?? string.Empty;
var parameters = args.Skip(1).Select(arg => arg.Evaluate()).ToArray();
var format = args.Evaluate(0)?.ToString() ?? string.Empty;
var parameters = new object[args.Count - 1];
for (var i = 1; i < args.Count; i++)
{
parameters[i - 1] = args.Evaluate(i) ?? string.Empty;
}
try
{
return string.Format(CultureInfo.CurrentCulture, format, parameters);
Expand Down
2 changes: 1 addition & 1 deletion StreamDeckSimHub.Plugin/StreamDeckSimHub.Plugin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.15" />
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="2.1.4" />
<PackageReference Include="Svg.Skia" Version="5.2.3" />
<PackageReference Include="NCalcSync" Version="5.4.2" />
<PackageReference Include="NCalcSync" Version="7.2.0" />
<PackageReference Include="TestableIO.System.IO.Abstractions.Wrappers" Version="21.0.29" />
<Reference Include="SharpDeck">
<HintPath>..\lib\SharpDeck.dll</HintPath>
Expand Down
6 changes: 3 additions & 3 deletions doc/generic-button/GenericButton.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -473,9 +473,9 @@ The usage of NCalc expressions is another key feature of the Generic Button acti

Please refer to the NCalc documentation for details about the syntax of NCalc expressions:

* https://ncalc.github.io/ncalc/articles/functions.html
* https://ncalc.github.io/ncalc/articles/operators.html
* https://ncalc.github.io/ncalc/articles/values.html
* https://ncalc.gumbarros.com.br/articles/language/functions.html
* https://ncalc.gumbarros.com.br/articles/language/operators.html
* https://ncalc.gumbarros.com.br/articles/language/values.html

SimHub properties can be used by putting their name into square brackets, e.g.

Expand Down
Loading