};
}
+ 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;
});
}
import java.util.Map;
public class Console extends Thread {
+ private boolean running;
private CakeLight cakelight;
private Configuration config;
private Map<String, Command> commands = new HashMap<>();
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());
return config;
}
- private class HelpCommand implements Command {
- @Override
- public String[] getNames() {
- return new String[] {"?", "h", "help"};
- }
+ List<Command> 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) {
@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);