summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDSUtils.java
blob: f07167143f44b2be665e812a639d261ba521df4f (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.INLINE, comInput, dataInput, out);
		fdsState.modes.push(mode);

		FDS.runFDS(fdsState);

		return ctx;
	}
}