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 CakeLight cakelight;
13 private Configuration config;
14 private Map<String, Command> commands = new HashMap<>();
15 private List<Command> commandList = new ArrayList<>();
17 public static Console start(CakeLight cakelight, Configuration config) {
18 Console console = new Console(cakelight, config);
23 private Console(CakeLight cakelight, Configuration config) {
24 this.cakelight = cakelight;
26 register(new HelpCommand());
27 register(Commands.quit());
28 register(Commands.video());
29 register(Commands.color());
30 register(Commands.brightness());
31 register(Commands.gamma());
32 register(Commands.saturation());
33 register(Commands.ambientMode());
34 register(Commands.twoColorNoiseMode());
35 register(Commands.sunriseMode());
38 public CakeLight getCakelight() {
42 public Configuration getConfig() {
46 private class HelpCommand implements Command {
48 public String[] getNames() {
49 return new String[] {"?", "h", "help"};
53 public void activate(Console console, String[] args) {
54 for (Command c : commandList) {
55 System.out.println(String.join("|", c.getNames()));
60 private void register(Command cmd) {
61 for (String name : cmd.getNames()){
62 if (commands.containsKey(name)) {
63 System.out.println("Command name '" + name + "' is already registered by '" + commands.get(name).getClass() + "'!");
66 commands.put(name, cmd);
73 try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
75 System.out.print("> ");
76 String input = reader.readLine();
79 } catch (IOException e) {
80 System.out.println("Error reading from command line");
84 void handleInput(String input) {
85 String[] splitInput = input.split("\\s+", 2);
86 String name = splitInput[0];
87 String[] args = splitInput.length == 2
88 ? splitInput[1].split("\\s+")
91 Command cmd = commands.get(name);
93 cmd.activate(this, args);
95 out("no command named '" + name + "'");
99 void out(String text) {
100 System.out.println("(" + text + ")");
103 Color parseColor(String s) {
104 switch (s.toLowerCase()) {
105 case "r": return Color.rgb(255, 0, 0);
106 case "g": return Color.rgb(0, 255, 0);
107 case "b": return Color.rgb(0, 0, 255);
108 default: // assume hexadecimal
109 if (s.startsWith("#")) {
112 if (s.length() == 3) {
114 Integer.parseInt(s.substring(0, 1), 16) * 16 + Integer.parseInt(s.substring(0, 1), 16),
115 Integer.parseInt(s.substring(1, 2), 16) * 16 + Integer.parseInt(s.substring(1, 2), 16),
116 Integer.parseInt(s.substring(2, 3), 16) * 16 + Integer.parseInt(s.substring(2, 3), 16)
118 } else if (s.length() == 6) {
120 Integer.parseInt(s.substring(0, 2), 16),
121 Integer.parseInt(s.substring(2, 4), 16),
122 Integer.parseInt(s.substring(4, 6), 16)
126 System.out.println("Failed to parse color '" + s + "'. Using black instead.");
130 public interface Command {
132 void activate(Console console, String[] args);