Push and pop modes
[kaka/cakelight.git] / src / kaka / cakelight / Console.java
index f32ba98..5dfe091 100644 (file)
@@ -1,5 +1,7 @@
 package kaka.cakelight;
 
+import kaka.cakelight.mode.Mode;
+
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
@@ -9,6 +11,7 @@ import java.util.List;
 import java.util.Map;
 
 public class Console extends Thread {
+    private boolean running;
     private CakeLight cakelight;
     private Configuration config;
     private Map<String, Command> commands = new HashMap<>();
@@ -23,8 +26,10 @@ public class Console extends Thread {
     private Console(CakeLight cakelight, Configuration config) {
         this.cakelight = cakelight;
        this.config = config;
-       register(new HelpCommand());
+       register(Commands.help());
        register(Commands.quit());
+       register(Commands.push());
+       register(Commands.pop());
        register(Commands.video());
        register(Commands.color());
        register(Commands.brightness());
@@ -32,6 +37,7 @@ public class Console extends Thread {
        register(Commands.saturation());
        register(Commands.ambientMode());
        register(Commands.twoColorNoiseMode());
+       register(Commands.sunriseMode());
     }
 
     public CakeLight getCakelight() {
@@ -42,18 +48,13 @@ public class Console extends Thread {
        return config;
     }
 
-    private class HelpCommand implements Command {
-       @Override
-       public String[] getNames() {
-           return new String[] {"?", "h", "help"};
-       }
+    List<Command> getCommands() {
+        return commandList;
+    }
 
-       @Override
-       public void activate(Console console, String[] args) {
-           for (Command c : commandList) {
-               System.out.println(String.join("|", c.getNames()));
-           }
-       }
+    void quit() {
+        cakelight.turnOff();
+        running = false;
     }
 
     private void register(Command cmd) {
@@ -69,19 +70,26 @@ public class Console extends Thread {
 
     @Override
     public void run() {
-        while (true) {
-           System.out.print("> ");
-           try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
+       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");
-               break;
+               internalHandleInput(input);
            }
-        }
+       } catch (IOException e) {
+           System.out.println("Error reading from command line");
+       }
+    }
+
+    private void internalHandleInput(String input) {
+       Object obj = handleInput(input);
+       if (obj instanceof Mode) {
+           cakelight.setMode((Mode) obj);
+       }
     }
 
-    void handleInput(String input) {
+    Object handleInput(String input) {
        String[] splitInput = input.split("\\s+", 2);
        String name = splitInput[0];
        String[] args = splitInput.length == 2
@@ -90,10 +98,11 @@ public class Console extends Thread {
 
        Command cmd = commands.get(name);
        if (cmd != null) {
-           cmd.activate(this, args);
+           return cmd.activate(this, args);
        } else {
            out("no command named '" + name + "'");
        }
+       return null;
     }
 
     void out(String text) {
@@ -129,6 +138,6 @@ public class Console extends Thread {
 
     public interface Command {
         String[] getNames();
-        void activate(Console console, String[] args);
+        Object activate(Console console, String[] args);
     }
 }