Add sunrise mode
[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 CakeLight cakelight;
13     private Configuration config;
14     private Map<String, Command> commands = new HashMap<>();
15     private List<Command> commandList = new ArrayList<>();
16
17     public static Console start(CakeLight cakelight, Configuration config) {
18         Console console = new Console(cakelight, config);
19         console.start();
20         return console;
21     }
22
23     private Console(CakeLight cakelight, Configuration config) {
24         this.cakelight = cakelight;
25         this.config = config;
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());
36     }
37
38     public CakeLight getCakelight() {
39         return cakelight;
40     }
41
42     public Configuration getConfig() {
43         return config;
44     }
45
46     private class HelpCommand implements Command {
47         @Override
48         public String[] getNames() {
49             return new String[] {"?", "h", "help"};
50         }
51
52         @Override
53         public void activate(Console console, String[] args) {
54             for (Command c : commandList) {
55                 System.out.println(String.join("|", c.getNames()));
56             }
57         }
58     }
59
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() + "'!");
64                 System.exit(1);
65             }
66             commands.put(name, cmd);
67         }
68         commandList.add(cmd);
69     }
70
71     @Override
72     public void run() {
73         while (true) {
74             System.out.print("> ");
75             try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
76                 String input = reader.readLine();
77                 handleInput(input);
78             } catch (IOException e) {
79                 System.out.println("Error reading from command line");
80                 break;
81             }
82         }
83     }
84
85     void handleInput(String input) {
86         String[] splitInput = input.split("\\s+", 2);
87         String name = splitInput[0];
88         String[] args = splitInput.length == 2
89                 ? splitInput[1].split("\\s+")
90                 : new String[]{};
91
92         Command cmd = commands.get(name);
93         if (cmd != null) {
94             cmd.activate(this, args);
95         } else {
96             out("no command named '" + name + "'");
97         }
98     }
99
100     void out(String text) {
101         System.out.println("(" + text + ")");
102     }
103
104     Color parseColor(String s) {
105         switch (s.toLowerCase()) {
106             case "r": return Color.rgb(255, 0, 0);
107             case "g": return Color.rgb(0, 255, 0);
108             case "b": return Color.rgb(0, 0, 255);
109             default: // assume hexadecimal
110                 if (s.startsWith("#")) {
111                     s = s.substring(1);
112                 }
113                 if (s.length() == 3) {
114                     return Color.rgb(
115                             Integer.parseInt(s.substring(0, 1), 16) * 16 + Integer.parseInt(s.substring(0, 1), 16),
116                             Integer.parseInt(s.substring(1, 2), 16) * 16 + Integer.parseInt(s.substring(1, 2), 16),
117                             Integer.parseInt(s.substring(2, 3), 16) * 16 + Integer.parseInt(s.substring(2, 3), 16)
118                     );
119                 } else if (s.length() == 6) {
120                     return Color.rgb(
121                             Integer.parseInt(s.substring(0, 2), 16),
122                             Integer.parseInt(s.substring(2, 4), 16),
123                             Integer.parseInt(s.substring(4, 6), 16)
124                     );
125                 }
126         }
127         System.out.println("Failed to parse color '" + s + "'. Using black instead.");
128         return Color.BLACK;
129     }
130
131     public interface Command {
132         String[] getNames();
133         void activate(Console console, String[] args);
134     }
135 }