From faedf1fe8c22e4ccfa375951166bd1a41a4e3b94 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Sun, 26 Mar 2017 23:00:28 -0400 Subject: More FDS work --- .../src/main/java/bjc/utils/cli/fds/FDS.java | 62 ++++++++++------ .../main/java/bjc/utils/cli/fds/FDSCommand.java | 4 +- .../src/main/java/bjc/utils/cli/fds/FDSState.java | 85 ++++++++++++++++++++++ 3 files changed, 126 insertions(+), 25 deletions(-) create mode 100644 BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDSState.java (limited to 'BJC-Utils2/src/main/java') 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 runFDS(InputStream comin, InputStream datain, OutputStream out, FDSMode initialMode, - S initialState) throws FDSException { - PrintStream printer = new PrintStream(out); + public static S runFDS(BlockReader blockSource, BlockReader datain, PrintStream printer, FDSMode mode, + FDSState 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 void handleCommandString(Block comBlock, BlockReader blockSource, BlockReader datain, + PrintStream printer, FDSMode mode, FDSState 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 void handleCommand(char charAt, BlockReader blockSource, BlockReader datain, + PrintStream printer, FDSMode mode, FDSState 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 { * * @return The new state, after running the command. */ - S run(S state, Iterator 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 + * The state type of the interface. + */ +public class FDSState { + /** + * 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 enqueCommand; + + /** + * Function to add a data block to be processed. + */ + public Consumer 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 comQueue, Consumer dataQueue) { + state = stat; + mode = inputMode; + + enqueCommand = comQueue; + enqueData = dataQueue; + } +} \ No newline at end of file -- cgit v1.2.3