Skip to content

Commit

Permalink
Merge pull request #83 from SubhadeepJasu/percentage_behavior
Browse files Browse the repository at this point in the history
Percentage behavior
  • Loading branch information
SubhadeepJasu authored Sep 8, 2021
2 parents d95677c + e49338c commit c4f908a
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 25 deletions.
12 changes: 11 additions & 1 deletion data/com.github.subhadeepjasu.pebbles.appdata.xml.in
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
</custom>
<translation type="gettext">com.github.subhadeepjasu.pebbles</translation>
<developer_name>Subhadeep Jasu</developer_name>
<url type="homepage">https://github.com/SubhadeepJasu/pebbles</url>
<url type="homepage">https://subhadeepjasu.github.io/#/project/pebbles</url>
<url type="bugtracker">https://github.com/SubhadeepJasu/pebbles/issues</url>
<url type="help">https://github.com/SubhadeepJasu/pebbles/issues</url>
<update_contact>[email protected]</update_contact>
Expand Down Expand Up @@ -93,6 +93,16 @@
<content_attribute id="money-gambling">none</content_attribute>
</content_rating>
<releases>
<release date="2021-09-08" version="2.0.1">
<description>
<p>Improved:</p>
<ul>
<li>[Core] Intelligently convert expression when there is a percentage sign</li>
<li>[Core] Clean up useless debug prints, clean up console output</li>
<li>[UI] Follow elementary theme even when run on another distro</li>
</ul>
</description>
</release>
<release date="2021-08-03" version="2.0.0">
<description>
<p>New:</p>
Expand Down
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
project (
'com.github.subhadeepjasu.pebbles',
'vala', 'c',
version: '2.0.0',
version: '2.0.1',
)

# GNOME module
Expand Down
19 changes: 15 additions & 4 deletions src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ namespace Pebbles {
}

protected override void activate () {
init_theme ();
var mainwindow = new MainWindow ();
mainwindow.application = this;

mainwindow.present ();
}

Expand All @@ -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 {
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions src/Core/HistoryManager.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
}
Expand Down Expand Up @@ -208,7 +208,7 @@ namespace Pebbles {
public void load_from_csv (string csv_data) {
//_history = new List<EvaluationResult>();
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 (",");
Expand Down Expand Up @@ -304,4 +304,4 @@ namespace Pebbles {
history_updated ();
}
}
}
}
1 change: 0 additions & 1 deletion src/Core/ScientificCalculator.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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 (), "."));
}
Expand Down
136 changes: 126 additions & 10 deletions src/Core/Utils.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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 ("", " - ");
Expand All @@ -174,9 +174,12 @@ namespace Pebbles {
// Intelligently convert expressions based on common rules
exp = algebraic_parenthesis_product_convert (exp);
exp = unary_minus_convert (exp);
while (exp.contains("%")) {
exp = relative_percentage_convert(exp);
}

//exp = space_removal (exp);
print ("Final exp: " + exp + "\n");
exp = space_removal (exp.strip ());
debug ("Final inferred expression: >>>>" + exp + "<<<<\n");
return exp;
}
else {
Expand Down Expand Up @@ -228,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;
}

Expand Down Expand Up @@ -349,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";
Expand All @@ -372,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;
}

Expand All @@ -385,7 +384,6 @@ namespace Pebbles {
}
}
converted_exp = space_removal(string.joinv (" ", tokens));
//print("algebraic converted: %s\n", converted_exp);
return converted_exp;
}

Expand All @@ -404,9 +402,127 @@ 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) {
if (exp.contains ("%")) {
// Expression is of the form `a +/- b %`
debug ("Percentage////////////////////\n");
debug ("Exp: %s\n", exp);
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;
tokens[i] = "[%]";
break;
}
}
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] == "-")) {
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);
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];
string result = string.joinv (" ", tokens);
result = space_removal(result);
return result.replace("[%]", " * " + exp_a + " / 100 ");
}
}
}
} else if (tokens[percentage_index - 1] == ")") {
int paren_balance_b = -1;
int paren_start_index_b = -1;
for (int i = percentage_index - 2; i >= 0; 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) {
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] == "-")) {
if (tokens[paren_start_index_b - 2] != null) {
if (tokens[paren_start_index_b - 2] == ")") {
int paren_balance = -1;
int paren_start_index = -1;
for (int i = paren_start_index_b - 3; 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: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);
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];
string result = string.joinv (" ", tokens);
result = space_removal(result);
return result.replace("[%]", " * " + exp_a + " / 100 ");
}
}
}
}
string result = string.joinv (" ", tokens);
result = space_removal(result);
return result.replace("[%]", " / 100 ");
}
return exp;
}

private static bool is_number (string exp) {
if (exp.has_suffix ("0") ||
Expand Down
4 changes: 2 additions & 2 deletions src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/Views/Displays/ProgrammerDisplay.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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 ();
Expand Down Expand Up @@ -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);
}
}
Expand Down

0 comments on commit c4f908a

Please sign in to comment.