Push and pop modes
[kaka/cakelight.git] / src / kaka / cakelight / Console.java
index cf72cce..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,22 +11,25 @@ import java.util.List;
 import java.util.Map;
 
 public class Console extends Thread {
+    private boolean running;
     private CakeLight cakelight;
     private Configuration config;
-    private BufferedReader reader;
     private Map<String, Command> commands = new HashMap<>();
     private List<Command> commandList = new ArrayList<>();
 
-    public static void start(CakeLight cakelight, Configuration config) {
-        new Console(cakelight, config).start();
+    public static Console start(CakeLight cakelight, Configuration config) {
+       Console console = new Console(cakelight, config);
+       console.start();
+       return console;
     }
 
     private Console(CakeLight cakelight, Configuration config) {
         this.cakelight = cakelight;
        this.config = config;
-       reader = new BufferedReader(new InputStreamReader(System.in));
-       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,27 +70,39 @@ public class Console extends Thread {
 
     @Override
     public void run() {
-        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();
+               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);
+       }
+    }
+
+    Object handleInput(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;
     }
 
     void out(String text) {
@@ -125,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);
     }
 }