Skip to content

Commit

Permalink
User Functions can now be run on cmd line without "func run"
Browse files Browse the repository at this point in the history
  • Loading branch information
frossm committed Jun 1, 2022
1 parent 27e4fc9 commit 179becc
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 25 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,11 @@ Simply add the value of the requested constant to the top of the stack / line1
|sol|Inserts the speed of light onto the stack in meters/second. **c = 299,792,458 m/s**|

## User Defined Functions
RPNCalc can record your commands and save them as a user defined function. You can then run this function on demand. So create your stack as you envision it being used in the future. Then enable recording, process the stack as you like, then stop the recording. Give a name to your user defined function and it will be saved. Then, you can build or import a stack, then run your function against it. These functions are stored in the preferences system much like the stacks and memory slots. `list func` will show a list of functions and their steps.
RPNCalc can record your commands and save them as a user defined function. You can then run this function on demand. You'll need to think about what data will need to be in your stack and what commands you wish to record.

Start by adding data to your stack that would emulate when you would run your function. This data will **not** be part of your recording. When you are ready, enter the `record on` command. Anything you add during this period will be recorded with the exception of the commands listed below. Enter your commands, numbers, etc. until you are done. Then enter `record off` to complete your recording. Give a name to your user defined function and it will be saved. Choose a name without spaces and that is easy to type when you wish to execute the function in the future.

Then you can run your function whever you like on the stack currently available. To run the function, simply type the name of your function as a standard command. To see a list of the saved functions, execute `list func` and it will display the name and the steps you recorded.

The following commands are not recorded.
- frac
Expand All @@ -207,7 +211,6 @@ The following commands are not recorded.
|-------|-----------|
|record on|Turn on recording. Most commands and numbers entered after record is enabled will be saved. There are some that are excluded from being recorded|
|record off| Turn off recording. The user will be prompted to enter in a name for this function and that name will be used to run it in the future. If you do not enter in a name the recording is canceled and nothing will be saved|
|func run|Execute the saved user defined function with the current stack as the starting point|
|func del NAME|Delete a saved function. The name must match the one given when saved. A list of functions can be viewed with `list func`. Undo will not recover a deleted function|
|func delall|Delete all saved user defined functions. Please note that undo will not recover deleted functions|

Expand Down
Binary file modified graphics/Banner.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified graphics/Banner.xcf
Binary file not shown.
Binary file modified graphics/ScreenShot.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.fross</groupId>
<artifactId>rpncalc</artifactId>
<version>3.2.8</version>
<version>3.3.0</version>
<packaging>jar</packaging>

<name>rpncalc</name>
Expand Down
2 changes: 1 addition & 1 deletion snap/snapcraft.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: rpncalc
version: '3.2.8'
version: '3.3.0'
summary: The command line Reverse Polish Notation (RPN) calculator
description: |
RPNCalc is an easy to use command line based Reverse Polish
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/org/fross/rpncalc/CommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,15 @@ public static void Parse(String cmdInput, String cmdInputCmd, String cmdInputPar
break;

default:
// Check for a fraction. If number entered contains a '/' but it's not at the
// end, then it must be a fraction.
if (cmdInput.contains("/") && !cmdInput.substring(cmdInput.length() - 1).matches("/")) {
// Determine if the command is a user defined function
// Verify user defined function exists
if (UserFunctions.FunctionExists(cmdInput) == true) {
Output.debugPrint("Executing User Defined Function: '" + cmdInput + "'");
UserFunctions.FunctionRun(cmdInput);

// Check for a fraction. If number entered contains a '/' but it's not at the end, then it must be a fraction.
} else if (cmdInput.contains("/") && !cmdInput.substring(cmdInput.length() - 1).matches("/")) {
Output.debugPrint("Fraction has been entered");
try {
long fracInteger = 0;
double fracDecimalEquiv = 0.0;
Expand Down
1 change: 0 additions & 1 deletion src/main/java/org/fross/rpncalc/Help.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ public static void Display() {
Output.printColorln(Ansi.Color.YELLOW, "\nUser Defined Functions:");
Output.printColorln(Ansi.Color.WHITE, " record on Turn on command recording");
Output.printColorln(Ansi.Color.WHITE, " record off Disable recording");
Output.printColorln(Ansi.Color.WHITE, " func run NAME Run the named user function on current stack");
Output.printColorln(Ansi.Color.WHITE, " func del NAME Delete named user defined function");
Output.printColorln(Ansi.Color.WHITE, " func delall Delete all user defined functions");

Expand Down
22 changes: 5 additions & 17 deletions src/main/java/org/fross/rpncalc/UserFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,6 @@ public static void cmdFunction(String args) {
Output.printColorln(Ansi.Color.RED, "Error: Could not remove the user defined functions");
return;
}

} else if (command[0].equals("run")) {
try {
FunctionRun(command[1]);
} catch (ArrayIndexOutOfBoundsException ex) {
Output.printColorln(Ansi.Color.RED, "ERROR: 'function run' requires a valid function name to execute");
return;
}

} else {
Output.printColorln(Ansi.Color.RED, "ERROR: Illegal argument for function command. Please see help");
Expand Down Expand Up @@ -144,8 +136,8 @@ public static void RecordCommand(String arg) {
}

/**
* RemoveItemFromRecording(): Remove the value at index from the recording Most likely used if user
* inputs an invalid command
* RemoveItemFromRecording(): Remove the value at index from the recording Most likely used if user inputs an invalid
* command
*
* @param index
*/
Expand Down Expand Up @@ -225,20 +217,16 @@ public static void FunctionDelete(String fname) {
} catch (BackingStoreException e) {
Output.printColorln(Ansi.Color.RED, "ERROR: Could not remove the function named: " + fname);
}

Output.printColorln(Ansi.Color.CYAN, "User defined function deleted: '" + fname + "'");
}

/**
* FunctionRun(): Execute the user defined function provided
* FunctionRun(): Execute the user defined function provided. Assumes functionName has been checked and is valid
*
* @param func
*/
public static void FunctionRun(String functionName) {
// Verify user defined function exists
if (FunctionExists(functionName) == false) {
Output.printColorln(Ansi.Color.RED, "ERROR: '" + functionName + "' is not a valid user defined function");
return;
}

Preferences pChild = Preferences.userRoot().node(PREFS_PATH_FUNCTIONS + "/" + functionName);
Output.printColorln(Ansi.Color.CYAN, "Executing User Defined Function: '" + functionName + "'");

Expand Down

0 comments on commit 179becc

Please sign in to comment.