From ebe9474a7098bf3e73f6e0de7fda38888657bbc5 Mon Sep 17 00:00:00 2001 From: ME1312 Date: Tue, 10 Apr 2018 02:21:07 -0400 Subject: [PATCH] Add the illusive ConsoleStream.java --- .../Host/Library/Log/ConsoleStream.java | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 SubServers.Host/src/net/ME1312/SubServers/Host/Library/Log/ConsoleStream.java diff --git a/SubServers.Host/src/net/ME1312/SubServers/Host/Library/Log/ConsoleStream.java b/SubServers.Host/src/net/ME1312/SubServers/Host/Library/Log/ConsoleStream.java new file mode 100644 index 00000000..7a0231a6 --- /dev/null +++ b/SubServers.Host/src/net/ME1312/SubServers/Host/Library/Log/ConsoleStream.java @@ -0,0 +1,59 @@ +package net.ME1312.SubServers.Host.Library.Log; + +import jline.console.ConsoleReader; +import jline.console.CursorBuffer; +import net.ME1312.SubServers.Host.Library.Container; +import net.ME1312.SubServers.Host.Library.TextColor; + +import java.io.IOException; +import java.io.PrintStream; +import java.util.LinkedList; + +/** + * Console Log Stream Class + */ +public class ConsoleStream extends PrintStream { + private ConsoleReader jline; + private PrintStream original; + private LinkedList buffer; + private CursorBuffer hidden; + + public ConsoleStream(ConsoleReader jline, PrintStream original) { + super(original); + this.jline = jline; + this.buffer = new LinkedList(); + this.original = original; + } + + @Override + public void write(int i) { + try { + if (((char) i) == '\n') { + LinkedList buffer = this.buffer; + this.buffer = new LinkedList(); + hide(); + for (Integer b : buffer) original.write(b); + original.print(TextColor.RESET.asAnsiCode()); + original.write(i); + show(); + } else { + buffer.add(i); + } + } catch (Exception e) {} + } + + private void hide() { + hidden = jline.getCursorBuffer().copy(); + try { + jline.getOutput().write("\u001b[1G\u001b[K"); + jline.flush(); + } catch (IOException e) {} + } + + private void show() { + try { + jline.resetPromptLine(jline.getPrompt(), hidden.toString(), hidden.cursor); + jline.flush(); + } catch (IOException e) {} + } +}