diff options
author | Vitaly Takmazov | 2016-12-23 12:48:44 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2016-12-23 12:48:44 +0300 |
commit | b3506329d5c3e0aca8bfc8e469826e0774e488cc (patch) | |
tree | c7d3b51687145daabc6dbf772b9f3a813405864f | |
parent | 1bc50475e5a226ca3467882dc50a8264cd376159 (diff) |
juick-console: jline3
-rw-r--r-- | juick-console/build.gradle | 7 | ||||
-rw-r--r-- | juick-console/src/main/java/com/juick/console/Console.java | 5 | ||||
-rw-r--r-- | juick-console/src/main/java/com/juick/console/ConsoleApp.java | 53 |
3 files changed, 38 insertions, 27 deletions
diff --git a/juick-console/build.gradle b/juick-console/build.gradle index 064493ce..0c20d04b 100644 --- a/juick-console/build.gradle +++ b/juick-console/build.gradle @@ -8,17 +8,12 @@ apply plugin: 'java' apply plugin: 'application' apply plugin: 'com.github.ben-manes.versions' -repositories { - jcenter() -} - mainClassName = "com.juick.console.Console" dependencies { compile project(':juick-server') compile 'mysql:mysql-connector-java:5.1.40' - compile 'ch.vorburger.mariaDB4j:mariaDB4j:2.2.2' - compile 'jline:jline:2.14.2' + compile 'org.jline:jline:3.1.2' } compileJava.options.encoding = 'UTF-8' 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(); } } } |