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