summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/cli
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2017-03-26 23:00:28 -0400
committerbculkin2442 <bjculkin@mix.wvu.edu>2017-03-26 23:00:28 -0400
commitfaedf1fe8c22e4ccfa375951166bd1a41a4e3b94 (patch)
tree5f5be2ff2ca77bb0d02f21624268a8cc9dedc31e /BJC-Utils2/src/main/java/bjc/utils/cli
parent0040f420f5cc9a8daf8e7ebb2899dec88fdd7214 (diff)
More FDS work
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/cli')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDS.java62
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDSCommand.java4
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDSState.java85
3 files changed, 126 insertions, 25 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDS.java b/BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDS.java
index 6dc0337..91934be 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDS.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDS.java
@@ -1,9 +1,7 @@
package bjc.utils.cli.fds;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.OutputStream;
import java.io.PrintStream;
+
import bjc.utils.ioutils.Block;
import bjc.utils.ioutils.BlockReader;
@@ -22,19 +20,19 @@ public class FDS {
/**
* Run a provided FDS mode until it is exited or there is no more input.
*
- * @param comin
+ * @param blockSource
* The command input source for the FDS mode.
*
* @param datain
* The data input source for the FDS mode.
*
- * @param out
+ * @param printer
* The output source for the FDS mode.
*
- * @param initialMode
+ * @param mode
* The mode to start in.
*
- * @param initialState
+ * @param state
* The initial state for the mode.
*
* @return The final state of the mode.
@@ -42,28 +40,46 @@ public class FDS {
* @throws FDSException
* If something went wrong during mode execution.
*/
- public static <S> S runFDS(InputStream comin, InputStream datain, OutputStream out, FDSMode<S> initialMode,
- S initialState) throws FDSException {
- PrintStream printer = new PrintStream(out);
+ public static <S> S runFDS(BlockReader blockSource, BlockReader datain, PrintStream printer, FDSMode<S> mode,
+ FDSState<S> state) throws FDSException {
+ //printer.print("Enter a command (m for help): ");
+
+ while (blockSource.hasNext()) {
+ Block comBlock = blockSource.next();
+
+ handleCommandString(comBlock, blockSource, datain, printer, mode, state);
+
+ //printer.print("Enter a command (m for help): ");
+ }
- try (BlockReader blockSource = new BlockReader("\\R", new InputStreamReader(comin))) {
- printer.print("Enter a command (m for help): ");
+ return state.state;
+ }
- while (blockSource.hasNext()) {
- Block comBlock = blockSource.next();
+ private static <S> void handleCommandString(Block comBlock, BlockReader blockSource, BlockReader datain,
+ PrintStream printer, FDSMode<S> mode, FDSState<S> state) throws FDSException {
+ String comString = comBlock.contents.trim();
- String comString = comBlock.contents.trim();
-
- char comChar = comString.charAt(0);
-
- printer.println(String.format("\nRecieved command '%s'\n", comChar));
+ switch (state.mode) {
+ case CHORD:
+ if (comString.length() > 1) {
+ for (char c : comString.substring(1).toCharArray()) {
+ Block newCom = new Block(comBlock.blockNo + 1, Character.toString(c),
+ comBlock.startLine, comBlock.startLine);
- printer.print("Enter a command (m for help): ");
+ state.enqueCommand.accept(newCom);
+ }
}
- } catch (Exception ex) {
- throw new FDSException("Unexpected I/O error", ex);
+ case NORMAL:
+ handleCommand(comString.charAt(0), blockSource, datain, printer, mode, state);
+ break;
+
+ default:
+ throw new FDSException(String.format("Unknown input mode '%s'", state.mode));
}
+ }
- return initialState;
+ private static <S> void handleCommand(char charAt, BlockReader blockSource, BlockReader datain,
+ PrintStream printer, FDSMode<S> mode, FDSState<S> state) {
+ printer.printf("Recieved command '%s'\n", charAt);
}
}
diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDSCommand.java b/BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDSCommand.java
index 0a6d29c..ddbb743 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDSCommand.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDSCommand.java
@@ -1,6 +1,6 @@
package bjc.utils.cli.fds;
-import java.util.Iterator;
+import bjc.utils.ioutils.BlockReader;
/**
* A command attached to an FDS interface.
@@ -22,5 +22,5 @@ public interface FDSCommand<S> {
*
* @return The new state, after running the command.
*/
- S run(S state, Iterator<String> input);
+ S run(S state, BlockReader input);
}
diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDSState.java b/BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDSState.java
new file mode 100644
index 0000000..a0ad5e6
--- /dev/null
+++ b/BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDSState.java
@@ -0,0 +1,85 @@
+package bjc.utils.cli.fds;
+
+import java.util.function.Consumer;
+
+import bjc.utils.ioutils.Block;
+
+/**
+ * Internal state for an FDS interface.
+ *
+ * @author bjculkin
+ *
+ * @param <S>
+ * The state type of the interface.
+ */
+public class FDSState<S> {
+ /**
+ * The input mode for the interface.
+ *
+ * @author bjculkin
+ *
+ */
+ public static enum InputMode {
+ /**
+ * Normal mode.
+ *
+ * Reads only the first character in the block as a command.
+ */
+ NORMAL,
+ /**
+ * Reads every character in the block as a command.
+ */
+ CHORD,
+ /**
+ * Reads every character in the block, but after a terminal
+ * command, data will be read inline separated by spaces until a
+ * semicolon is read.
+ *
+ * The semicolon can be escaped with a backslash.
+ */
+ INLINE,
+ /**
+ * Reads every character in the block, but after a terminal
+ * command, data will be read inline with each character being a
+ * separate item until a semicolon is read.
+ *
+ * The semicolon can be escaped with a backslash.
+ */
+ }
+
+ /**
+ * The state of the interface
+ */
+ public S state;
+ /**
+ * The input mode for the interface.
+ */
+ public InputMode mode;
+
+ /**
+ * Function to add a command block to be processed.
+ */
+ public Consumer<Block> enqueCommand;
+
+ /**
+ * Function to add a data block to be processed.
+ */
+ public Consumer<Block> enqueData;
+
+ /**
+ * Create a new interface state.
+ *
+ * @param stat
+ * The initial state for the interface.
+ *
+ * @param inputMode
+ * The input mode for the interface.
+ */
+ public FDSState(S stat, InputMode inputMode, Consumer<Block> comQueue, Consumer<Block> dataQueue) {
+ state = stat;
+ mode = inputMode;
+
+ enqueCommand = comQueue;
+ enqueData = dataQueue;
+ }
+} \ No newline at end of file