Skip to content

Commit

Permalink
Merge pull request #63 from SubhadeepJasu/remediation
Browse files Browse the repository at this point in the history
Remediation
  • Loading branch information
SubhadeepJasu authored Jun 20, 2020
2 parents fe8a046 + 19cb2dd commit e302bf3
Show file tree
Hide file tree
Showing 32 changed files with 698 additions and 230 deletions.
14 changes: 14 additions & 0 deletions data/com.github.subhadeepjasu.pebbles.appdata.xml.in
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,20 @@
</screenshot>
</screenshots>
<releases>
<release date="2020-06-20" version="1.0.4">
<description>
<p>Fixed:</p>
<ul>
<li>[UI] Comma and Radix symbols now obey Language &amp; Region settings</li>
<li>[UI] Backspace button should now check if input entry has text on start up</li>
<li>[Core] Fix precedance with proper PEMDAS rule</li>
</ul>
<p>Improved:</p>
<ul>
<li>[UI] Better clipboard system for Scientific, Calculus, Statistics and converters</li>
</ul>
</description>
</release>
<release date="2020-03-20" version="1.0.3">
<description>
<p>New:</p>
Expand Down
11 changes: 11 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
com.github.subhadeepjasu.pebbles (1.0.4) bionic; urgency=low

[FIXED]
* Comma and Radix symbols now obey Language and Region settings
* Backspace button should now check if input entry has text on start up
* Fix precedance with proper PEMDAS rule
[IMPROVED]
* Better clipboard system for Scientific, Calculus, Statistics and converters

-- Subhadeep Jasu <[email protected]> Sat, 20 Jun 2020 21:03:05 +0530

com.github.subhadeepjasu.pebbles (1.0.3) bionic; urgency=medium

[NEW]
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: '1.0.3',
version: '1.0.4',
)

