Fix quit command and move help command
[kaka/cakelight.git] / src / kaka / cakelight / Console.java
1 package kaka.cakelight;
2
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;
8 import java.util.List;
9 import java.util.Map;
10
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<>();
17
18     public static Console start(CakeLight cakelight, Configuration config) {
19         Console console = new Console(cakelight, config);
20         console.start();
21         return console;
22     }
23
24     private Console(CakeLight cakelight, Configuration config) {
25         this.cakelight = cakelight;
26         this.config = config;
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());
37     }
38
39     public CakeLight getCakelight() {
40         return cakelight;
41     }
42
43     public Configuration getConfig() {
44         return config;
45     }
46
47     List<Command> getCommands() {
48         return commandList;
49     }
50
51     void quit() {
52         cakelight.turnOff();
53         running = false;
54     }
55
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() + "'!");
60                 System.exit(1);
61             }
62             commands.put(name, cmd);
63         }
64         commandList.add(cmd);
65     }
66
67     @Override
68     public void run() {
69         running = true;
70         try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
71             while (running) {
72                 System.out.print("> ");
73                 String input = reader.readLine();
74                 handleInput(input);
75             }
76         } catch (IOException e) {
77             System.out.println("Error reading from command line");
78         }
79     }
80
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+")
86                 : new String[]{};
87
88         Command cmd = commands.get(name);
89         if (cmd != null) {
90             cmd.activate(this, args);
91         } else {
92             out("no command named '" + name + "'");
93         }
94     }
95
96     void out(String text) {
97         System.out.println("(" + text + ")");
98     }
99
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("#")) {
107                     s = s.substring(1);
108                 }
109                 if (s.length() == 3) {
110                     return Color.rgb(
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)
114                     );
115                 } else if (s.length() == 6) {
116                     return Color.rgb(
117                             Integer.parseInt(s.substring(0, 2), 16),
118                             Integer.parseInt(s.substring(2, 4), 16),
119                             Integer.parseInt(s.substring(4, 6), 16)
120                     );
121                 }
122         }
123         System.out.println("Failed to parse color '" + s + "'. Using black instead.");
124         return Color.BLACK;
125     }
126
127     public interface Command {
128         String[] getNames();
129         void activate(Console console, String[] args);
130     }
131 }