blob: ec8cea89e99c9442b9b9426eeccfbe3e9c616a27 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
package bjc.utils.cli.fds;
import static bjc.utils.ioutils.BlockReaders.pushback;
import static bjc.utils.ioutils.BlockReaders.simple;
import static bjc.utils.ioutils.BlockReaders.trigger;
import java.io.PrintStream;
import java.io.Reader;
import bjc.utils.cli.fds.FDSState.InputMode;
import bjc.utils.ioutils.BlockReader;
import bjc.utils.ioutils.Prompter;
import bjc.utils.ioutils.PushbackBlockReader;
/**
* Utilities for dealing with FDS
*
* @author bjculkin
*
*/
public class FDSUtils {
/**
* Run a FDS instance from a reader.
*
* @param reader
* The reader to use.
*
* @param out
* The output stream to use.
*
* @param mode
* The mode to use.
*
* @param ctx
* The initial state.
*
* @return The final state.
*
* @throws FDSException
* If something goes wrong.
*/
public static <S> S runFromReader(Reader reader, PrintStream out, FDSMode<S> mode, S ctx) throws FDSException {
BlockReader input = simple("\\R", reader);
Prompter comPrompter = new Prompter("Enter a command (m for help): ", out);
Prompter dataPrompter = new Prompter("> ", out);
BlockReader rawComInput = trigger(input, comPrompter);
BlockReader rawDataInput = trigger(input, dataPrompter);
PushbackBlockReader comInput = pushback(rawComInput);
PushbackBlockReader dataInput = pushback(rawDataInput);
FDSState<S> fdsState = new FDSState<>(ctx, InputMode.NORMAL, comInput, dataInput, out);
fdsState.modes.push(mode);
FDS.runFDS(fdsState);
return ctx;
}
}
|