diff --git a/zond/build.gradle b/zond/build.gradle index 77e19fe..b75a046 100644 --- a/zond/build.gradle +++ b/zond/build.gradle @@ -1,5 +1,5 @@ group = 'asys' -version = '0.7.13-SNAPSHOT' +version = '0.7.14-SNAPSHOT' apply plugin: 'application' diff --git a/zond/src/main/java/asys/zond/shell/CommandLooper.java b/zond/src/main/java/asys/zond/shell/CommandLooper.java index 681577e..0350453 100644 --- a/zond/src/main/java/asys/zond/shell/CommandLooper.java +++ b/zond/src/main/java/asys/zond/shell/CommandLooper.java @@ -1,5 +1,5 @@ /* - * DmitriyMX + * DmitriyMX * 2017-06-15 */ package asys.zond.shell; @@ -11,12 +11,10 @@ import java.io.IOException; public class CommandLooper implements Runnable { private ConsoleReader consoleReader; private CommandHandler commandHandler; - private ShellStdOut output; CommandLooper(ConsoleReader consoleReader, CommandHandler commandHandler) { this.consoleReader = consoleReader; this.commandHandler = commandHandler; - this.output = (ShellStdOut) Shell.getInstance().getOutput(); } @Override @@ -24,9 +22,7 @@ public class CommandLooper implements Runnable { while (!Thread.currentThread().isInterrupted()) { String line; try { - output.needDrawLine = true; line = consoleReader.readLine(); - output.needDrawLine = false; } catch (IOException e) { break; } diff --git a/zond/src/main/java/asys/zond/shell/Shell.java b/zond/src/main/java/asys/zond/shell/Shell.java index f63bc2a..ed850c7 100644 --- a/zond/src/main/java/asys/zond/shell/Shell.java +++ b/zond/src/main/java/asys/zond/shell/Shell.java @@ -1,14 +1,12 @@ /* - * DmitriyMX + * DmitriyMX * 2017-06-15 */ package asys.zond.shell; import jline.console.ConsoleReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.PrintStream; +import java.io.*; public class Shell { private static final String DEFAULT_PROMPT = ":"; @@ -27,7 +25,7 @@ public class Shell { } public void start(InputStream inputStream) throws IOException { - consoleReader = new ConsoleReader(inputStream, (shellStdOut = new ShellStdOut())); + consoleReader = new ConsoleReader(inputStream, (shellStdOut = new ShellStdOut(System.out))); shellStdOut.setConsoleReader(consoleReader); consoleReader.setPrompt(DEFAULT_PROMPT); @@ -56,10 +54,6 @@ public class Shell { return shellStdOut; } - public void setOutputHook(ShellOutputHook outputHook) { - shellStdOut.setOutputHook(outputHook); - } - public void setCommandHandler(CommandHandler commandHandler) { this.commandHandler = commandHandler; } diff --git a/zond/src/main/java/asys/zond/shell/ShellOutputHook.java b/zond/src/main/java/asys/zond/shell/ShellOutputHook.java deleted file mode 100644 index 9936e2e..0000000 --- a/zond/src/main/java/asys/zond/shell/ShellOutputHook.java +++ /dev/null @@ -1,9 +0,0 @@ -/* - * DmitriyMX - * 2017-06-15 - */ -package asys.zond.shell; - -public interface ShellOutputHook { - String handle(String line); -} diff --git a/zond/src/main/java/asys/zond/shell/ShellStdOut.java b/zond/src/main/java/asys/zond/shell/ShellStdOut.java index 7205e2c..1cd8f60 100644 --- a/zond/src/main/java/asys/zond/shell/ShellStdOut.java +++ b/zond/src/main/java/asys/zond/shell/ShellStdOut.java @@ -1,83 +1,93 @@ /* - * DmitriyMX - * 2017-06-15 + * DmitriyMX + * 2017-07-23 */ package asys.zond.shell; import jline.console.ConsoleReader; +import java.io.IOException; +import java.io.OutputStream; import java.io.PrintStream; -import java.io.PrintWriter; public class ShellStdOut extends PrintStream { private ConsoleReader consoleReader; - private PrintWriter writer; - private ShellOutputHook shellOutputHook; - boolean needDrawLine = false; - ShellStdOut() { - super(System.out, true); + ShellStdOut(OutputStream out) { + super(out); } - void setConsoleReader(ConsoleReader consoleReader) { - this.consoleReader = consoleReader; - this.writer = new PrintWriter(consoleReader.getOutput()); + private void _write(char c) { + try { + this.out.write(c); + } catch (IOException ignore) { + } } - private void _print(String line) { - String newLine; - if (shellOutputHook != null) { - try { - newLine = shellOutputHook.handle(line); - if (newLine == null) return; - else line = newLine; - } catch (Exception ignore) { - } + private void _write(byte[] buf) { + try { + this.out.write(buf); + } catch (IOException ignore) { } + } - writer.print(ConsoleReader.RESET_LINE); - writer.print(line); - cleanTrashLine(line); - writer.println(); - if (needDrawLine) { - try { - consoleReader.drawLine(); - } catch (Throwable ignore) { - } + private void _write(byte[] buf, int off, int len) { + try { + this.out.write(buf, off, len); + } catch (IOException ignore) { + } + } + + private void _flush() { + try { + this.out.flush(); + } catch (IOException ignore) { } - writer.flush(); } /** * Очистка печатной строки от мусора */ - private void cleanTrashLine(String string) { + private void cleanTrashLine(int len) throws IOException { // очищает полностью строку - if (consoleReader.getCursorBuffer().buffer.length() + consoleReader.getPrompt().length() > string.length()) { - for (int i = string.length(); i <= consoleReader.getCursorBuffer().buffer.length() + 2; i++) { - writer.print(' '); + if (consoleReader.getCursorBuffer().buffer.length() + consoleReader.getPrompt().length() > len) { + for (int i = len; i <= consoleReader.getCursorBuffer().buffer.length() + 2; i++) { + this.out.write(' '); } + this.out.flush(); } } - void setOutputHook(ShellOutputHook shellOutputHook) { - this.shellOutputHook = shellOutputHook; + public void printEx(byte[] buf, int len) { + _write(ConsoleReader.RESET_LINE); + _write(buf, 0, len); + try { + cleanTrashLine(len); + consoleReader.drawLine(); + consoleReader.flush(); + } catch (Throwable ignore) { + } + _flush(); } @Override public void write(byte[] buf, int off, int len) { - if ((char)buf[len-1] == '\n') len--; - if ((char)buf[len-1] == '\r') len--; - _print(new String(buf, off, len)); + _write(buf, off, len); } @Override public void print(String s) { - _print(s); + _write(ConsoleReader.RESET_LINE); + _write(s.getBytes()); } @Override public void println(String x) { - _print(x); + print(x.concat("\n")); + _flush(); + } + + void setConsoleReader(ConsoleReader consoleReader) { + this.consoleReader = consoleReader; } } diff --git a/zond/src/main/java/org/apache/commons/exec/StreamPumper.java b/zond/src/main/java/org/apache/commons/exec/StreamPumper.java index b50bc13..5bc73ec 100644 --- a/zond/src/main/java/org/apache/commons/exec/StreamPumper.java +++ b/zond/src/main/java/org/apache/commons/exec/StreamPumper.java @@ -22,6 +22,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import asys.zond.shell.ShellStdOut; import org.apache.commons.exec.util.DebugUtils; /** @@ -106,7 +107,11 @@ public class StreamPumper implements Runnable { try { //hack: пропатчили алгоритм while (!Thread.currentThread().isInterrupted() && (length = is.read(buf)) > 0) { - os.write(buf, 0, length); + if (os instanceof ShellStdOut) { + ((ShellStdOut)os).printEx(buf, length); + } else { + os.write(buf, 0, length); + } os.flush(); } } catch (final Exception e) {