From 4139a9dbef1463a3401f1a5eaff5e05a7ceab5c5 Mon Sep 17 00:00:00 2001 From: Michael Fross Date: Wed, 7 Aug 2024 14:40:44 -0500 Subject: [PATCH] Corrected decimal alignment display bug - Adding number without decimal would not align - Added a test for the new queryDecimalIndex() method - Updated pom.xml to ignore windows error on `chmod` for exec-maven-plugin - Updated maven exec plugin from 3.3.0 -> 3.4.0 - Updated JUnit from 5.11.0-M2 -> 5.11.0-RC1 --- pom.xml | 13 ++++++---- snap/snapcraft.yaml | 2 +- src/main/java/org/fross/rpncalc/Display.java | 17 ++++++++++++- src/main/java/org/fross/rpncalc/Main.java | 25 +++++++++---------- .../java/org/fross/rpncalc/DisplayTest.java | 15 +++++++++-- 5 files changed, 50 insertions(+), 22 deletions(-) diff --git a/pom.xml b/pom.xml index 457f952..2ae4cce 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ org.fross rpncalc - 5.2.9 + 5.2.10 jar rpncalc @@ -208,7 +208,7 @@ org.codehaus.mojo exec-maven-plugin - 3.3.0 + 3.4.0 chmod @@ -220,9 +220,12 @@ chmod +x - - ${project.build.directory}/${project.name}.jar + ${project.build.directory}/${project.name}.jar + + 0 + 1 + @@ -273,7 +276,7 @@ org.junit.jupiter junit-jupiter - 5.11.0-M2 + 5.11.0-RC1 test diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index aa4eab4..f6cfbfd 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -1,5 +1,5 @@ name: rpncalc -version: '5.2.9' +version: '5.2.10' summary: The command line Reverse Polish Notation (RPN) calculator description: | RPNCalc is an easy to use command line based Reverse Polish diff --git a/src/main/java/org/fross/rpncalc/Display.java b/src/main/java/org/fross/rpncalc/Display.java index 49675de..0d52141 100644 --- a/src/main/java/org/fross/rpncalc/Display.java +++ b/src/main/java/org/fross/rpncalc/Display.java @@ -42,10 +42,25 @@ public class Display { */ @SuppressWarnings("MalformedFormatString") // No idea what's wrong with the return statement public static String Comma(BigDecimal bd) { - Output.debugPrintln("BigDecimal Scale: " + bd.scale()); + // Output.debugPrintln("Comma: BigDecimal Scale: " + bd.scale()); return String.format("%,." + bd.scale() + "f", bd); } + /** + * queryDecimalIndex(): Return the index of the decimal in a string. If none is given assume it's at the end. + * + * @param str - String with or without a decimal point in it + * @return An integer with the location of the decimal (or the end if it doesn't exist) + */ + public static int queryDecimalIndex(String str) { + // Determine where the decimal point is located. If no decimal exists (-1) assume it's at the end + int di = str.indexOf("."); + if (di == -1) { + di = str.length(); + } + return di; + } + /** * DisplayStatusLine(): Display the last line of the header and the separator line. This is a separate function given it also * inserts the loaded stack and spaces everything correctly. diff --git a/src/main/java/org/fross/rpncalc/Main.java b/src/main/java/org/fross/rpncalc/Main.java index a57f9aa..60b06e9 100644 --- a/src/main/java/org/fross/rpncalc/Main.java +++ b/src/main/java/org/fross/rpncalc/Main.java @@ -150,8 +150,8 @@ public static void main(String[] args) { currentStackItem = Display.Comma(calcStack.get(i)); } - // Determine where the decimal point is located - decimalIndex = currentStackItem.indexOf("."); + // Determine where the decimal point is located. If no decimal exists (-1) assume it's at the end + decimalIndex = Display.queryDecimalIndex(currentStackItem); // If current stack item has more digits ahead of decimal make that the max - commas are included. if (maxDigitsBeforeDecimal < decimalIndex) { @@ -165,9 +165,9 @@ public static void main(String[] args) { } - // Uncomment to debug alignment issues - // Output.debugPrintln("Alignment: Max digits before the decimal: " + maxDigitsBeforeDecimal); - // Output.debugPrintln("Alignment: Max length of longest item in stack: " + maxLenOfNumbers); + // Output information for alignment debugging + Output.debugPrintln("Alignment: Max digits before the decimal: " + maxDigitsBeforeDecimal); + Output.debugPrintln("Alignment: Max length of longest item in stack: " + maxLenOfNumbers); // Display the current stack contents for (int i = 0; i < calcStack.size(); i++) { @@ -184,18 +184,17 @@ public static void main(String[] args) { String stkLineNumber = String.format("%0" + LINE_NUMBER_DIGITS + "d: ", calcStack.size() - i); Output.printColor(Ansi.Color.CYAN, stkLineNumber); - // Decimal Alignment - insert spaces before number to align to the decimal point + // DECIMAL ALIGNMENT: Insert spaces before number to align to the decimal point if (configAlignment.compareTo("d") == 0) { - for (int k = 0; k < (maxDigitsBeforeDecimal - currentStackItem.indexOf(".")); k++) { - Output.print(" "); - } + int decimalIndex = Display.queryDecimalIndex(currentStackItem); + + // Output the spaces in front so the decimals align + Output.print(" ".repeat(maxDigitsBeforeDecimal - decimalIndex)); } - // Right Alignment - insert spaces before number to right align + // RIGHT ALIGNMENT: Insert spaces before number to right align if (configAlignment.compareTo("r") == 0) { - for (int k = 0; k < (maxLenOfNumbers - currentStackItem.length()); k++) { - Output.print(" "); - } + Output.print(" ".repeat(maxLenOfNumbers - currentStackItem.length())); } // Now that the spaces are inserted (for decimal/right) display the number diff --git a/src/test/java/org/fross/rpncalc/DisplayTest.java b/src/test/java/org/fross/rpncalc/DisplayTest.java index ca82e1d..e1f535d 100644 --- a/src/test/java/org/fross/rpncalc/DisplayTest.java +++ b/src/test/java/org/fross/rpncalc/DisplayTest.java @@ -33,10 +33,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals; public class DisplayTest { - @Test void testComma() { - assertEquals("1", Display.Comma(new BigDecimal("1"))); assertEquals("1.01", Display.Comma(new BigDecimal("1.01"))); assertEquals("-2.000000001231", Display.Comma(new BigDecimal("-2.000000001231"))); @@ -46,4 +44,17 @@ void testComma() { assertEquals("-1,987,654,321,987,654,321", Display.Comma(new BigDecimal("-1987654321987654321"))); } + @Test + void testQueryDecimalIndex() { + assertEquals(6, Display.queryDecimalIndex("50,000")); + assertEquals(1, Display.queryDecimalIndex("1")); + assertEquals(1, Display.queryDecimalIndex("1.01")); + assertEquals(5, Display.queryDecimalIndex("-1234.4321")); + assertEquals(0, Display.queryDecimalIndex(".00001")); + assertEquals(18, Display.queryDecimalIndex("-1,123,454,678,999.1234532643")); + assertEquals(5, Display.queryDecimalIndex("-8e21")); + assertEquals(1, Display.queryDecimalIndex("1.02e+22")); + assertEquals(6, Display.queryDecimalIndex("-1e+22")); + } + }