X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fkaka%2Fcakelight%2FCommands.java;h=8f617134a4d38b36b46ac40c8d7d9dfe51f8c2c6;hb=e147b561b263ad878201f68ba0f76b9a1d76c59c;hp=38fb77e8ac522f7818cae90fb4adccaf5648f3a3;hpb=40a06a9bffb56d41e931038fcfdb8f10fe8ca441;p=kaka%2Fcakelight.git diff --git a/src/kaka/cakelight/Commands.java b/src/kaka/cakelight/Commands.java index 38fb77e..8f61713 100644 --- a/src/kaka/cakelight/Commands.java +++ b/src/kaka/cakelight/Commands.java @@ -3,6 +3,7 @@ package kaka.cakelight; import kaka.cakelight.mode.*; import java.util.function.BiFunction; +import java.util.stream.Stream; class Commands { private static Console.Command command(String[] names, BiFunction activate) { @@ -13,31 +14,74 @@ class Commands { } @Override - public void activate(Console console, String[] args) { + public Object activate(Console console, String[] args) { if (!activate.apply(console, args)) { console.out("did NOT run command"); } + return null; } }; } + private static Console.Command modeCommand(String[] names, BiFunction activate) { + return new Console.Command() { + @Override + public String[] getNames() { + return names; + } + + @Override + public Object activate(Console console, String[] args) { + Mode mode = activate.apply(console, args); + if (mode == null) { + console.out("did NOT run command"); + } + return mode; + } + }; + } + + static Console.Command help() { + return command(new String[] {"?", "h", "help"}, (console, args) -> { + for (Console.Command c : console.getCommands()) { + System.out.println(String.join("|", c.getNames())); + } + return true; + }); + } + static Console.Command quit() { return command(new String[] {"q", "quit"}, (console, args) -> { - console.getCakelight().turnOff(); - console.out("stopping cakelight"); + console.quit(); + console.out("terminating"); return true; }); } - static Console.Command video() { - return command(new String[] {"v", "video"}, (console, args) -> { - console.getCakelight().setMode(new VideoMode()); - return true; + static Console.Command push() { + return command(new String[] {"push"}, (console, args) -> { + Object obj = console.internalHandleInput(String.join(" ", args)); + if (obj instanceof Mode) { // obj could be anything, which should be fixed + console.out("pushing mode " + obj.getClass().getSimpleName()); + console.getCakelight().pushMode((Mode) obj); + } + return true; }); } + static Console.Command pop() { + return command(new String[] {"pop"}, (console, args) -> { + console.out("popping mode " + console.getCakelight().popMode().getClass().getSimpleName()); + return true; + }); + } + + static Console.Command video() { + return modeCommand(new String[] {"v", "video"}, (console, args) -> new VideoMode()); + } + static Console.Command color() { - return command(new String[] {"c", "col", "color"}, (console, args) -> { + return modeCommand(new String[] {"c", "col", "color"}, (console, args) -> { Color c = null; if (args.length == 1) { c = console.parseColor(args[0]); @@ -49,12 +93,10 @@ class Commands { ); } if (c != null) { - console.getCakelight().setMode(new SingleColorMode(c)); console.out("setting color to " + c); - return true; - } else { - return false; + return new SingleColorMode(c); } + return null; }); } @@ -104,42 +146,49 @@ class Commands { } static Console.Command ambientMode() { - return command(new String[] {"m", "mode"}, (console, args) -> { + return modeCommand(new String[] {"m", "mode"}, (console, args) -> { if (args.length == 1) { - console.getCakelight().setMode(new AmbientMode(new String[] {args[0]})); console.out("setting ambient mode to " + args[0]); - return true; - } else { - return false; + return new AmbientMode(new String[]{args[0]}); } + return null; }); } - static Console.Command twoColorNoiseMode() { - return command(new String[] {"n", "noise"}, (console, args) -> { - if (args.length == 2) { - console.getCakelight().setMode(new TwoColorNoiseMode( - console.parseColor(args[0]), - console.parseColor(args[1]) - )); - console.out("setting two-color noise mode"); - return true; - } else { - return false; + static Console.Command noiseMode() { + return modeCommand(new String[] {"n", "noise"}, (console, args) -> { + if (args.length > 1) { + console.out("setting multi-color noise mode"); + return new NoiseMode(Stream.of(args) + .map(console::parseColor) + .toArray(Color[]::new) + ); + } + return null; + }); + } + + static Console.Command fireMode() { + return modeCommand(new String[] {"f", "fire"}, (console, args) -> { + if (args.length > 1) { + console.out("setting multi-color fire mode"); + return new FireMode(Stream.of(args) + .map(console::parseColor) + .toArray(Color[]::new) + ); } + return null; }); } static Console.Command sunriseMode() { - return command(new String[] {"sunrise"}, (console, args) -> { + return modeCommand(new String[] {"sunrise"}, (console, args) -> { if (args.length == 1) { int durationSeconds = Integer.parseInt(args[0]); - console.getCakelight().setMode(new SunriseMode(durationSeconds)); console.out("setting sunrise mode with duration " + durationSeconds); - return true; - } else { - return false; + return new SunriseMode(durationSeconds); } + return null; }); } }