summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/cli/fds/FDS.java
blob: 6dc0337654003cfa0a347992ea75dd222b22e6a9 (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
package bjc.utils.cli.fds;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import bjc.utils.ioutils.Block;
import bjc.utils.ioutils.BlockReader;

/**
 * Runs a FDS (FDiskScript) interface.
 * 
 * This is a rudimentary console interface inspired heavily by FDisk's interface
 * style.
 * 
 * Commands are denoted by a single character, but can invoke submodes.
 * 
 * @author bjculkin
 *
 */
public class FDS {
	/**
	 * Run a provided FDS mode until it is exited or there is no more input.
	 * 
	 * @param comin
	 *                The command input source for the FDS mode.
	 * 
	 * @param datain
	 *                The data input source for the FDS mode.
	 * 
	 * @param out
	 *                The output source for the FDS mode.
	 * 
	 * @param initialMode
	 *                The mode to start in.
	 * 
	 * @param initialState
	 *                The initial state for the mode.
	 * 
	 * @return The final state of the mode.
	 * 
	 * @throws FDSException
	 *                 If something went wrong during mode execution.
	 */
	public static <S> S runFDS(InputStream comin, InputStream datain, OutputStream out, FDSMode<S> initialMode,
			S initialState) throws FDSException {
		PrintStream printer = new PrintStream(out);

		try (BlockReader blockSource = new BlockReader("\\R", new InputStreamReader(comin))) {
			printer.print("Enter a command (m for help): ");

			while (blockSource.hasNext()) {
				Block comBlock = blockSource.next();

				String comString = comBlock.contents.trim();
				
				char comChar = comString.charAt(0);
				
				printer.println(String.format("\nRecieved command '%s'\n", comChar));

				printer.print("Enter a command (m for help): ");
			}
		} catch (Exception ex) {
			throw new FDSException("Unexpected I/O error", ex);
		}

		return initialState;
	}
}