diff options
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDS.java')
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDS.java | 121 |
1 files changed, 81 insertions, 40 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 3076415..e1e3baa 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,11 +1,19 @@ package bjc.utils.cli.fds; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.InputStreamReader; import java.io.PrintStream; +import java.util.Collection; import bjc.utils.cli.CommandHelp; +import bjc.utils.cli.GenericHelp; import bjc.utils.cli.fds.FDSState.InputMode; import bjc.utils.ioutils.Block; import bjc.utils.ioutils.BlockReader; +import bjc.utils.ioutils.PushbackBlockReader; + +import static bjc.utils.ioutils.BlockReaders.*; /** * Runs a FDS (FDiskScript) interface. @@ -19,21 +27,19 @@ import bjc.utils.ioutils.BlockReader; * */ public class FDS { + private static SimpleFDSMode<?> miscMode; + + static { + miscMode = new SimpleFDSMode<>(); + + miscMode.addCommand('X', (stat) -> { + + }, new GenericHelp("load-script\tLoad a script from a file", "")); + } + /** * 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. - * - * @param datain - * The data input source for the FDS mode. - * - * @param printer - * The output source for the FDS mode. - * - * @param mode - * The mode to start in. - * * @param state * The initial state for the mode. * @@ -42,26 +48,26 @@ public class FDS { * @throws FDSException * If something went wrong during mode execution. */ - public static <S> S runFDS(BlockReader blockSource, BlockReader datain, PrintStream printer, FDSState<S> state) - throws FDSException { - while(blockSource.hasNext() && !state.modes.empty()) { + public static <S> S runFDS(FDSState<S> state) throws FDSException { + BlockReader blockSource = state.comin; + + while (blockSource.hasNext() && !state.modes.empty()) { Block comBlock = blockSource.next(); - handleCommandString(comBlock, blockSource, datain, printer, state); + handleCommandString(comBlock, state); } return state.state; } - private static <S> void handleCommandString(Block comBlock, BlockReader blockSource, BlockReader datain, - PrintStream printer, FDSState<S> state) throws FDSException { + private static <S> void handleCommandString(Block comBlock, FDSState<S> 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); + handleCommand(comString.charAt(0), state); break; case INLINE: break; @@ -73,39 +79,40 @@ public class FDS { } private static <S> void chordCommand(Block comBlock, FDSState<S> state, String comString) { - for(int i = 1; i < comString.length(); i++) { + PushbackBlockReader source = state.comin; + + 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, comBlock.startLine); - state.enqueCommand.accept(newCom); + source.addBlock(newCom); } } - private static <S> void handleCommand(char com, BlockReader blockSource, BlockReader datain, - PrintStream printer, FDSState<S> state) throws FDSException { - if(state.modes.empty()) return; + @SuppressWarnings("unchecked") + private static <S> void handleCommand(char com, FDSState<S> state) throws FDSException { + if (state.modes.empty()) return; + + PrintStream printer = state.printer; /* * 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"); } break; case 'X': - /* - * TODO implement loading scripts from file. - */ + loadScript(state); break; - case 'q': state.modes.drop(); break; @@ -115,28 +122,62 @@ public class FDS { case 'm': helpSummary(printer, state); break; + case 'M': + /* + * We'll see if this cast actually causes any issues. + * + * None of the commands in misc. mode should depend on + * the state type, so it shouldn't matter. + */ + state.modes.push((FDSMode<S>) miscMode); + break; default: FDSMode<S> curMode = state.modes.top(); - if(curMode.hasSubmode(com)) { - curMode.getCommand(com).run(state.state, datain); - } else if(curMode.hasCommand(com)) { + if (curMode.hasSubmode(com)) { state.modes.push(curMode.getSubmode(com)); + } else if (curMode.hasCommand(com)) { + curMode.getCommand(com).run(state); } else { printer.printf("? UBC '%s'\n", com); } } } + private static <S> void loadScript(FDSState<S> state) { + String fileName = state.datain.next().contents.split(" ")[0]; + + PrintStream printer = state.printer; + + try { + FileInputStream fis = new FileInputStream(fileName); + + BlockReader reader = simple("\\R", new InputStreamReader(fis)); + + state.comin = pushback(serial(reader, state.comin)); + state.datain = pushback(serial(reader, state.datain)); + } catch (FileNotFoundException fnfex) { + printer.printf("? FNF '%s'\n", fileName); + } + } + private static <S> void helpSummary(PrintStream printer, FDSState<S> state) { FDSMode<S> 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()); + for (char bound : mode.registeredChars()) { + Collection<CommandHelp> help = mode.getHelp(bound); + + if (help.size() > 1) { + for (CommandHelp hlp : help) { + printer.printf("%s\t-\t%s", hlp.getSummary()); + } + } else { + CommandHelp hlp = help.iterator().next(); + + printer.printf("%s\t-\t%s", hlp.getSummary()); + } } } -} +}
\ No newline at end of file |
