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

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 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.
	 * 
	 * @return The final state of the mode.
	 * 
	 * @throws FDSException
	 *                 If something went wrong during mode execution.
	 */
	public static <S> S runFDS(BlockReader blockSource, BlockReader datain, PrintStream printer, FDSMode<S> mode,
			FDSState<S> state) throws FDSException {
		//printer.print("Enter a command (m for help): ");

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

			handleCommandString(comBlock, blockSource, datain, printer, mode, state);

			//printer.print("Enter a command (m for help): ");
		}

		return state.state;
	}

	private static <S> void handleCommandString(Block comBlock, BlockReader blockSource, BlockReader datain,
			PrintStream printer, FDSMode<S> mode, FDSState<S> state) throws FDSException {
		String comString = comBlock.contents.trim();

		switch (state.mode) {
		case CHORD:
			if (comString.length() > 1) {
				for (char c : comString.substring(1).toCharArray()) {
					Block newCom = new Block(comBlock.blockNo + 1, Character.toString(c),
							comBlock.startLine, comBlock.startLine);

					state.enqueCommand.accept(newCom);
				}
			}
		case NORMAL:
			handleCommand(comString.charAt(0), blockSource, datain, printer, mode, state);
			break;

		default:
			throw new FDSException(String.format("Unknown input mode '%s'", state.mode));
		}
	}

	private static <S> void handleCommand(char charAt, BlockReader blockSource, BlockReader datain,
			PrintStream printer, FDSMode<S> mode, FDSState<S> state) {
		printer.printf("Recieved command '%s'\n", charAt);
	}
}