Commit | Line | Data |
---|---|---|
cd28f68c TW |
1 | package kaka.cakelight; |
2 | ||
3 | import java.io.BufferedReader; | |
4 | import java.io.IOException; | |
5 | import java.io.InputStreamReader; | |
2b49e4e2 TW |
6 | import java.util.ArrayList; |
7 | import java.util.HashMap; | |
8 | import java.util.List; | |
9 | import java.util.Map; | |
cd28f68c TW |
10 | |
11 | public class Console extends Thread { | |
e4738966 | 12 | private boolean running; |
cd28f68c TW |
13 | private CakeLight cakelight; |
14 | private Configuration config; | |
35990bbd TW |
15 | private Map<String, Command> commands = new HashMap<>(); |
16 | private List<Command> commandList = new ArrayList<>(); | |
cd28f68c | 17 | |
f1a6a6a5 TW |
18 | public static Console start(CakeLight cakelight, Configuration config) { |
19 | Console console = new Console(cakelight, config); | |
20 | console.start(); | |
21 | return console; | |
cd28f68c TW |
22 | } |
23 | ||
24 | private Console(CakeLight cakelight, Configuration config) { | |
25 | this.cakelight = cakelight; | |
26 | this.config = config; | |
e4738966 | 27 | register(Commands.help()); |
2b49e4e2 TW |
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()); | |
40a06a9b | 36 | register(Commands.sunriseMode()); |
be3f2496 | 37 | } |
fa013e4b TW |
38 | |
39 | public CakeLight getCakelight() { | |
40 | return cakelight; | |
41 | } | |
42 | ||
43 | public Configuration getConfig() { | |
44 | return config; | |
45 | } | |
35990bbd | 46 | |
e4738966 TW |
47 | List<Command> getCommands() { |
48 | return commandList; | |
49 | } | |
be3f2496 | 50 | |
e4738966 TW |
51 | void quit() { |
52 | cakelight.turnOff(); | |
53 | running = false; | |
be3f2496 TW |
54 | } |
55 | ||
35990bbd TW |
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); | |
cd28f68c TW |
65 | } |
66 | ||
67 | @Override | |
68 | public void run() { | |
e4738966 | 69 | running = true; |
9f2bc172 | 70 | try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) { |
e4738966 | 71 | while (running) { |
9f2bc172 | 72 | System.out.print("> "); |
f1a6a6a5 TW |
73 | String input = reader.readLine(); |
74 | handleInput(input); | |
f1a6a6a5 | 75 | } |
9f2bc172 TW |
76 | } catch (IOException e) { |
77 | System.out.println("Error reading from command line"); | |
78 | } | |
cd28f68c | 79 | } |
eca6fd31 | 80 | |
f1a6a6a5 TW |
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 | ||
2b49e4e2 TW |
96 | void out(String text) { |
97 | System.out.println("(" + text + ")"); | |
98 | } | |
baaaa10b | 99 | |
2b49e4e2 TW |
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 | } | |
baaaa10b | 122 | } |
2b49e4e2 TW |
123 | System.out.println("Failed to parse color '" + s + "'. Using black instead."); |
124 | return Color.BLACK; | |
125 | } | |
35990bbd | 126 | |
2b49e4e2 TW |
127 | public interface Command { |
128 | String[] getNames(); | |
129 | void activate(Console console, String[] args); | |
eca6fd31 | 130 | } |
cd28f68c | 131 | } |