From 4da4974456179168125fd78e5bde09c2de81b258 Mon Sep 17 00:00:00 2001 From: bjculkin Date: Mon, 27 Mar 2017 18:58:46 -0400 Subject: Work on FDS more --- .../src/main/java/bjc/utils/cli/CommandHelp.java | 2 +- .../src/main/java/bjc/utils/cli/fds/FDS.java | 63 ++++++++++++++++------ .../src/main/java/bjc/utils/cli/fds/FDSMode.java | 9 ++++ .../src/main/java/bjc/utils/cli/fds/FDSState.java | 15 ++++-- 4 files changed, 67 insertions(+), 22 deletions(-) (limited to 'BJC-Utils2/src/main') diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/CommandHelp.java b/BJC-Utils2/src/main/java/bjc/utils/cli/CommandHelp.java index 327fb75..68ed41a 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/CommandHelp.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/CommandHelp.java @@ -18,7 +18,7 @@ public interface CommandHelp { * Get the summary line for a command. * * A summary line should consist of a string of the following format - * "\t" where anything in angle brackets + *
"<command-name>\t<command-summary>"
where anything in angle brackets * should be filled in. * * @return The summary line line for a command 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 effc200..3076415 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 @@ -2,6 +2,7 @@ package bjc.utils.cli.fds; import java.io.PrintStream; +import bjc.utils.cli.CommandHelp; import bjc.utils.cli.fds.FDSState.InputMode; import bjc.utils.ioutils.Block; import bjc.utils.ioutils.BlockReader; @@ -22,28 +23,28 @@ public class FDS { * Run a provided FDS mode until it is exited or there is no more input. * * @param blockSource - * The command input source for the FDS mode. + * The command input source for the FDS mode. * * @param datain - * The data input source for the FDS mode. + * The data input source for the FDS mode. * * @param printer - * The output source for the FDS mode. + * The output source for the FDS mode. * * @param mode - * The mode to start in. + * The mode to start in. * * @param state - * The initial state for the mode. + * The initial state for the mode. * * @return The final state of the mode. * * @throws FDSException - * If something went wrong during mode execution. + * If something went wrong during mode execution. */ public static S runFDS(BlockReader blockSource, BlockReader datain, PrintStream printer, FDSState state) throws FDSException { - while (blockSource.hasNext()) { + while(blockSource.hasNext() && !state.modes.empty()) { Block comBlock = blockSource.next(); handleCommandString(comBlock, blockSource, datain, printer, state); @@ -56,19 +57,23 @@ public class FDS { PrintStream printer, FDSState state) throws FDSException { String comString = comBlock.contents.trim(); - switch (state.mode) { + switch(state.mode) { case CHORD: chordCommand(comBlock, state, comString); case NORMAL: handleCommand(comString.charAt(0), blockSource, datain, printer, state); break; + case INLINE: + break; + case CHARINLINE: + break; default: throw new FDSException(String.format("Unknown input mode '%s'", state.mode)); } } private static void chordCommand(Block comBlock, FDSState state, String comString) { - for (int i = 1; i < comString.length(); i++) { + for(int i = 1; i < comString.length(); i++) { char c = comString.charAt(i); Block newCom = new Block(comBlock.blockNo + 1, Character.toString(c), comBlock.startLine, @@ -78,16 +83,18 @@ public class FDS { } } - private static void handleCommand(char com, BlockReader blockSource, BlockReader datain, PrintStream printer, - FDSState state) throws FDSException { + private static void handleCommand(char com, BlockReader blockSource, BlockReader datain, + PrintStream printer, FDSState state) throws FDSException { + if(state.modes.empty()) return; + /* * Handle built-in commands over user commands. */ - switch (com) { + switch(com) { case 'x': - if (state.mode == InputMode.CHORD) { + if(state.mode == InputMode.CHORD) { state.mode = InputMode.NORMAL; - } else if (state.mode == InputMode.NORMAL) { + } else if(state.mode == InputMode.NORMAL) { state.mode = InputMode.CHORD; } else { printer.println("? CNV\n"); @@ -98,16 +105,38 @@ public class FDS { * TODO implement loading scripts from file. */ break; + + case 'q': + state.modes.drop(); + break; + case 'Q': + state.modes.drop(state.modes.size()); + break; + case 'm': + helpSummary(printer, state); + break; default: FDSMode curMode = state.modes.top(); - if (curMode.hasSubmode(com)) { + if(curMode.hasSubmode(com)) { curMode.getCommand(com).run(state.state, datain); - } else if (curMode.hasCommand(com)) { + } else if(curMode.hasCommand(com)) { state.modes.push(curMode.getSubmode(com)); } else { - printer.printf("? UBC '%s'", com); + printer.printf("? UBC '%s'\n", com); } } } + + private static void helpSummary(PrintStream printer, FDSState state) { + FDSMode mode = state.modes.top(); + + printer.printf("Help for mode %s:\n", mode.getName()); + + for(char bound : mode.registeredChars()) { + CommandHelp help = mode.getHelp(bound); + + printer.printf("%s\t-\t%s", help.getSummary()); + } + } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDSMode.java b/BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDSMode.java index acdcbcb..29857e0 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDSMode.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDSMode.java @@ -12,6 +12,15 @@ import bjc.utils.cli.NullHelp; * The FDS state type. */ public interface FDSMode { + /** + * Get the name of this mode. + * + * @return The mode of this name. + */ + default String getName() { + return "Unnamed Mode"; + } + /** * Get all the characters that are registered to something in this mode. * 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 index 4a6e1c6..bef86ae 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDSState.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDSState.java @@ -34,7 +34,7 @@ public class FDSState { CHORD, /** * Reads every character in the block, but after a terminal - * command, data will be read inline separated by spaces until a + * command, data will be read in-line separated by spaces until a * semicolon is read. * * The semicolon can be escaped with a backslash. @@ -42,11 +42,12 @@ public class FDSState { INLINE, /** * Reads every character in the block, but after a terminal - * command, data will be read inline with each character being a + * command, data will be read in-line with each character being a * separate item until a semicolon is read. * * The semicolon can be escaped with a backslash. */ + CHARINLINE, } /** @@ -62,7 +63,7 @@ public class FDSState { * The modes being used. */ public Stack> modes; - + /** * Function to add a command block to be processed. */ @@ -81,13 +82,19 @@ public class FDSState { * * @param inputMode * The input mode for the interface. + * + * @param comQueue + * The function to call to add a command block. + * + * @param dataQueue + * The function to call to add a data block. */ public FDSState(S stat, InputMode inputMode, Consumer comQueue, Consumer dataQueue) { state = stat; mode = inputMode; modes = new SimpleStack<>(); - + enqueCommand = comQueue; enqueData = dataQueue; } -- cgit v1.2.3