diff options
Diffstat (limited to 'juick-console/src/main/java/com/juick/console/ConsoleApp.java')
-rw-r--r-- | juick-console/src/main/java/com/juick/console/ConsoleApp.java | 53 |
1 files changed, 34 insertions, 19 deletions
diff --git a/juick-console/src/main/java/com/juick/console/ConsoleApp.java b/juick-console/src/main/java/com/juick/console/ConsoleApp.java index 9a061bb6..22a26609 100644 --- a/juick-console/src/main/java/com/juick/console/ConsoleApp.java +++ b/juick-console/src/main/java/com/juick/console/ConsoleApp.java @@ -4,7 +4,12 @@ import com.juick.User; import com.juick.server.protocol.JuickProtocol; import com.juick.server.protocol.ProtocolReply; import com.juick.service.UserService; -import jline.console.ConsoleReader; +import org.jline.reader.EndOfFileException; +import org.jline.reader.LineReader; +import org.jline.reader.LineReaderBuilder; +import org.jline.reader.UserInterruptException; +import org.jline.terminal.Terminal; +import org.jline.terminal.TerminalBuilder; import org.springframework.context.ApplicationContext; import org.springframework.core.env.Environment; @@ -27,30 +32,40 @@ public class ConsoleApp { public ConsoleApp(ApplicationContext context) { juickProtocol = context.getBean(JuickProtocol.class); } - public void start() { - //jline.TerminalFactory.registerFlavor(jline.TerminalFactory.Flavor.WINDOWS, UnsupportedTerminal.class); - try (ConsoleReader reader = new ConsoleReader()) { - PrintWriter out = new PrintWriter(reader.getOutput()); - String uname = env.getProperty("uname", "ugnich"); - User user = userService.getUserByName(uname); - if (user != null) { - reader.setPrompt(String.format("%s>", user.getName())); - out.println(String.format("logged in as @%s", user.getName())); - out.flush(); - String input; - while ((input = reader.readLine()) != null) { + public void start() throws IOException { + Terminal terminal = TerminalBuilder.builder() + .system(true) + .nativeSignals(true) + .jna(true) + .build(); + LineReader reader = LineReaderBuilder.builder() + .terminal(terminal) + .build(); + PrintWriter out = terminal.writer(); + String uname = env.getProperty("uname", "ugnich"); + User user = userService.getUserByName(uname); + if (user != null) { + String prompt = String.format("@%s>", user.getName()); + while (true) { + try { + String input = reader.readLine(prompt, null, null); ProtocolReply reply = juickProtocol.getReply(user, input); out.println(reply.getResult()); out.flush(); + } catch (UserInterruptException e) { + + } catch (EndOfFileException e) { + break; + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); } } + } else { out.println("uname missing or invalid"); - } catch (IOException | NoSuchMethodException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (InvocationTargetException e) { - e.printStackTrace(); } } } |