|
30 | 30 | import org.fusesource.jansi.Ansi;
|
31 | 31 | import org.jline.reader.UserInterruptException;
|
32 | 32 |
|
| 33 | +import java.io.File; |
| 34 | +import java.io.FileInputStream; |
| 35 | +import java.io.FileOutputStream; |
| 36 | +import java.io.IOException; |
33 | 37 | import java.util.ArrayList;
|
34 | 38 | import java.util.prefs.BackingStoreException;
|
35 | 39 | import java.util.prefs.Preferences;
|
@@ -133,31 +137,116 @@ public static void cmdRecord(String args) {
|
133 | 137 | */
|
134 | 138 | public static void cmdFunction(String args) {
|
135 | 139 | String[] command = args.toLowerCase().trim().split("\\s", 2);
|
| 140 | + String fileNameStr = ""; |
136 | 141 |
|
137 | 142 | try {
|
138 |
| - if (command[0].equals("del")) { |
139 |
| - try { |
140 |
| - FunctionDelete(command[1]); |
141 |
| - } catch (ArrayIndexOutOfBoundsException ex) { |
142 |
| - Output.printColorln(Ansi.Color.RED, "ERROR: 'function del' requires a valid function name to delete"); |
143 |
| - } |
| 143 | + switch (command[0]) { |
| 144 | + case "del": |
| 145 | + try { |
| 146 | + FunctionDelete(command[1]); |
| 147 | + } catch (ArrayIndexOutOfBoundsException ex) { |
| 148 | + Output.printColorln(Ansi.Color.RED, "ERROR: 'function del' requires a valid function name to delete"); |
| 149 | + } |
| 150 | + break; |
144 | 151 |
|
145 |
| - } else if (command[0].equals("delall")) { |
146 |
| - Preferences p = Preferences.userRoot().node(UserFunctions.PREFS_PATH_FUNCTIONS); |
| 152 | + case "delall": |
| 153 | + Preferences p = Preferences.userRoot().node(UserFunctions.PREFS_PATH_FUNCTIONS); |
147 | 154 |
|
148 |
| - // Loop through each function (child of the root) and delete it |
149 |
| - try { |
150 |
| - for (String functionName : p.childrenNames()) { |
151 |
| - Output.debugPrintln("Removing function: " + functionName); |
152 |
| - FunctionDelete(functionName); |
| 155 | + // Loop through each function (child of the root) and delete it |
| 156 | + try { |
| 157 | + for (String functionName : p.childrenNames()) { |
| 158 | + Output.debugPrintln("Removing function: " + functionName); |
| 159 | + FunctionDelete(functionName); |
| 160 | + } |
| 161 | + } catch (BackingStoreException e) { |
| 162 | + Output.printColorln(Ansi.Color.RED, "Error: Could not remove the user defined functions"); |
153 | 163 | }
|
154 |
| - } catch (BackingStoreException e) { |
155 |
| - Output.printColorln(Ansi.Color.RED, "Error: Could not remove the user defined functions"); |
156 |
| - } |
| 164 | + break; |
157 | 165 |
|
158 |
| - } else { |
159 |
| - Output.printColorln(Ansi.Color.RED, "ERROR: Illegal argument for function command. Please see help"); |
| 166 | + case "export": |
| 167 | + Output.printColorln(Ansi.Color.YELLOW, "Please enter the export filename. A blank name will cancel the export"); |
| 168 | + Output.printColorln(Ansi.Color.YELLOW, "Note, if needed, please use use '/' as the path separator"); |
| 169 | + |
| 170 | + // Read the user input. If ctrl-C is entered, discard the function |
| 171 | + try { |
| 172 | + fileNameStr = Main.scanner.readLine(Main.INPUT_PROMPT); |
| 173 | + |
| 174 | + } catch (UserInterruptException ex) { |
| 175 | + fileNameStr = ""; |
| 176 | + } catch (Exception e) { |
| 177 | + Output.fatalError("Could not read user input", 5); |
| 178 | + } |
| 179 | + |
| 180 | + // If no name is given, cancel export |
| 181 | + if (fileNameStr.isBlank()) { |
| 182 | + Output.printColorln(Ansi.Color.YELLOW, "Canceling export"); |
| 183 | + break; |
| 184 | + } |
| 185 | + |
| 186 | + // Output the full path/name of the output file |
| 187 | + Output.printColorln(Ansi.Color.CYAN, "Export filename: '" + new File(fileNameStr).getAbsolutePath() + "'"); |
| 188 | + |
| 189 | + // Output preferences as an XML file |
| 190 | + try { |
| 191 | + FileOutputStream fos = new FileOutputStream(fileNameStr); |
| 192 | + Preferences pExport = Preferences.userRoot().node(PREFS_PATH_FUNCTIONS); |
| 193 | + pExport.exportSubtree(fos); |
| 194 | + fos.close(); |
| 195 | + |
| 196 | + } catch (IOException ex) { |
| 197 | + Output.printColorln(Ansi.Color.RED, "Error: RPNCalc can't write to '" + fileNameStr + "'"); |
| 198 | + } catch (BackingStoreException ex) { |
| 199 | + Output.printColorln(Ansi.Color.RED, "Error: Could not export the user defined functions (BackingStoreException)"); |
| 200 | + } catch (IllegalStateException ex) { |
| 201 | + Output.printColorln(Ansi.Color.RED, "Error: Could not export the user defined functions (IllegalStateException)"); |
| 202 | + } catch (Exception ex) { |
| 203 | + Output.printColorln(Ansi.Color.RED, "Error: Could not export the user defined functions (Exception)"); |
| 204 | + } |
| 205 | + break; |
| 206 | + |
| 207 | + case "import": |
| 208 | + Output.printColorln(Ansi.Color.YELLOW, "Please enter the import filename. A blank name will cancel the import"); |
| 209 | + Output.printColorln(Ansi.Color.YELLOW, "Note, if needed, please use use '/' as the path separator"); |
| 210 | + |
| 211 | + // Read the user input. If ctrl-C is entered, discard the function |
| 212 | + try { |
| 213 | + fileNameStr = Main.scanner.readLine(Main.INPUT_PROMPT); |
| 214 | + |
| 215 | + } catch (UserInterruptException ex) { |
| 216 | + fileNameStr = ""; |
| 217 | + } catch (Exception e) { |
| 218 | + Output.fatalError("Could not read user input", 5); |
| 219 | + } |
| 220 | + |
| 221 | + // If no name is given, cancel export |
| 222 | + if (fileNameStr.isBlank()) { |
| 223 | + Output.printColorln(Ansi.Color.YELLOW, "Canceling import"); |
| 224 | + break; |
| 225 | + } |
| 226 | + |
| 227 | + // Import preferences from the XML file |
| 228 | + try { |
| 229 | + FileInputStream fis = new FileInputStream(fileNameStr); |
| 230 | + Preferences pImport = Preferences.userRoot().node(PREFS_PATH_FUNCTIONS); |
| 231 | + pImport.importPreferences(fis); |
| 232 | + fis.close(); |
| 233 | + |
| 234 | + Output.printColorln(Ansi.Color.CYAN, "Import complete"); |
| 235 | + |
| 236 | + } catch (IOException ex) { |
| 237 | + Output.printColorln(Ansi.Color.RED, "Error: RPNCalc can't read from '" + fileNameStr + "'"); |
| 238 | + } catch (IllegalStateException ex) { |
| 239 | + Output.printColorln(Ansi.Color.RED, "Error: Could not import the user defined functions (IllegalStateException)"); |
| 240 | + } catch (Exception ex) { |
| 241 | + Output.printColorln(Ansi.Color.RED, "Error: Could not import the user defined functions (Exception)"); |
| 242 | + } |
| 243 | + break; |
| 244 | + |
| 245 | + default: |
| 246 | + Output.printColorln(Ansi.Color.RED, "ERROR: Illegal argument for function command. Please see help"); |
| 247 | + break; |
160 | 248 | }
|
| 249 | + |
161 | 250 | } catch (StringIndexOutOfBoundsException ex) {
|
162 | 251 | // User did not enter a command
|
163 | 252 | Output.printColorln(Ansi.Color.RED, "ERROR: An argument is requirement for function command. Please see help");
|
@@ -215,7 +304,7 @@ public static void RemoveItemFromRecording(int index) {
|
215 | 304 | * RemoveItemFromRecording(): If no argument is given, remove the last item
|
216 | 305 | */
|
217 | 306 | public static void RemoveItemFromRecording() {
|
218 |
| - RemoveItemFromRecording(recording.size() -1); |
| 307 | + RemoveItemFromRecording(recording.size() - 1); |
219 | 308 | }
|
220 | 309 |
|
221 | 310 | /**
|
|
0 commit comments