- while (true) {
- System.out.print("> ");
- try {
- String input = reader.readLine();
- String[] splitInput = input.split("\\s+", 2);
- String name = splitInput[0];
- String[] args = splitInput.length == 2
- ? splitInput[1].split("\\s+")
- : new String[]{};
-
- Command cmd = commands.get(name);
- if (cmd != null) {
- cmd.activate(this, args);
- } else {
- out("no command named '" + name + "'");
- }
- } catch (IOException e) {
- System.out.println("Error reading from command line");
- break;
- }
- }
+ running = true;
+ try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
+ while (running) {
+ System.out.print("> ");
+ String input = reader.readLine();
+ handleInput(input);
+ }
+ } catch (IOException e) {
+ System.out.println("Error reading from command line");
+ }
+ }
+
+ void handleInput(String input) {
+ Object obj = internalHandleInput(input);
+ if (obj instanceof Mode) {
+ cakelight.setMode((Mode) obj);
+ }
+ }
+
+ Object internalHandleInput(String input) {
+ String[] splitInput = input.split("\\s+", 2);
+ String name = splitInput[0];
+ String[] args = splitInput.length == 2
+ ? splitInput[1].split("\\s+")
+ : new String[]{};
+
+ Command cmd = commands.get(name);
+ if (cmd != null) {
+ return cmd.activate(this, args);
+ } else {
+ out("no command named '" + name + "'");
+ }
+ return null;