From b3506329d5c3e0aca8bfc8e469826e0774e488cc Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 23 Dec 2016 12:48:44 +0300 Subject: juick-console: jline3 --- .../src/main/java/com/juick/console/Console.java | 5 +- .../main/java/com/juick/console/ConsoleApp.java | 53 ++++++++++++++-------- 2 files changed, 37 insertions(+), 21 deletions(-) (limited to 'juick-console/src') diff --git a/juick-console/src/main/java/com/juick/console/Console.java b/juick-console/src/main/java/com/juick/console/Console.java index 9fff298f..ad858c56 100644 --- a/juick-console/src/main/java/com/juick/console/Console.java +++ b/juick-console/src/main/java/com/juick/console/Console.java @@ -1,16 +1,17 @@ package com.juick.console; -import ch.vorburger.exec.ManagedProcessException; import com.juick.console.configuration.ConsoleAppConfiguration; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.core.env.SimpleCommandLinePropertySource; +import java.io.IOException; + /** * Created by vitalyster on 30.08.2016. */ public class Console { - public static void main(String ...args) throws ManagedProcessException { + public static void main(String ...args) throws IOException { final AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); // setup configuration 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(); } } } -- cgit v1.2.3