summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDSState.java
blob: e28e6bc1fe88ddee30450d3c394ded58f1b23797 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package bjc.utils.cli.fds;

import java.io.PrintStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

import bjc.utils.esodata.SimpleStack;
import bjc.utils.esodata.Stack;
import bjc.utils.ioutils.Block;
import bjc.utils.ioutils.PushbackBlockReader;

/**
 * Internal state for an FDS interface.
 * 
 * @author bjculkin
 *
 * @param <S>
 *                The state type of the interface.
 */
public class FDSState<S> {
	/**
	 * 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 in-line separated by spaces until
		 * a semicolon is read.
		 * 
		 * The semicolon can be escaped with a backslash.
		 */
		INLINE;
	}

	/**
	 * The state of the interface
	 */
	public S		state;
	/**
	 * The input mode for the interface.
	 */
	public InputMode	mode;

	/**
	 * The modes being used.
	 */
	public Stack<FDSMode<S>> modes;

	/**
	 * The source to read command blocks from.
	 */
	public PushbackBlockReader comin;

	/**
	 * The source to read data blocks from.
	 */
	public PushbackBlockReader datain;

	/**
	 * The destination for output.
	 */
	public PrintStream printer;

	/**
	 * The repository for data macros.
	 */
	public Map<String, List<Block>> dataMacros;

	/**
	 * The repository for command macros.
	 */
	public Map<String, List<Block>> commandMacros;

	FDSMode<S>	dataMacroMode;
	FDSMode<S>	comMacroMode;

	/**
	 * Function to change the current data prompt.
	 */
	public Consumer<String> dataPrompter;

	/**
	 * The default data prompt.
	 */
	public String defaultPrompt;

	/**
	 * Create a new interface state.
	 * 
	 * @param stat
	 *                The initial state for the interface.
	 * 
	 * @param inputMode
	 *                The input mode for the interface.
	 * @param cmin
	 *                The source of command blocks.
	 * 
	 * @param datin
	 *                The source of data blocks.
	 * 
	 * @param print
	 *                The destination for output.
	 * @param dataPrompt
	 *                The function to use for changing the data prompt.
	 * 
	 * @param normalPrompt
	 *                The default data prompt.
	 */
	public FDSState(S stat, InputMode inputMode, PushbackBlockReader cmin, PushbackBlockReader datin,
			PrintStream print, Consumer<String> dataPrompt, String normalPrompt) {
		state = stat;
		mode = inputMode;

		comin = cmin;
		datain = datin;
		printer = print;

		dataPrompter = dataPrompt;
		defaultPrompt = normalPrompt;

		modes = new SimpleStack<>();

		dataMacros = new HashMap<>();
		commandMacros = new HashMap<>();

		dataMacroMode = new MacroFDSMode<>(dataMacros, datain::addBlock);
		comMacroMode = new MacroFDSMode<>(commandMacros, comin::addBlock);
	}
}