Skip to content

Commit ca5f5cf

Browse files
authored
Merge pull request #4206 from Flow-Launcher/copilot/fix-calculator-thousands-separator
Add setting to disable thousands separator in Calculator plugin
2 parents a46350c + befcc9b commit ca5f5cf

File tree

5 files changed

+61
-5
lines changed

5 files changed

+61
-5
lines changed

Flow.Launcher.Test/Plugins/CalculatorTest.cs

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@ public class CalculatorPluginTest
1616
{
1717
DecimalSeparator = DecimalSeparator.UseSystemLocale,
1818
MaxDecimalPlaces = 10,
19-
ShowErrorMessage = false // Make sure we return the empty results when error occurs
19+
ShowErrorMessage = false, // Make sure we return the empty results when error occurs
20+
UseThousandsSeparator = true // Default value
2021
};
2122
private readonly Engine _engine = new(new Configuration
2223
{
2324
Scope = new Dictionary<string, object>
24-
{
25-
{ "e", Math.E }, // e is not contained in the default mages engine
26-
}
25+
{
26+
{ "e", Math.E }, // e is not contained in the default mages engine
27+
}
2728
});
2829

2930
public CalculatorPluginTest()
@@ -41,6 +42,44 @@ public CalculatorPluginTest()
4142
engineField.SetValue(null, _engine);
4243
}
4344

45+
[Test]
46+
public void ThousandsSeparatorTest_Enabled()
47+
{
48+
_settings.UseThousandsSeparator = true;
49+
50+
_settings.DecimalSeparator = DecimalSeparator.Dot;
51+
var result = GetCalculationResult("1000+234");
52+
// When thousands separator is enabled, the result should contain a separator
53+
// Since decimal separator is dot, thousands separator should be comma
54+
ClassicAssert.AreEqual("1,234", result);
55+
56+
_settings.DecimalSeparator = DecimalSeparator.Comma;
57+
var result2 = GetCalculationResult("1000+234");
58+
// When thousands separator is enabled, the result should contain a separator
59+
// Since decimal separator is comma, thousands separator should be dot
60+
ClassicAssert.AreEqual("1.234", result2);
61+
}
62+
63+
[Test]
64+
public void ThousandsSeparatorTest_Disabled()
65+
{
66+
_settings.UseThousandsSeparator = false;
67+
_settings.DecimalSeparator = DecimalSeparator.UseSystemLocale;
68+
69+
var result = GetCalculationResult("1000+234");
70+
ClassicAssert.AreEqual("1234", result);
71+
}
72+
73+
[Test]
74+
public void ThousandsSeparatorTest_LargeNumber()
75+
{
76+
_settings.UseThousandsSeparator = false;
77+
_settings.DecimalSeparator = DecimalSeparator.UseSystemLocale;
78+
79+
var result = GetCalculationResult("1000000+234567");
80+
ClassicAssert.AreEqual("1234567", result);
81+
}
82+
4483
// Basic operations
4584
[TestCase(@"1+1", "2")]
4685
[TestCase(@"2-1", "1")]
@@ -77,6 +116,9 @@ public CalculatorPluginTest()
77116
[TestCase(@"invalid_expression", "")]
78117
public void CalculatorTest(string expression, string result)
79118
{
119+
_settings.UseThousandsSeparator = false;
120+
_settings.DecimalSeparator = DecimalSeparator.Dot;
121+
80122
ClassicAssert.AreEqual(GetCalculationResult(expression), result);
81123
}
82124

Plugins/Flow.Launcher.Plugin.Calculator/Languages/en.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<system:String x:Key="flowlauncher_plugin_calculator_decimal_separator_comma">Comma (,)</system:String>
1515
<system:String x:Key="flowlauncher_plugin_calculator_decimal_separator_dot">Dot (.)</system:String>
1616
<system:String x:Key="flowlauncher_plugin_calculator_max_decimal_places">Max. decimal places</system:String>
17+
<system:String x:Key="flowlauncher_plugin_calculator_use_thousands_separator">Show thousands separator in results</system:String>
1718
<system:String x:Key="flowlauncher_plugin_calculator_failed_to_copy">Copy failed, please try later</system:String>
1819
<system:String x:Key="flowlauncher_plugin_calculator_show_error_message">Show error message when calculation fails</system:String>
1920
</ResourceDictionary>

Plugins/Flow.Launcher.Plugin.Calculator/Main.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ private string FormatResult(decimal roundedResult)
363363
string integerPart = parts[0];
364364
string fractionalPart = parts.Length > 1 ? parts[1] : string.Empty;
365365

366-
if (integerPart.Length > 3)
366+
if (_settings.UseThousandsSeparator && integerPart.Length > 3)
367367
{
368368
integerPart = ThousandGroupRegex.Replace(integerPart, groupSeparator);
369369
}

Plugins/Flow.Launcher.Plugin.Calculator/Settings.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ public class Settings
77
public int MaxDecimalPlaces { get; set; } = 10;
88

99
public bool ShowErrorMessage { get; set; } = false;
10+
11+
public bool UseThousandsSeparator { get; set; } = true;
1012
}

Plugins/Flow.Launcher.Plugin.Calculator/Views/CalculatorSettings.xaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<RowDefinition Height="auto" />
1717
<RowDefinition Height="auto" />
1818
<RowDefinition Height="auto" />
19+
<RowDefinition Height="auto" />
1920
</Grid.RowDefinitions>
2021
<Grid.ColumnDefinitions>
2122
<ColumnDefinition Width="Auto" />
@@ -66,6 +67,16 @@
6667
Margin="{StaticResource SettingPanelItemTopBottomMargin}"
6768
HorizontalAlignment="Left"
6869
VerticalAlignment="Center"
70+
Content="{DynamicResource flowlauncher_plugin_calculator_use_thousands_separator}"
71+
IsChecked="{Binding Settings.UseThousandsSeparator, Mode=TwoWay}" />
72+
73+
<CheckBox
74+
Grid.Row="3"
75+
Grid.Column="0"
76+
Grid.ColumnSpan="2"
77+
Margin="{StaticResource SettingPanelItemTopBottomMargin}"
78+
HorizontalAlignment="Left"
79+
VerticalAlignment="Center"
6980
Content="{DynamicResource flowlauncher_plugin_calculator_show_error_message}"
7081
IsChecked="{Binding Settings.ShowErrorMessage, Mode=TwoWay}" />
7182
</Grid>

0 commit comments

Comments
 (0)