From 462a32608c1279c65b923f15197c7d8fc1ce3d79 Mon Sep 17 00:00:00 2001 From: SubhadeepJasu Date: Sat, 21 Aug 2021 22:26:55 +0530 Subject: [PATCH 1/5] Try to implement correct percentage behavior --- src/Core/Utils.vala | 54 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/src/Core/Utils.vala b/src/Core/Utils.vala index 7882e709..c5720821 100644 --- a/src/Core/Utils.vala +++ b/src/Core/Utils.vala @@ -153,7 +153,7 @@ namespace Pebbles { exp = exp.replace (")", " ) "); exp = exp.replace ("\xC3\x97", " * "); exp = exp.replace ("\xC3\xB7", " / "); - exp = exp.replace ("%", " / 100 "); + //exp = exp.replace ("%", " / 100 "); exp = exp.replace ("+", " + "); exp = exp.replace ("-", " - "); exp = exp.replace ("−", " - "); @@ -227,6 +227,7 @@ namespace Pebbles { // Intelligently convert expressions based on common rules exp = algebraic_parenthesis_product_convert (exp); exp = unary_minus_convert (exp); + exp = relative_percentage_convert(exp); //exp = space_removal (exp); print ("Final exp: " + exp + "\n"); @@ -407,6 +408,57 @@ namespace Pebbles { print("algebraic converted: %s\n", converted_exp); return converted_exp; } + + public static string relative_percentage_convert (string exp) { + if (exp.contains ("%")) { + // Expression is of the form `a +/- b %` + string exp_a = ""; + string exp_b = ""; + string[] tokens = exp.split (" "); + int percentage_index = -1; + for (int i = tokens.length - 1; i > 0; i--) { + if (tokens[i] == "%") { + percentage_index = i; + break; + } + } + if (is_number (tokens[percentage_index - 1])) { + exp_b = tokens[percentage_index - 1]; + if (tokens[percentage_index - 2] != null && !is_number (tokens[percentage_index - 2])) { + if (tokens[percentage_index - 3] != null) { + if (tokens[percentage_index - 3] == ")") { + int paren_balance = -1; + int paren_start_index = -1; + for (int i = percentage_index - 4; i > 0; i--) { + if (tokens[i] == "(") { + paren_balance++; + } else if (tokens[i] == ")") { + paren_balance--; + } + if (paren_balance == 0) { + paren_start_index = i; + break; + } + } + if (paren_start_index >= 0) { + string[] tokens_in_range = tokens[paren_start_index:percentage_index - 2]; + for (int i = 0; i < tokens_in_range.length; i++) { + exp_a += " " + tokens_in_range[i] + " "; + } + exp_a = space_removal (exp_a); + return exp.replace("%", " / " + exp_a); + } + } else if (is_number (tokens[percentage_index - 3])) { + exp_a = tokens[percentage_index - 3]; + return exp.replace("%", " / " + exp_a); + } + } + } + return exp.replace("%", " / 100 "); + } + } + return exp; + } private static bool is_number (string exp) { if (exp.has_suffix ("0") || From ae831ddcfba6bff65fc99e73fbb5b9e150707851 Mon Sep 17 00:00:00 2001 From: SubhadeepJasu Date: Sat, 21 Aug 2021 23:34:52 +0530 Subject: [PATCH 2/5] Recitify % conversion logic --- src/Core/Utils.vala | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/Core/Utils.vala b/src/Core/Utils.vala index c5720821..93f6e2e7 100644 --- a/src/Core/Utils.vala +++ b/src/Core/Utils.vala @@ -153,7 +153,7 @@ namespace Pebbles { exp = exp.replace (")", " ) "); exp = exp.replace ("\xC3\x97", " * "); exp = exp.replace ("\xC3\xB7", " / "); - //exp = exp.replace ("%", " / 100 "); + exp = exp.replace ("%", " % "); exp = exp.replace ("+", " + "); exp = exp.replace ("-", " - "); exp = exp.replace ("−", " - "); @@ -174,9 +174,10 @@ namespace Pebbles { // Intelligently convert expressions based on common rules exp = algebraic_parenthesis_product_convert (exp); exp = unary_minus_convert (exp); + exp = relative_percentage_convert(exp); - //exp = space_removal (exp); - print ("Final exp: " + exp + "\n"); + exp = space_removal (exp.strip ()); + print ("Finalle exp: >>>>" + exp + "<<<<\n"); return exp; } else { @@ -227,7 +228,6 @@ namespace Pebbles { // Intelligently convert expressions based on common rules exp = algebraic_parenthesis_product_convert (exp); exp = unary_minus_convert (exp); - exp = relative_percentage_convert(exp); //exp = space_removal (exp); print ("Final exp: " + exp + "\n"); @@ -410,8 +410,10 @@ namespace Pebbles { } public static string relative_percentage_convert (string exp) { + print ("Exp: %s\n", exp); if (exp.contains ("%")) { // Expression is of the form `a +/- b %` + print ("Percentage////////////////////\n"); string exp_a = ""; string exp_b = ""; string[] tokens = exp.split (" "); @@ -422,6 +424,7 @@ namespace Pebbles { break; } } + print ("1\n"); if (is_number (tokens[percentage_index - 1])) { exp_b = tokens[percentage_index - 1]; if (tokens[percentage_index - 2] != null && !is_number (tokens[percentage_index - 2])) { @@ -446,16 +449,16 @@ namespace Pebbles { exp_a += " " + tokens_in_range[i] + " "; } exp_a = space_removal (exp_a); - return exp.replace("%", " / " + exp_a); + return exp.replace("%", " * " + exp_a + " / 100 "); } } else if (is_number (tokens[percentage_index - 3])) { exp_a = tokens[percentage_index - 3]; - return exp.replace("%", " / " + exp_a); + return exp.replace("%", " * " + exp_a + " / 100 "); } } } - return exp.replace("%", " / 100 "); } + return exp.replace("%", " / 100 "); } return exp; } From 0a8b6c6af5988f264252ea5cfd909146f1ec4ab3 Mon Sep 17 00:00:00 2001 From: SubhadeepJasu Date: Sun, 22 Aug 2021 14:05:20 +0530 Subject: [PATCH 3/5] Handle more complex use cases of percentage --- src/Core/Utils.vala | 72 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 70 insertions(+), 2 deletions(-) diff --git a/src/Core/Utils.vala b/src/Core/Utils.vala index 93f6e2e7..f79e95d9 100644 --- a/src/Core/Utils.vala +++ b/src/Core/Utils.vala @@ -427,12 +427,18 @@ namespace Pebbles { print ("1\n"); if (is_number (tokens[percentage_index - 1])) { exp_b = tokens[percentage_index - 1]; - if (tokens[percentage_index - 2] != null && !is_number (tokens[percentage_index - 2])) { + if (tokens[percentage_index - 2] != null && + (tokens[percentage_index - 2] == "+" || + tokens[percentage_index - 2] == "-")) { + print ("2\n"); if (tokens[percentage_index - 3] != null) { + print ("3\n"); if (tokens[percentage_index - 3] == ")") { + print ("4\n"); int paren_balance = -1; int paren_start_index = -1; - for (int i = percentage_index - 4; i > 0; i--) { + for (int i = percentage_index - 4; i >= 0; i--) { + print ("%s\n", tokens[i]); if (tokens[i] == "(") { paren_balance++; } else if (tokens[i] == ")") { @@ -444,6 +450,7 @@ namespace Pebbles { } } if (paren_start_index >= 0) { + print ("5\n"); string[] tokens_in_range = tokens[paren_start_index:percentage_index - 2]; for (int i = 0; i < tokens_in_range.length; i++) { exp_a += " " + tokens_in_range[i] + " "; @@ -457,6 +464,67 @@ namespace Pebbles { } } } + } else if (tokens[percentage_index - 1] == ")") { + print ("6\n"); + int paren_balance_b = -1; + int paren_start_index_b = -1; + for (int i = percentage_index - 2; i >= 0; i--) { + print ("%s\n", tokens[i]); + if (tokens[i] == "(") { + paren_balance_b++; + } else if (tokens[i] == ")") { + paren_balance_b--; + } + if (paren_balance_b == 0) { + paren_start_index_b = i; + break; + } + } + if (paren_start_index_b >= 0) { + print ("7\n"); + string[] tokens_in_range = tokens[paren_start_index_b:percentage_index - 2]; + for (int i = 0; i < tokens_in_range.length; i++) { + exp_b += " " + tokens_in_range[i] + " "; + } + exp_b = space_removal (exp_b); + } + if (tokens[paren_start_index_b - 1] != null && + (tokens[paren_start_index_b - 1] == "+" || + tokens[paren_start_index_b - 1] == "-")) { + print ("8\n"); + if (tokens[paren_start_index_b - 2] != null) { + print ("9\n"); + if (tokens[paren_start_index_b - 2] == ")") { + print ("10\n"); + int paren_balance = -1; + int paren_start_index = -1; + for (int i = paren_start_index_b - 3; i >= 0; i--) { + print ("%s\n", tokens[i]); + if (tokens[i] == "(") { + paren_balance++; + } else if (tokens[i] == ")") { + paren_balance--; + } + if (paren_balance == 0) { + paren_start_index = i; + break; + } + } + if (paren_start_index >= 0) { + print ("11\n"); + string[] tokens_in_range = tokens[paren_start_index:paren_start_index_b - 1]; + for (int i = 0; i < tokens_in_range.length; i++) { + exp_a += " " + tokens_in_range[i] + " "; + } + exp_a = space_removal (exp_a); + return exp.replace("%", " * " + exp_a + " / 100 "); + } + } else if (is_number (tokens[paren_start_index_b - 2])) { + exp_a = tokens[paren_start_index_b - 2]; + return exp.replace("%", " * " + exp_a + " / 100 "); + } + } + } } return exp.replace("%", " / 100 "); } From 3b23328095ee201cd71b4e9c7e6fbe3b8845607e Mon Sep 17 00:00:00 2001 From: SubhadeepJasu Date: Sun, 5 Sep 2021 20:54:21 +0530 Subject: [PATCH 4/5] Convert expression until no percent sign left --- ...ithub.subhadeepjasu.pebbles.appdata.xml.in | 2 +- src/Core/HistoryManager.vala | 8 +-- src/Core/ScientificCalculator.vala | 1 - src/Core/Utils.vala | 53 ++++++++----------- src/MainWindow.vala | 4 +- src/Views/Displays/ProgrammerDisplay.vala | 3 +- 6 files changed, 31 insertions(+), 40 deletions(-) diff --git a/data/com.github.subhadeepjasu.pebbles.appdata.xml.in b/data/com.github.subhadeepjasu.pebbles.appdata.xml.in index 4984b141..fbb73dcc 100644 --- a/data/com.github.subhadeepjasu.pebbles.appdata.xml.in +++ b/data/com.github.subhadeepjasu.pebbles.appdata.xml.in @@ -28,7 +28,7 @@ com.github.subhadeepjasu.pebbles Subhadeep Jasu - https://github.com/SubhadeepJasu/pebbles + https://subhadeepjasu.github.io/#/project/pebbles https://github.com/SubhadeepJasu/pebbles/issues https://github.com/SubhadeepJasu/pebbles/issues subhajasu@gmail.com diff --git a/src/Core/HistoryManager.vala b/src/Core/HistoryManager.vala index cb2717eb..0282f662 100644 --- a/src/Core/HistoryManager.vala +++ b/src/Core/HistoryManager.vala @@ -106,7 +106,7 @@ namespace Pebbles { } public bool is_empty (EvaluationResult.ResultSource? mode = null) { - print("H\n"); + debug ("Finding if history is empty"); if (_history.length == 0) { return true; } else { @@ -117,7 +117,7 @@ namespace Pebbles { if (_history.peek_nth(i) != null && _history.peek_nth(i).result_source == mode) { return false; } - print("Counting_history (%u)...\n", i); + debug ("Found history item (%u)...", i); if (i == 0) { return true; } @@ -208,7 +208,7 @@ namespace Pebbles { public void load_from_csv (string csv_data) { //_history = new List(); string[] lines = csv_data.split ("\n"); - print(lines.length.to_string ()); + debug ("Found " + lines.length.to_string () + "entries in memory"); foreach (string line in lines) { if (line != "") { string[] item = line.split (","); @@ -304,4 +304,4 @@ namespace Pebbles { history_updated (); } } -} \ No newline at end of file +} diff --git a/src/Core/ScientificCalculator.vala b/src/Core/ScientificCalculator.vala index 1c6c8a03..bc3986fe 100644 --- a/src/Core/ScientificCalculator.vala +++ b/src/Core/ScientificCalculator.vala @@ -37,7 +37,6 @@ namespace Pebbles { public string get_result (string exp, GlobalAngleUnit angle_mode_in, int? float_accuracy = -1, bool? tokenize = true) { var result = exp; - warning(result); if (tokenize) { result = Utils.st_tokenize (exp.replace (Utils.get_local_radix_symbol (), ".")); } diff --git a/src/Core/Utils.vala b/src/Core/Utils.vala index f79e95d9..fef824aa 100644 --- a/src/Core/Utils.vala +++ b/src/Core/Utils.vala @@ -174,10 +174,12 @@ namespace Pebbles { // Intelligently convert expressions based on common rules exp = algebraic_parenthesis_product_convert (exp); exp = unary_minus_convert (exp); - exp = relative_percentage_convert(exp); + while (exp.contains("%")) { + exp = relative_percentage_convert(exp); + } exp = space_removal (exp.strip ()); - print ("Finalle exp: >>>>" + exp + "<<<<\n"); + debug ("Final inferred expression: >>>>" + exp + "<<<<\n"); return exp; } else { @@ -229,8 +231,7 @@ namespace Pebbles { exp = algebraic_parenthesis_product_convert (exp); exp = unary_minus_convert (exp); - //exp = space_removal (exp); - print ("Final exp: " + exp + "\n"); + debug ("Final inferred expression: " + exp); return exp; } @@ -350,12 +351,10 @@ namespace Pebbles { return result; } private static string unary_minus_convert (string exp) { - print(">%s<\n", exp); string uniminus_converted = ""; string[] tokens = exp.split (" "); for (int i = 0; i < tokens.length; i++) { if (tokens[i] == "-") { - print("token: %s\n", tokens[i + 1]); if (i == 0) { if (i < tokens.length) { tokens [i] = "( 0 u"; @@ -373,7 +372,6 @@ namespace Pebbles { } uniminus_converted = string.joinv (" ", tokens); //uniminus_converted = uniminus_converted.replace ("u", "0 u"); - print("unary converted: %s\n", uniminus_converted); return uniminus_converted; } @@ -386,7 +384,6 @@ namespace Pebbles { } } converted_exp = space_removal(string.joinv (" ", tokens)); - //print("algebraic converted: %s\n", converted_exp); return converted_exp; } @@ -405,15 +402,14 @@ namespace Pebbles { } } string converted_exp = space_removal(string.joinv (" ", tokens)); - print("algebraic converted: %s\n", converted_exp); return converted_exp; } public static string relative_percentage_convert (string exp) { - print ("Exp: %s\n", exp); if (exp.contains ("%")) { // Expression is of the form `a +/- b %` - print ("Percentage////////////////////\n"); + debug ("Percentage////////////////////\n"); + debug ("Exp: %s\n", exp); string exp_a = ""; string exp_b = ""; string[] tokens = exp.split (" "); @@ -421,24 +417,20 @@ namespace Pebbles { for (int i = tokens.length - 1; i > 0; i--) { if (tokens[i] == "%") { percentage_index = i; + tokens[i] = "[%]"; break; } } - print ("1\n"); if (is_number (tokens[percentage_index - 1])) { exp_b = tokens[percentage_index - 1]; if (tokens[percentage_index - 2] != null && (tokens[percentage_index - 2] == "+" || tokens[percentage_index - 2] == "-")) { - print ("2\n"); if (tokens[percentage_index - 3] != null) { - print ("3\n"); if (tokens[percentage_index - 3] == ")") { - print ("4\n"); int paren_balance = -1; int paren_start_index = -1; for (int i = percentage_index - 4; i >= 0; i--) { - print ("%s\n", tokens[i]); if (tokens[i] == "(") { paren_balance++; } else if (tokens[i] == ")") { @@ -450,26 +442,27 @@ namespace Pebbles { } } if (paren_start_index >= 0) { - print ("5\n"); string[] tokens_in_range = tokens[paren_start_index:percentage_index - 2]; for (int i = 0; i < tokens_in_range.length; i++) { exp_a += " " + tokens_in_range[i] + " "; } exp_a = space_removal (exp_a); - return exp.replace("%", " * " + exp_a + " / 100 "); + string result = string.joinv (" ", tokens); + result = space_removal(result); + return result.replace("[%]", " * " + exp_a + " / 100 "); } } else if (is_number (tokens[percentage_index - 3])) { exp_a = tokens[percentage_index - 3]; - return exp.replace("%", " * " + exp_a + " / 100 "); + string result = string.joinv (" ", tokens); + result = space_removal(result); + return result.replace("[%]", " * " + exp_a + " / 100 "); } } } } else if (tokens[percentage_index - 1] == ")") { - print ("6\n"); int paren_balance_b = -1; int paren_start_index_b = -1; for (int i = percentage_index - 2; i >= 0; i--) { - print ("%s\n", tokens[i]); if (tokens[i] == "(") { paren_balance_b++; } else if (tokens[i] == ")") { @@ -481,7 +474,6 @@ namespace Pebbles { } } if (paren_start_index_b >= 0) { - print ("7\n"); string[] tokens_in_range = tokens[paren_start_index_b:percentage_index - 2]; for (int i = 0; i < tokens_in_range.length; i++) { exp_b += " " + tokens_in_range[i] + " "; @@ -491,15 +483,11 @@ namespace Pebbles { if (tokens[paren_start_index_b - 1] != null && (tokens[paren_start_index_b - 1] == "+" || tokens[paren_start_index_b - 1] == "-")) { - print ("8\n"); if (tokens[paren_start_index_b - 2] != null) { - print ("9\n"); if (tokens[paren_start_index_b - 2] == ")") { - print ("10\n"); int paren_balance = -1; int paren_start_index = -1; for (int i = paren_start_index_b - 3; i >= 0; i--) { - print ("%s\n", tokens[i]); if (tokens[i] == "(") { paren_balance++; } else if (tokens[i] == ")") { @@ -511,22 +499,27 @@ namespace Pebbles { } } if (paren_start_index >= 0) { - print ("11\n"); string[] tokens_in_range = tokens[paren_start_index:paren_start_index_b - 1]; for (int i = 0; i < tokens_in_range.length; i++) { exp_a += " " + tokens_in_range[i] + " "; } exp_a = space_removal (exp_a); - return exp.replace("%", " * " + exp_a + " / 100 "); + string result = string.joinv (" ", tokens); + result = space_removal(result); + return result.replace("[%]", " * " + exp_a + " / 100 "); } } else if (is_number (tokens[paren_start_index_b - 2])) { exp_a = tokens[paren_start_index_b - 2]; - return exp.replace("%", " * " + exp_a + " / 100 "); + string result = string.joinv (" ", tokens); + result = space_removal(result); + return result.replace("[%]", " * " + exp_a + " / 100 "); } } } } - return exp.replace("%", " / 100 "); + string result = string.joinv (" ", tokens); + result = space_removal(result); + return result.replace("[%]", " / 100 "); } return exp; } diff --git a/src/MainWindow.vala b/src/MainWindow.vala index 0170c300..98c64495 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -991,9 +991,9 @@ namespace Pebbles { } if (settings.saved_history != "") { - print("load\n"); + debug ("Loading History from memory"); history_manager.load_from_csv (settings.saved_history); - print("loaded\n"); + debug ("Loaded History"); } } diff --git a/src/Views/Displays/ProgrammerDisplay.vala b/src/Views/Displays/ProgrammerDisplay.vala index 60a4c0f8..7a8cd1a2 100644 --- a/src/Views/Displays/ProgrammerDisplay.vala +++ b/src/Views/Displays/ProgrammerDisplay.vala @@ -234,7 +234,7 @@ namespace Pebbles { if (!this.prog_view.window.history_manager.is_empty (EvaluationResult.ResultSource.PROG)) { bool[] last_output_array= this.prog_view.window.history_manager.get_last_evaluation_result (EvaluationResult.ResultSource.PROG).prog_output; string last_answer = programmer_calculator_front_end.bool_array_to_string (last_output_array, settings.global_word_length, settings.number_system); - warning(last_answer); + debug (last_answer); input_entry.set_text (input_entry.get_text().replace ("ans", last_answer)); if (dont_push_history != true) { this.set_number_system (); @@ -429,7 +429,6 @@ namespace Pebbles { settings.prog_input_text = input_entry.get_text (); settings.prog_output_text = Utils.remove_leading_zeroes (result); } else { - warning("h"); get_answer_evaluate(true); } } From e49338ca3c1a6be2ada73893a381b47350b1d4b4 Mon Sep 17 00:00:00 2001 From: SubhadeepJasu Date: Sun, 5 Sep 2021 21:11:43 +0530 Subject: [PATCH 5/5] Version bump and stylesheet fix --- ...ithub.subhadeepjasu.pebbles.appdata.xml.in | 10 ++++++++++ meson.build | 2 +- src/Application.vala | 19 +++++++++++++++---- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/data/com.github.subhadeepjasu.pebbles.appdata.xml.in b/data/com.github.subhadeepjasu.pebbles.appdata.xml.in index fbb73dcc..4c4a808e 100644 --- a/data/com.github.subhadeepjasu.pebbles.appdata.xml.in +++ b/data/com.github.subhadeepjasu.pebbles.appdata.xml.in @@ -93,6 +93,16 @@ none + + +

Improved:

+
    +
  • [Core] Intelligently convert expression when there is a percentage sign
  • +
  • [Core] Clean up useless debug prints, clean up console output
  • +
  • [UI] Follow elementary theme even when run on another distro
  • +
+
+

New:

diff --git a/meson.build b/meson.build index 098ea3de..92375721 100644 --- a/meson.build +++ b/meson.build @@ -2,7 +2,7 @@ project ( 'com.github.subhadeepjasu.pebbles', 'vala', 'c', - version: '2.0.0', + version: '2.0.1', ) # GNOME module diff --git a/src/Application.vala b/src/Application.vala index e471584b..4b034c75 100644 --- a/src/Application.vala +++ b/src/Application.vala @@ -65,9 +65,9 @@ namespace Pebbles { } protected override void activate () { + init_theme (); var mainwindow = new MainWindow (); mainwindow.application = this; - mainwindow.present (); } @@ -79,15 +79,15 @@ namespace Pebbles { private void command_line_interpreter (ApplicationCommandLine cmd) { string[] cmd_args = cmd.get_arguments (); unowned string[] args = cmd_args; - + bool new_window = false, mini_mode = false; - + GLib.OptionEntry [] option = new OptionEntry [4]; option [0] = { "mini_mode", 0, 0, OptionArg.NONE, ref mini_mode, _("Open In Mini Mode"), null }; option [1] = { "new_window", 0, 0, OptionArg.NONE, ref new_window, _("Open A New Window"), null }; option [2] = { "test", 0, 0, OptionArg.NONE, ref test_mode, _("Enable test mode"), null }; option [3] = { null }; - + var option_context = new OptionContext ("actions"); option_context.add_main_entries (option, null); try { @@ -114,6 +114,7 @@ namespace Pebbles { Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION ); if (mini_mode) { + init_theme (); var minicalcwindow = new Pebbles.MiniCalculator (); minicalcwindow.show_all (); minicalcwindow.application = this; @@ -141,6 +142,16 @@ namespace Pebbles { Process.exit(1); } } + + private void init_theme () { + GLib.Value value = GLib.Value (GLib.Type.STRING); + Gtk.Settings.get_default ().get_property ("gtk-theme-name", ref value); + if (!value.get_string ().has_prefix ("io.elementary.")) { + Gtk.Settings.get_default ().set_property ("gtk-icon-theme-name", "elementary"); + Gtk.Settings.get_default ().set_property ("gtk-theme-name", "io.elementary.stylesheet.blueberry"); + } + } + public static int main (string[] args) { var app = new PebblesApp (); return app.run (args);