diff options
author | 2016-12-23 12:48:44 +0300 | |
---|---|---|
committer | 2016-12-23 12:48:44 +0300 | |
commit | b3506329d5c3e0aca8bfc8e469826e0774e488cc (patch) | |
tree | c7d3b51687145daabc6dbf772b9f3a813405864f /juick-console/src/main/java/com/juick/console/ConsoleApp.java | |
parent | 1bc50475e5a226ca3467882dc50a8264cd376159 (diff) |
juick-console: jline3
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(); } } } |