1 package kaka.cakelight;
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStreamReader;
6 import java.util.ArrayList;
7 import java.util.HashMap;
11 public class Console extends Thread {
12 private boolean running;
13 private CakeLight cakelight;
14 private Configuration config;
15 private Map<String, Command> commands = new HashMap<>();
16 private List<Command> commandList = new ArrayList<>();
18 public static Console start(CakeLight cakelight, Configuration config) {
19 Console console = new Console(cakelight, config);
24 private Console(CakeLight cakelight, Configuration config) {
25 this.cakelight = cakelight;
27 register(Commands.help());
28 register(Commands.quit());
29 register(Commands.video());
30 register(Commands.color());
31 register(Commands.brightness());
32 register(Commands.gamma());
33 register(Commands.saturation());
34 register(Commands.ambientMode());
35 register(Commands.twoColorNoiseMode());
36 register(Commands.sunriseMode());
39 public CakeLight getCakelight() {
43 public Configuration getConfig() {
47 List<Command> getCommands() {
56 private void register(Command cmd) {
57 for (String name : cmd.getNames()){
58 if (commands.containsKey(name)) {
59 System.out.println("Command name '" + name + "' is already registered by '" + commands.get(name).getClass() + "'!");
62 commands.put(name, cmd);
70 try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
72 System.out.print("> ");
73 String input = reader.readLine();
76 } catch (IOException e) {
77 System.out.println("Error reading from command line");
81 void handleInput(String input) {
82 String[] splitInput = input.split("\\s+", 2);
83 String name = splitInput[0];
84 String[] args = splitInput.length == 2
85 ? splitInput[1].split("\\s+")
88 Command cmd = commands.get(name);
90 cmd.activate(this, args);
92 out("no command named '" + name + "'");
96 void out(String text) {
97 System.out.println("(" + text + ")");
100 Color parseColor(String s) {
101 switch (s.toLowerCase()) {
102 case "r": return Color.rgb(255, 0, 0);
103 case "g": return Color.rgb(0, 255, 0);
104 case "b": return Color.rgb(0, 0, 255);
105 default: // assume hexadecimal
106 if (s.startsWith("#")) {
109 if (s.length() == 3) {
111 Integer.parseInt(s.substring(0, 1), 16) * 16 + Integer.parseInt(s.substring(0, 1), 16),
112 Integer.parseInt(s.substring(1, 2), 16) * 16 + Integer.parseInt(s.substring(1, 2), 16),
113 Integer.parseInt(s.substring(2, 3), 16) * 16 + Integer.parseInt(s.substring(2, 3), 16)
115 } else if (s.length() == 6) {
117 Integer.parseInt(s.substring(0, 2), 16),
118 Integer.parseInt(s.substring(2, 4), 16),
119 Integer.parseInt(s.substring(4, 6), 16)
123 System.out.println("Failed to parse color '" + s + "'. Using black instead.");
127 public interface Command {
129 void activate(Console console, String[] args);