Help command for listing all commands
[kaka/cakelight.git] / src / kaka / cakelight / Console.java
1 package kaka.cakelight;
2
3 import kaka.cakelight.mode.AmbientMode;
4 import kaka.cakelight.mode.SingleColorMode;
5 import kaka.cakelight.mode.TwoColorNoiseMode;
6 import kaka.cakelight.mode.VideoMode;
7
8 import java.io.BufferedReader;
9 import java.io.IOException;
10 import java.io.InputStreamReader;
11 import java.util.*;
12
13 public class Console extends Thread {
14     private CakeLight cakelight;
15     private Configuration config;
16     private BufferedReader reader;
17     private Map<String, Command> commands = new HashMap<>();
18     private List<Command> commandList = new ArrayList<>();
19
20     public static void start(CakeLight cakelight, Configuration config) {
21         new Console(cakelight, config).start();
22     }
23
24     private Console(CakeLight cakelight, Configuration config) {
25         this.cakelight = cakelight;
26         this.config = config;
27         reader = new BufferedReader(new InputStreamReader(System.in));
28         register(new HelpCommand());
29         register(TwoColorNoiseMode.getCommand());
30     }
31
32     public CakeLight getCakelight() {
33         return cakelight;
34     }
35
36     public Configuration getConfig() {
37         return config;
38     }
39
40     private class HelpCommand implements Command {
41         @Override
42         public String[] getNames() {
43             return new String[] {"?", "h", "help"};
44         }
45
46         @Override
47         public void activate(Console console, String[] args) {
48             for (Command c : commandList) {
49                 System.out.println(String.join("|", c.getNames()));
50             }
51         }
52     }
53
54     private void register(Command cmd) {
55         for (String name : cmd.getNames()){
56             if (commands.containsKey(name)) {
57                 System.out.println("Command name '" + name + "' is already registered by '" + commands.get(name).getClass() + "'!");
58                 System.exit(1);
59             }
60             commands.put(name, cmd);
61         }
62         commandList.add(cmd);
63     }
64
65     @Override
66     public void run() {
67         while (true) {
68             System.out.print("> ");
69             try {
70                 String input = reader.readLine();
71                 String[] splitInput = input.split("\\s+", 2);
72                 String name = splitInput[0];
73                 String[] args = splitInput.length == 2
74                         ? splitInput[1].split("\\s+")
75                         : new String[]{};
76
77                 Command cmd = commands.get(name);
78                 if (cmd != null) {
79                     cmd.activate(this, args);
80                     continue;
81                 }
82
83                 if (input.matches("[0-5]")) {
84                     cakelight.setMode(new AmbientMode(new String[] {input}));
85                     System.out.println("setting ambient mode to " + input);
86                 } else if (input.matches("v|video")) {
87                     cakelight.setMode(new VideoMode());
88                 } else if (input.matches("(b|brightness)\\s+[0-9]+")) {
89                     String[] split = input.split("\\s+");
90                     config.leds.brightness = Integer.parseInt(split[1]);
91                     System.out.println("setting brightness to " + config.leds.brightness);
92                 } else if (input.matches("q|quit")) {
93                     cakelight.turnOff();
94                     System.out.println("stopping cakelight");
95                     break;
96                 } else if (input.matches("(c|col|color)(\\s+[0-9]+){3}")) {
97                     String[] split = input.split("\\s+");
98                     Color c = Color.rgb(
99                             Integer.parseInt(split[1]),
100                             Integer.parseInt(split[2]),
101                             Integer.parseInt(split[3])
102                     );
103                     cakelight.setMode(new SingleColorMode(c));
104                     System.out.println("setting color to " + c);
105                 } else if (input.matches("(g|gamma)\\s+[0-9.]+")) {
106                     String[] split = input.split("\\s+");
107                     config.gamma = Double.parseDouble(split[1]);
108                     Color.calculateGammaCorrection(config.gamma);
109                     System.out.println("setting gamma to " + config.gamma);
110                 } else if (input.matches("(s|saturation)\\s+[0-9.]+")) {
111                     String[] split = input.split("\\s+");
112                     config.video.saturation = Double.parseDouble(split[1]);
113                     System.out.println("setting saturation to " + config.video.saturation);
114                 } else if (input.matches("(n|noise)(\\s+[a-z0-9]+){2}")) {
115                     TwoColorNoiseMode.getCommand().activate(this, input.split("\\s+"));
116                     System.out.println("setting two-color noise mode");
117                 }
118             } catch (IOException e) {
119                 System.out.println("Error reading from command line");
120                 break;
121             }
122         }
123     }
124
125     public interface Command {
126         String[] getNames();
127         void activate(Console console, String[] args);
128
129         default Color parseColor(String s) {
130             switch (s.toLowerCase()) {
131                 case "r": return Color.rgb(255, 0, 0);
132                 case "g": return Color.rgb(0, 255, 0);
133                 case "b": return Color.rgb(0, 0, 255);
134                 default: // assume hexadecimal
135                     if (s.startsWith("#")) {
136                         s = s.substring(1);
137                     }
138                     if (s.length() == 3) {
139                         return Color.rgb(
140                                 Integer.parseInt(s.substring(0, 1), 16) * 16 + Integer.parseInt(s.substring(0, 1), 16),
141                                 Integer.parseInt(s.substring(1, 2), 16) * 16 + Integer.parseInt(s.substring(1, 2), 16),
142                                 Integer.parseInt(s.substring(2, 3), 16) * 16 + Integer.parseInt(s.substring(2, 3), 16)
143                         );
144                     } else if (s.length() == 6) {
145                         return Color.rgb(
146                                 Integer.parseInt(s.substring(0, 2), 16),
147                                 Integer.parseInt(s.substring(2, 4), 16),
148                                 Integer.parseInt(s.substring(4, 6), 16)
149                         );
150                     }
151             }
152             System.out.println("Failed to parse color '" + s + "'. Using black instead.");
153             return Color.BLACK;
154         }
155
156         default void output(String text) {
157             System.out.println("(" + text + ")");
158         }
159     }
160 }