From: Tomas Wenström Date: Thu, 28 Nov 2019 21:31:26 +0000 (+0100) Subject: Fix quit command and move help command X-Git-Url: http://dolda2000.com/gitweb/?p=kaka%2Fcakelight.git;a=commitdiff_plain;h=e4738966c601bac22b7f7cbad375bd172913bde9 Fix quit command and move help command --- diff --git a/src/kaka/cakelight/Commands.java b/src/kaka/cakelight/Commands.java index 1a6a850..a5b612c 100644 --- a/src/kaka/cakelight/Commands.java +++ b/src/kaka/cakelight/Commands.java @@ -25,10 +25,19 @@ class Commands { }; } + 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; }); } diff --git a/src/kaka/cakelight/Console.java b/src/kaka/cakelight/Console.java index 873dac1..3f9bce9 100644 --- a/src/kaka/cakelight/Console.java +++ b/src/kaka/cakelight/Console.java @@ -9,6 +9,7 @@ import java.util.List; import java.util.Map; public class Console extends Thread { + private boolean running; private CakeLight cakelight; private Configuration config; private Map commands = new HashMap<>(); @@ -23,7 +24,7 @@ public class Console extends Thread { private Console(CakeLight cakelight, Configuration config) { this.cakelight = cakelight; this.config = config; - register(new HelpCommand()); + register(Commands.help()); register(Commands.quit()); register(Commands.video()); register(Commands.color()); @@ -43,18 +44,13 @@ public class Console extends Thread { return config; } - private class HelpCommand implements Command { - @Override - public String[] getNames() { - return new String[] {"?", "h", "help"}; - } + List getCommands() { + return commandList; + } - @Override - public void activate(Console console, String[] args) { - for (Command c : commandList) { - System.out.println(String.join("|", c.getNames())); - } - } + void quit() { + cakelight.turnOff(); + running = false; } private void register(Command cmd) { @@ -70,8 +66,9 @@ public class Console extends Thread { @Override public void run() { + running = true; try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) { - while (true) { + while (running) { System.out.print("> "); String input = reader.readLine(); handleInput(input);