X-Git-Url: http://dolda2000.com/gitweb/?p=kaka%2Fcakelight.git;a=blobdiff_plain;f=src%2Fkaka%2Fcakelight%2FConsole.java;h=f32ba9824edfe632868c22823c1dfcf9210fddcd;hp=cf72cceca85c97b3c9c97a96f449e3be3c5540e1;hb=f1a6a6a5cf7d61c2df185206cb0a5b0e7eceb3c1;hpb=cc124c322996f68454baf4405b2959c84e771296 diff --git a/src/kaka/cakelight/Console.java b/src/kaka/cakelight/Console.java index cf72cce..f32ba98 100644 --- a/src/kaka/cakelight/Console.java +++ b/src/kaka/cakelight/Console.java @@ -11,18 +11,18 @@ import java.util.Map; public class Console extends Thread { private CakeLight cakelight; private Configuration config; - private BufferedReader reader; private Map commands = new HashMap<>(); private List commandList = new ArrayList<>(); - public static void start(CakeLight cakelight, Configuration config) { - new Console(cakelight, config).start(); + public static Console start(CakeLight cakelight, Configuration config) { + Console console = new Console(cakelight, config); + console.start(); + return console; } private Console(CakeLight cakelight, Configuration config) { this.cakelight = cakelight; this.config = config; - reader = new BufferedReader(new InputStreamReader(System.in)); register(new HelpCommand()); register(Commands.quit()); register(Commands.video()); @@ -70,28 +70,32 @@ public class Console extends Thread { @Override public void run() { while (true) { - System.out.print("> "); - try { - String input = reader.readLine(); - String[] splitInput = input.split("\\s+", 2); - String name = splitInput[0]; - String[] args = splitInput.length == 2 - ? splitInput[1].split("\\s+") - : new String[]{}; - - Command cmd = commands.get(name); - if (cmd != null) { - cmd.activate(this, args); - } else { - out("no command named '" + name + "'"); - } - } catch (IOException e) { - System.out.println("Error reading from command line"); - break; - } + System.out.print("> "); + try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) { + String input = reader.readLine(); + handleInput(input); + } catch (IOException e) { + System.out.println("Error reading from command line"); + break; + } } } + void handleInput(String input) { + String[] splitInput = input.split("\\s+", 2); + String name = splitInput[0]; + String[] args = splitInput.length == 2 + ? splitInput[1].split("\\s+") + : new String[]{}; + + Command cmd = commands.get(name); + if (cmd != null) { + cmd.activate(this, args); + } else { + out("no command named '" + name + "'"); + } + } + void out(String text) { System.out.println("(" + text + ")"); }