# GNOME module
Expand Down
6 changes: 6 additions & 0 deletions src/ControlsScheme.vala
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ namespace Pebbles {
},
{
_("All Clear"), "Delete"
},
{
_("Copy Result"), "<Ctrl>C"
},
{
_("Paste Input Expression"), "<Ctrl>V"
}
};
scientific = {
Expand Down
14 changes: 5 additions & 9 deletions src/Core/Converter.vala
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ namespace Pebbles {
}
}
Settings settings;
public string convert (double input, int unit_a, int unit_b) {
public string convert (string input_string, int unit_a, int unit_b) {
string input_temp = input_string.replace (Utils.get_local_separator_symbol (), "");
input_temp = input_temp.replace(Utils.get_local_radix_symbol (), ".");
double input = double.parse (input_temp);
settings = Settings.get_default ();
double result = input * (unit_multipliers_list [unit_b] / unit_multipliers_list [unit_a]);
string output = "";
Expand All @@ -42,15 +45,8 @@ namespace Pebbles {
} else {
output = Utils.manage_decimal_places (result, settings.decimal_places);
}
// Remove trailing 0s and decimals
while (output.has_suffix ("0")) {
output = output.slice (0, -1);
}
if (output.has_suffix (".")) {
output = output.slice (0, -1);
}

return output;
return Utils.format_result (output);
}
public void update_multipliers (double[] multipliers) {
unit_multipliers_list = multipliers;
Expand Down
4 changes: 2 additions & 2 deletions src/Core/CurrencyConverter.vala
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ namespace Pebbles {
string data_set = settings.currency_multipliers;
double[] multipliers;
if (data_set != "") {
string[] token = data_set.split (",");
string[] token = data_set.split ("[&&]");
multipliers = new double[token.length];
for (int i = 0; i < token.length; i++) {
multipliers [i] = double.parse (token [i]);
Expand All @@ -134,7 +134,7 @@ namespace Pebbles {
for (int i = 0; i < multipliers.length; i++) {
save_data = (save_data + multipliers[i].to_string ());
if (i < multipliers.length - 1) {
save_data += ",";
save_data += "[&&]";
}
}
var date_time = new DateTime.now_local ();
Expand Down
50 changes: 9 additions & 41 deletions src/Core/ScientificCalculator.vala
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,18 @@ 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);
result = Utils.st_tokenize (exp.replace (Utils.get_local_radix_symbol (), "."));
}
angle_mode_sci = angle_mode_in;
if (result == "E") {
return result;
return "E";
}
string evaluated_result = evaluate_exp (result, float_accuracy);
if (evaluated_result == "nan")
evaluated_result = "E";
if (evaluated_result == "inf")
evaluated_result = "";
return evaluated_result;
return evaluate_exp (result, float_accuracy);
}

private static bool has_precedence (char op1, char op2) {
private static bool has_precedence_pemdas (char op1, char op2) {
if (op2 == '(' || op2 == ')') {
return false;
}
Expand All @@ -69,10 +65,10 @@ namespace Pebbles {
else if ((op1 == '^' || op1 == 'q') && (op2 == '*' || op2 == '/' || op2 == '-' || op2 == '+' || op2 == 'm')) {
return false;
}
else if ((op1 == '*' || op1 == 'm') && (op2 == '/' || op2 == '+' || op2 == '-')) {
else if ((op1 == 'm') && (op2 == '/' || op2 == '*' || op2 == '+' || op2 == '-')) {
return false;
}
else if ((op1 == '/') && (op2 == '+' || op2 == '-')) {
else if ((op1 == '/' || op1 == '*') && (op2 == '+' || op2 == '-')) {
return false;
}
else {
Expand Down Expand Up @@ -285,7 +281,7 @@ namespace Pebbles {

// If token is an operator
else if (is_operator(tokens[i])) {
while (!r_l_associative (tokens[i]) && !ops.empty() && has_precedence(tokens[i].get(0), ops.peek())) {
while (!r_l_associative (tokens[i]) && !ops.empty() && has_precedence_pemdas(tokens[i].get(0), ops.peek())) {
string tmp = apply_op(ops.pop(), values.pop(), values.pop());
if (tmp != "E") {
values.push(double.parse(tmp));
Expand All @@ -311,35 +307,7 @@ namespace Pebbles {

// Take care of float accuracy of the result
string output = Utils.manage_decimal_places (values.pop (), float_accuracy);

// Remove trailing 0s and decimals
while (output.has_suffix ("0")) {
output = output.slice (0, -1);
}
if (output.has_suffix (".")) {
output = output.slice (0, -1);
}

// Insert separator symbol in large numbers
StringBuilder output_builder = new StringBuilder (output);
var decimalPos = output.last_index_of (".");
if (decimalPos == -1) {
decimalPos = output.length;
}
int end_position = 0;

// Take care of minus sign at the beginning of string, if any
if (output.has_prefix ("-")) {
end_position = 1;
}
for (int i = decimalPos - 3; i > end_position; i -= 3) {
output_builder.insert (i, ",");
}

if (output_builder.str == "-0") {
return "0";
}
return output_builder.str;
return output;
}
private static bool r_l_associative (string operator) {
if (operator == "u" || operator == "^" || operator == "") {
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Statistics.vala
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace Pebbles
}

private void string_splitter (string input_vals){
tokens = input_vals.split(",");
tokens = input_vals.split("[&&]");
x = new double [tokens.length];
for(int i = 0; i < tokens.length; i++) {
x[i] = double.parse(tokens[i]);
Expand Down
56 changes: 52 additions & 4 deletions src/Core/Utils.vala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,50 @@

namespace Pebbles {
public class Utils {
public static string get_local_radix_symbol () {
return Posix.nl_langinfo (Posix.NLItem.RADIXCHAR);
}

public static string get_local_separator_symbol () {
return Posix.nl_langinfo (Posix.NLItem.THOUSEP);
}

public static string format_result (string result) {
string output = result.replace (".", Utils.get_local_radix_symbol ());

// Remove trailing 0s and decimals
while (output.has_suffix ("0")) {
output = output.slice (0, -1);
}
if (output.has_suffix (Utils.get_local_radix_symbol ())) {
output = output.slice (0, -1);
}

// Insert separator symbol in large numbers
StringBuilder output_builder = new StringBuilder (output);
var decimalPos = output.last_index_of (Utils.get_local_radix_symbol ());
if (decimalPos == -1) {
decimalPos = output.length;
}
int end_position = 0;

// Take care of minus sign at the beginning of string, if any
if (output.has_prefix ("-")) {
end_position = 1;
}
for (int i = decimalPos - 3; i > end_position; i -= 3) {
output_builder.insert (i, Utils.get_local_separator_symbol ());
}

if (output_builder.str == "-0") {
return "0";
}
if (output_builder.str == "nan")
output_builder.str = "E";
if (output_builder.str == "inf")
output_builder.str = "";
return output_builder.str;
}
public static bool check_parenthesis (string exp) {
int bracket_balance = 0;
for (int i = 0; i < exp.length; i++) {
Expand Down Expand Up @@ -156,20 +200,24 @@ namespace Pebbles {
return result;
}
private static string uniminus_convert (string exp) {
print(">%s<\n", exp);
string uniminus_converted = "";
string[] tokens = exp.split (" ");
for (int i = 1; i < tokens.length; i++) {
for (int i = 0; i < tokens.length; i++) {
if (tokens[i] == "-") {
if (tokens [i - 1] == ")" || tokens [i - 1] == "x" || is_number (tokens [i - 1]) ) {
print("token: %d\n", i);
if (i == 0) {
tokens [i] = "u";
} else if (tokens [i - 1] == ")" || tokens [i - 1] == "x" || is_number (tokens [i - 1]) ) {
tokens [i] = "-";
}
else {
} else {
tokens [i] = "u";
}
}
}
uniminus_converted = string.joinv (" ", tokens);
uniminus_converted = uniminus_converted.replace ("u", "0 u");
print("converted: %s\n", uniminus_converted);
return uniminus_converted;
}

Expand Down
4 changes: 3 additions & 1 deletion src/KeyboardHandler.vala
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ namespace Pebbles {
RETURN = 65293,
RETURN_NUMPAD= 65421,
ESCAPE = 65307,
SPACE_BAR = 32
SPACE_BAR = 32,

CTRL = 65507
}
public static bool key_is_number (uint key) {
if (((key >= KeyMap.NUMPAD_0) && (key <= KeyMap.NUMPAD_9))
Expand Down
18 changes: 15 additions & 3 deletions src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ namespace Pebbles {
// Keyboard Events
Gdk.Keymap keymap;
bool keyboard_shift_status;
private bool ctrl_held = false;

public MainWindow () {
load_settings ();
Expand Down Expand Up @@ -856,18 +857,26 @@ namespace Pebbles {
this.angle_unit_button_label_update ();
}
}
if (event.keyval == KeyboardHandler.KeyMap.CTRL) {
ctrl_held = true;
}
if(event.keyval == KeyboardHandler.KeyMap.V_LOWER || event.keyval == KeyboardHandler.KeyMap.V_UPPER) {
if (ctrl_held) {
return false;
}
}
return true;
});
key_release_event.connect ((event) => {
switch (settings.view_index) {
case 0:
scientific_view.key_released ();
scientific_view.key_released (event);
break;
case 2:
calculus_view.key_released ();
calculus_view.key_released (event);
break;
case 4:
statistics_view.key_released ();
statistics_view.key_released (event);
break;
case 5:
conv_length_view.key_release_event (event);
Expand Down Expand Up @@ -912,6 +921,9 @@ namespace Pebbles {
if (event.keyval == 65505) {
keyboard_shift_status = false;
}
if (event.keyval == KeyboardHandler.KeyMap.CTRL) {
ctrl_held = false;
}
return false;
});
}
Expand Down
Loading

0 comments on commit e302bf3

Please sign in to comment.