2022-11-25 04:00:37 +00:00
|
|
|
|
using System;
|
2020-03-31 11:00:09 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Globalization;
|
2025-07-22 22:48:11 +00:00
|
|
|
|
using System.Linq;
|
2016-01-06 21:34:42 +00:00
|
|
|
|
using System.Runtime.InteropServices;
|
2014-03-17 16:43:20 +00:00
|
|
|
|
using System.Text.RegularExpressions;
|
2020-03-31 11:00:09 +00:00
|
|
|
|
using System.Windows.Controls;
|
2017-10-15 18:21:23 +00:00
|
|
|
|
using Mages.Core;
|
2024-05-25 06:35:14 +00:00
|
|
|
|
using Flow.Launcher.Plugin.Calculator.Views;
|
2025-07-22 15:48:39 +00:00
|
|
|
|
using Flow.Launcher.Plugin.Calculator.ViewModels;
|
2014-03-17 16:43:20 +00:00
|
|
|
|
|
2024-05-25 06:35:14 +00:00
|
|
|
|
namespace Flow.Launcher.Plugin.Calculator
|
2014-03-17 16:43:20 +00:00
|
|
|
|
{
|
2021-05-11 16:37:28 +00:00
|
|
|
|
public class Main : IPlugin, IPluginI18n, ISettingProvider
|
2014-03-17 16:43:20 +00:00
|
|
|
|
{
|
2025-07-22 15:59:33 +00:00
|
|
|
|
private static readonly Regex RegValidExpressChar = MainRegexHelper.GetRegValidExpressChar();
|
|
|
|
|
|
private static readonly Regex RegBrackets = MainRegexHelper.GetRegBrackets();
|
2025-07-27 10:39:38 +00:00
|
|
|
|
private static readonly Regex ThousandGroupRegex = MainRegexHelper.GetThousandGroupRegex();
|
|
|
|
|
|
private static readonly Regex NumberRegex = MainRegexHelper.GetNumberRegex();
|
2025-07-27 06:43:31 +00:00
|
|
|
|
|
2021-03-20 10:02:35 +00:00
|
|
|
|
private static Engine MagesEngine;
|
2025-06-23 04:25:16 +00:00
|
|
|
|
private const string Comma = ",";
|
|
|
|
|
|
private const string Dot = ".";
|
2022-04-24 09:33:06 +00:00
|
|
|
|
|
2025-06-23 04:25:16 +00:00
|
|
|
|
internal static PluginInitContext Context { get; set; } = null!;
|
2014-03-17 16:43:20 +00:00
|
|
|
|
|
2025-07-22 15:48:39 +00:00
|
|
|
|
private Settings _settings;
|
|
|
|
|
|
private SettingsViewModel _viewModel;
|
2020-03-31 11:00:09 +00:00
|
|
|
|
|
2025-07-23 00:18:26 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Holds the formatting information for a single query.
|
|
|
|
|
|
/// This is used to ensure thread safety by keeping query state local.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private class ParsingContext
|
|
|
|
|
|
{
|
|
|
|
|
|
public string InputDecimalSeparator { get; set; }
|
|
|
|
|
|
public bool InputUsesGroupSeparators { get; set; }
|
|
|
|
|
|
}
|
2025-07-22 22:48:11 +00:00
|
|
|
|
|
2020-03-31 11:00:09 +00:00
|
|
|
|
public void Init(PluginInitContext context)
|
|
|
|
|
|
{
|
|
|
|
|
|
Context = context;
|
2021-05-13 11:29:21 +00:00
|
|
|
|
_settings = context.API.LoadSettingJsonStorage<Settings>();
|
2025-07-22 15:48:39 +00:00
|
|
|
|
_viewModel = new SettingsViewModel(_settings);
|
2022-03-31 18:53:07 +00:00
|
|
|
|
|
2021-03-18 03:39:13 +00:00
|
|
|
|
MagesEngine = new Engine(new Configuration
|
|
|
|
|
|
{
|
|
|
|
|
|
Scope = new Dictionary<string, object>
|
|
|
|
|
|
{
|
|
|
|
|
|
{ "e", Math.E }, // e is not contained in the default mages engine
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
2020-03-31 11:00:09 +00:00
|
|
|
|
}
|
2014-03-17 16:43:20 +00:00
|
|
|
|
|
2015-01-03 07:20:34 +00:00
|
|
|
|
public List<Result> Query(Query query)
|
2014-03-17 16:43:20 +00:00
|
|
|
|
{
|
2020-03-31 11:00:09 +00:00
|
|
|
|
if (!CanCalculate(query))
|
|
|
|
|
|
{
|
|
|
|
|
|
return new List<Result>();
|
|
|
|
|
|
}
|
2014-03-17 16:43:20 +00:00
|
|
|
|
|
2025-07-23 00:18:26 +00:00
|
|
|
|
var context = new ParsingContext();
|
2025-07-22 22:48:11 +00:00
|
|
|
|
|
2014-03-17 16:43:20 +00:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-07-23 00:18:26 +00:00
|
|
|
|
var expression = NumberRegex.Replace(query.Search, m => NormalizeNumber(m.Value, context));
|
2022-03-31 18:53:07 +00:00
|
|
|
|
|
2020-03-31 11:00:09 +00:00
|
|
|
|
var result = MagesEngine.Interpret(expression);
|
2017-10-15 18:21:23 +00:00
|
|
|
|
|
2021-02-26 08:20:47 +00:00
|
|
|
|
if (result?.ToString() == "NaN")
|
2025-06-23 04:25:16 +00:00
|
|
|
|
result = Localize.flowlauncher_plugin_calculator_not_a_number();
|
2017-10-15 18:21:23 +00:00
|
|
|
|
|
|
|
|
|
|
if (result is Function)
|
2025-06-23 04:25:16 +00:00
|
|
|
|
result = Localize.flowlauncher_plugin_calculator_expression_not_complete();
|
2017-10-15 18:21:23 +00:00
|
|
|
|
|
2017-10-16 18:17:53 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(result?.ToString()))
|
2014-03-17 16:43:20 +00:00
|
|
|
|
{
|
2020-03-31 11:00:09 +00:00
|
|
|
|
decimal roundedResult = Math.Round(Convert.ToDecimal(result), _settings.MaxDecimalPlaces, MidpointRounding.AwayFromZero);
|
2025-07-23 00:18:26 +00:00
|
|
|
|
string newResult = FormatResult(roundedResult, context);
|
2020-03-31 11:00:09 +00:00
|
|
|
|
|
2016-01-06 21:34:42 +00:00
|
|
|
|
return new List<Result>
|
2017-04-10 21:43:23 +00:00
|
|
|
|
{
|
2017-10-16 18:17:53 +00:00
|
|
|
|
new Result
|
2014-03-17 16:43:20 +00:00
|
|
|
|
{
|
2020-03-31 11:00:09 +00:00
|
|
|
|
Title = newResult,
|
2017-10-16 18:17:53 +00:00
|
|
|
|
IcoPath = "Images/calculator.png",
|
|
|
|
|
|
Score = 300,
|
2025-06-23 04:25:16 +00:00
|
|
|
|
SubTitle = Localize.flowlauncher_plugin_calculator_copy_number_to_clipboard(),
|
2022-03-31 18:53:07 +00:00
|
|
|
|
CopyText = newResult,
|
2017-10-16 18:17:53 +00:00
|
|
|
|
Action = c =>
|
2017-10-15 18:21:23 +00:00
|
|
|
|
{
|
2017-10-16 18:17:53 +00:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2023-06-08 12:46:37 +00:00
|
|
|
|
Context.API.CopyToClipboard(newResult);
|
2017-10-16 18:17:53 +00:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2022-08-08 04:31:38 +00:00
|
|
|
|
catch (ExternalException)
|
2017-10-16 18:17:53 +00:00
|
|
|
|
{
|
2025-07-22 16:04:52 +00:00
|
|
|
|
Context.API.ShowMsgBox(Localize.flowlauncher_plugin_calculator_failed_to_copy());
|
2017-10-16 18:17:53 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2017-10-15 18:21:23 +00:00
|
|
|
|
}
|
2020-10-13 06:01:26 +00:00
|
|
|
|
}
|
2017-10-16 18:17:53 +00:00
|
|
|
|
};
|
2014-03-17 16:43:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-01-05 08:11:38 +00:00
|
|
|
|
catch (Exception)
|
2017-10-16 18:17:53 +00:00
|
|
|
|
{
|
|
|
|
|
|
// ignored
|
|
|
|
|
|
}
|
2014-03-17 16:43:20 +00:00
|
|
|
|
|
|
|
|
|
|
return new List<Result>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-22 22:48:11 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Parses a string representation of a number, detecting its format. It uses structural analysis
|
2025-07-23 03:27:19 +00:00
|
|
|
|
/// and falls back to system culture for truly ambiguous cases (e.g., "1,234").
|
2025-07-23 00:18:26 +00:00
|
|
|
|
/// It populates the provided ParsingContext with the detected format for later use.
|
2025-07-22 22:48:11 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>A normalized number string with '.' as the decimal separator for the Mages engine.</returns>
|
2025-07-23 00:18:26 +00:00
|
|
|
|
private string NormalizeNumber(string numberStr, ParsingContext context)
|
2020-03-31 11:00:09 +00:00
|
|
|
|
{
|
2025-07-23 03:27:19 +00:00
|
|
|
|
var systemGroupSep = CultureInfo.CurrentCulture.NumberFormat.NumberGroupSeparator;
|
|
|
|
|
|
int dotCount = numberStr.Count(f => f == '.');
|
|
|
|
|
|
int commaCount = numberStr.Count(f => f == ',');
|
2025-07-22 22:48:11 +00:00
|
|
|
|
|
2025-07-23 03:27:19 +00:00
|
|
|
|
// Case 1: Unambiguous mixed separators (e.g., "1.234,56")
|
|
|
|
|
|
if (dotCount > 0 && commaCount > 0)
|
2020-03-31 11:00:09 +00:00
|
|
|
|
{
|
2025-07-23 00:18:26 +00:00
|
|
|
|
context.InputUsesGroupSeparators = true;
|
2025-07-23 03:27:19 +00:00
|
|
|
|
if (numberStr.LastIndexOf('.') > numberStr.LastIndexOf(','))
|
2025-07-22 22:48:11 +00:00
|
|
|
|
{
|
2025-07-27 06:43:31 +00:00
|
|
|
|
context.InputDecimalSeparator = Dot;
|
|
|
|
|
|
return numberStr.Replace(Comma, string.Empty);
|
2025-07-22 22:48:11 +00:00
|
|
|
|
}
|
2025-07-23 03:27:19 +00:00
|
|
|
|
else
|
2025-07-22 22:48:11 +00:00
|
|
|
|
{
|
2025-07-27 06:43:31 +00:00
|
|
|
|
context.InputDecimalSeparator = Comma;
|
|
|
|
|
|
return numberStr.Replace(Dot, string.Empty).Replace(Comma, Dot);
|
2025-07-22 22:48:11 +00:00
|
|
|
|
}
|
2020-03-31 11:00:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-23 03:27:19 +00:00
|
|
|
|
// Case 2: Only dots
|
|
|
|
|
|
if (dotCount > 0)
|
2020-03-31 11:00:09 +00:00
|
|
|
|
{
|
2025-07-23 03:27:19 +00:00
|
|
|
|
if (dotCount > 1)
|
2025-07-22 22:48:11 +00:00
|
|
|
|
{
|
2025-07-23 03:27:19 +00:00
|
|
|
|
context.InputUsesGroupSeparators = true;
|
2025-07-27 06:43:31 +00:00
|
|
|
|
return numberStr.Replace(Dot, string.Empty);
|
2025-07-23 03:27:19 +00:00
|
|
|
|
}
|
2025-07-27 06:43:31 +00:00
|
|
|
|
// A number is ambiguous if it has a single Dot in the thousands position,
|
2025-07-23 03:27:19 +00:00
|
|
|
|
// and does not start with a "0." or "."
|
|
|
|
|
|
bool isAmbiguous = numberStr.Length - numberStr.LastIndexOf('.') == 4
|
|
|
|
|
|
&& !numberStr.StartsWith("0.")
|
|
|
|
|
|
&& !numberStr.StartsWith(".");
|
|
|
|
|
|
if (isAmbiguous)
|
|
|
|
|
|
{
|
2025-07-27 06:43:31 +00:00
|
|
|
|
if (systemGroupSep == Dot)
|
2025-07-22 22:48:11 +00:00
|
|
|
|
{
|
2025-07-23 03:27:19 +00:00
|
|
|
|
context.InputUsesGroupSeparators = true;
|
2025-07-27 06:43:31 +00:00
|
|
|
|
return numberStr.Replace(Dot, string.Empty);
|
2025-07-22 22:48:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-07-27 06:43:31 +00:00
|
|
|
|
context.InputDecimalSeparator = Dot;
|
2025-07-23 03:27:19 +00:00
|
|
|
|
return numberStr;
|
2025-07-22 22:48:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-07-23 03:27:19 +00:00
|
|
|
|
else // Unambiguous decimal (e.g., "12.34" or "0.123" or ".123")
|
2025-07-22 22:48:11 +00:00
|
|
|
|
{
|
2025-07-27 06:43:31 +00:00
|
|
|
|
context.InputDecimalSeparator = Dot;
|
2025-07-23 03:27:19 +00:00
|
|
|
|
return numberStr;
|
2025-07-22 22:48:11 +00:00
|
|
|
|
}
|
2020-03-31 11:00:09 +00:00
|
|
|
|
}
|
2020-10-13 06:01:26 +00:00
|
|
|
|
|
2025-07-23 03:27:19 +00:00
|
|
|
|
// Case 3: Only commas
|
|
|
|
|
|
if (commaCount > 0)
|
2020-03-31 11:00:09 +00:00
|
|
|
|
{
|
2025-07-23 03:27:19 +00:00
|
|
|
|
if (commaCount > 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
context.InputUsesGroupSeparators = true;
|
2025-07-27 06:43:31 +00:00
|
|
|
|
return numberStr.Replace(Comma, string.Empty);
|
2025-07-23 03:27:19 +00:00
|
|
|
|
}
|
2025-07-27 06:43:31 +00:00
|
|
|
|
// A number is ambiguous if it has a single Comma in the thousands position,
|
2025-07-23 03:27:19 +00:00
|
|
|
|
// and does not start with a "0," or ","
|
|
|
|
|
|
bool isAmbiguous = numberStr.Length - numberStr.LastIndexOf(',') == 4
|
|
|
|
|
|
&& !numberStr.StartsWith("0,")
|
|
|
|
|
|
&& !numberStr.StartsWith(",");
|
|
|
|
|
|
if (isAmbiguous)
|
2025-07-22 22:48:11 +00:00
|
|
|
|
{
|
2025-07-27 06:43:31 +00:00
|
|
|
|
if (systemGroupSep == Comma)
|
2025-07-22 22:48:11 +00:00
|
|
|
|
{
|
2025-07-23 03:27:19 +00:00
|
|
|
|
context.InputUsesGroupSeparators = true;
|
2025-07-27 06:43:31 +00:00
|
|
|
|
return numberStr.Replace(Comma, string.Empty);
|
2025-07-22 22:48:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-07-27 06:43:31 +00:00
|
|
|
|
context.InputDecimalSeparator = Comma;
|
|
|
|
|
|
return numberStr.Replace(Comma, Dot);
|
2025-07-22 22:48:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-07-23 03:27:19 +00:00
|
|
|
|
else // Unambiguous decimal (e.g., "12,34" or "0,123" or ",123")
|
2025-07-22 22:48:11 +00:00
|
|
|
|
{
|
2025-07-27 06:43:31 +00:00
|
|
|
|
context.InputDecimalSeparator = Comma;
|
|
|
|
|
|
return numberStr.Replace(Comma, Dot);
|
2025-07-22 22:48:11 +00:00
|
|
|
|
}
|
2020-03-31 11:00:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-23 03:27:19 +00:00
|
|
|
|
// Case 4: No separators
|
2025-07-22 22:48:11 +00:00
|
|
|
|
return numberStr;
|
|
|
|
|
|
}
|
2022-04-24 09:33:06 +00:00
|
|
|
|
|
2025-07-23 00:18:26 +00:00
|
|
|
|
private string FormatResult(decimal roundedResult, ParsingContext context)
|
2025-07-22 22:48:11 +00:00
|
|
|
|
{
|
2025-07-23 00:18:26 +00:00
|
|
|
|
string decimalSeparator = context.InputDecimalSeparator ?? GetDecimalSeparator();
|
2025-07-23 16:56:58 +00:00
|
|
|
|
string groupSeparator = GetGroupSeparator(decimalSeparator);
|
2025-07-22 22:48:11 +00:00
|
|
|
|
|
|
|
|
|
|
string resultStr = roundedResult.ToString(CultureInfo.InvariantCulture);
|
|
|
|
|
|
|
|
|
|
|
|
string[] parts = resultStr.Split('.');
|
|
|
|
|
|
string integerPart = parts[0];
|
|
|
|
|
|
string fractionalPart = parts.Length > 1 ? parts[1] : string.Empty;
|
|
|
|
|
|
|
2025-07-23 16:56:58 +00:00
|
|
|
|
if (context.InputUsesGroupSeparators && integerPart.Length > 3)
|
2025-07-22 22:48:11 +00:00
|
|
|
|
{
|
|
|
|
|
|
integerPart = ThousandGroupRegex.Replace(integerPart, groupSeparator);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(fractionalPart))
|
|
|
|
|
|
{
|
|
|
|
|
|
return integerPart + decimalSeparator + fractionalPart;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return integerPart;
|
2020-03-31 11:00:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-23 16:56:58 +00:00
|
|
|
|
private string GetGroupSeparator(string decimalSeparator)
|
2020-03-31 11:00:09 +00:00
|
|
|
|
{
|
2025-07-24 01:33:59 +00:00
|
|
|
|
// This logic is now independent of the system's group separator
|
|
|
|
|
|
// to ensure consistent output for unit testing.
|
2025-07-27 06:43:31 +00:00
|
|
|
|
return decimalSeparator == Dot ? Comma : Dot;
|
2025-07-23 16:56:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-31 11:00:09 +00:00
|
|
|
|
private bool CanCalculate(Query query)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (query.Search.Length < 2)
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!RegValidExpressChar.IsMatch(query.Search))
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2020-10-13 06:01:26 +00:00
|
|
|
|
|
2020-03-31 11:00:09 +00:00
|
|
|
|
if (!IsBracketComplete(query.Search))
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-27 06:43:31 +00:00
|
|
|
|
private string GetDecimalSeparator()
|
2020-03-31 11:00:09 +00:00
|
|
|
|
{
|
2025-03-20 10:11:53 +00:00
|
|
|
|
string systemDecimalSeparator = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
|
|
|
|
|
|
return _settings.DecimalSeparator switch
|
2020-03-31 11:00:09 +00:00
|
|
|
|
{
|
2025-03-20 10:11:53 +00:00
|
|
|
|
DecimalSeparator.UseSystemLocale => systemDecimalSeparator,
|
2025-06-23 04:25:16 +00:00
|
|
|
|
DecimalSeparator.Dot => Dot,
|
|
|
|
|
|
DecimalSeparator.Comma => Comma,
|
2025-03-20 10:11:53 +00:00
|
|
|
|
_ => systemDecimalSeparator,
|
|
|
|
|
|
};
|
2020-03-31 11:00:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-23 04:25:16 +00:00
|
|
|
|
private static bool IsBracketComplete(string query)
|
2014-03-17 16:43:20 +00:00
|
|
|
|
{
|
2017-10-16 18:17:53 +00:00
|
|
|
|
var matchs = RegBrackets.Matches(query);
|
2014-03-17 16:43:20 +00:00
|
|
|
|
var leftBracketCount = 0;
|
|
|
|
|
|
foreach (Match match in matchs)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (match.Value == "(" || match.Value == "[")
|
|
|
|
|
|
{
|
|
|
|
|
|
leftBracketCount++;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
leftBracketCount--;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return leftBracketCount == 0;
|
|
|
|
|
|
}
|
2020-10-13 06:01:26 +00:00
|
|
|
|
|
2015-02-07 13:27:48 +00:00
|
|
|
|
public string GetTranslatedPluginTitle()
|
|
|
|
|
|
{
|
2025-07-27 10:41:23 +00:00
|
|
|
|
return Localize.flowlauncher_plugin_calculator_plugin_name();
|
2015-02-07 13:27:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public string GetTranslatedPluginDescription()
|
|
|
|
|
|
{
|
2025-07-27 10:41:23 +00:00
|
|
|
|
return Localize.flowlauncher_plugin_calculator_plugin_description();
|
2015-02-07 13:27:48 +00:00
|
|
|
|
}
|
2020-03-31 11:00:09 +00:00
|
|
|
|
|
|
|
|
|
|
public Control CreateSettingPanel()
|
|
|
|
|
|
{
|
2025-06-23 04:25:16 +00:00
|
|
|
|
return new CalculatorSettings(_settings);
|
2020-03-31 11:00:09 +00:00
|
|
|
|
}
|
2025-07-22 15:48:39 +00:00
|
|
|
|
|
|
|
|
|
|
public void OnCultureInfoChanged(CultureInfo newCulture)
|
|
|
|
|
|
{
|
|
|
|
|
|
DecimalSeparatorLocalized.UpdateLabels(_viewModel.AllDecimalSeparator);
|
|
|
|
|
|
}
|
2014-03-17 16:43:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|