Fix quit command and move help command
[kaka/cakelight.git] / src / kaka / cakelight / Commands.java
1 package kaka.cakelight;
2
3 import kaka.cakelight.mode.AmbientMode;
4 import kaka.cakelight.mode.SingleColorMode;
5 import kaka.cakelight.mode.SunriseMode;
6 import kaka.cakelight.mode.TwoColorNoiseMode;
7 import kaka.cakelight.mode.VideoMode;
8
9 import java.util.function.BiFunction;
10
11 class Commands {
12     private static Console.Command command(String[] names, BiFunction<Console, String[], Boolean> activate) {
13         return new Console.Command() {
14             @Override
15             public String[] getNames() {
16                 return names;
17             }
18
19             @Override
20             public void activate(Console console, String[] args) {
21                 if (!activate.apply(console, args)) {
22                     console.out("did NOT run command");
23                 }
24             }
25         };
26     }
27
28     static Console.Command help() {
29         return command(new String[] {"?", "h", "help"}, (console, args) -> {
30             for (Console.Command c : console.getCommands()) {
31                 System.out.println(String.join("|", c.getNames()));
32             }
33             return true;
34         });
35     }
36
37     static Console.Command quit() {
38         return command(new String[] {"q", "quit"}, (console, args) -> {
39             console.quit();
40             console.out("terminating");
41             return true;
42         });
43     }
44
45     static Console.Command video() {
46         return command(new String[] {"v", "video"}, (console, args) -> {
47             console.getCakelight().setMode(new VideoMode());
48             return true;
49         });
50     }
51
52     static Console.Command color() {
53         return command(new String[] {"c", "col", "color"}, (console, args) -> {
54             Color c = null;
55             if (args.length == 1) {
56                 c = console.parseColor(args[0]);
57             } else if (args.length == 3) {
58                 c = Color.rgb(
59                         Integer.parseInt(args[0]),
60                         Integer.parseInt(args[1]),
61                         Integer.parseInt(args[2])
62                 );
63             }
64             if (c != null) {
65                 console.getCakelight().setMode(new SingleColorMode(c));
66                 console.out("setting color to " + c);
67                 return true;
68             } else {
69                 return false;
70             }
71         });
72     }
73
74     static Console.Command brightness() {
75         return command(new String[] {"b", "brightness"}, (console, args) -> {
76             if (args.length == 1) {
77                 int b = Integer.parseInt(args[0].replaceAll("[+-]+", ""));
78                 if (args[0].startsWith("+")) {
79                     b = Math.min(console.getConfig().leds.brightness + b, 31);
80                 } else if (args[0].startsWith("-")) {
81                     b = Math.max(console.getConfig().leds.brightness - b, 0);
82                 }
83                 console.getConfig().leds.brightness = b;
84                 console.out("setting brightness to " + b);
85                 return true;
86             } else {
87                 return false;
88             }
89         });
90     }
91
92     static Console.Command gamma() {
93         return command(new String[] {"g", "gamma"}, (console, args) -> {
94             if (args.length == 1) {
95                 double g = Double.parseDouble(args[0]);
96                 console.getConfig().gamma = g;
97                 Color.calculateGammaCorrection(g);
98                 console.out("setting gamma to " + g);
99                 return true;
100             } else {
101                 return false;
102             }
103         });
104     }
105
106     static Console.Command saturation() {
107         return command(new String[] {"s", "saturation"}, (console, args) -> {
108             if (args.length == 1) {
109                 double s = Double.parseDouble(args[0]);
110                 console.getConfig().video.saturation = s;
111                 console.out("setting saturation to " + s);
112                 return true;
113             } else {
114                 return false;
115             }
116         });
117     }
118
119     static Console.Command ambientMode() {
120         return command(new String[] {"m", "mode"}, (console, args) -> {
121             if (args.length == 1) {
122                 console.getCakelight().setMode(new AmbientMode(new String[] {args[0]}));
123                 console.out("setting ambient mode to " + args[0]);
124                 return true;
125             } else {
126                 return false;
127             }
128         });
129     }
130
131     static Console.Command twoColorNoiseMode() {
132         return command(new String[] {"n", "noise"}, (console, args) -> {
133             if (args.length == 2) {
134                 console.getCakelight().setMode(new TwoColorNoiseMode(
135                         console.parseColor(args[0]),
136                         console.parseColor(args[1])
137                 ));
138                 console.out("setting two-color noise mode");
139                 return true;
140             } else {
141                 return false;
142             }
143         });
144     }
145
146     static Console.Command sunriseMode() {
147         return command(new String[] {"sunrise"}, (console, args) -> {
148             if (args.length == 1) {
149                 int durationSeconds = Integer.parseInt(args[0]);
150                 console.getCakelight().setMode(new SunriseMode(durationSeconds));
151                 console.out("setting sunrise mode with duration " + durationSeconds);
152                 return true;
153             } else {
154                 return false;
155             }
156         });
157     }
158 }