X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fkaka%2Fcakelight%2FConsole.java;h=5dfe091e8935ec6a6f95238acc3c03564374afa4;hb=c9edf58db4c00b7b95bb7f521063e5ecd79db262;hp=cf72cceca85c97b3c9c97a96f449e3be3c5540e1;hpb=2b49e4e255f8591fb730f88e71b6b006008ccd3d;p=kaka%2Fcakelight.git diff --git a/src/kaka/cakelight/Console.java b/src/kaka/cakelight/Console.java index cf72cce..5dfe091 100644 --- a/src/kaka/cakelight/Console.java +++ b/src/kaka/cakelight/Console.java @@ -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 commands = new HashMap<>(); private List 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 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); } }