Better Console Printing

This commit is contained in:
Konloch 2021-07-07 16:49:06 -07:00
parent f1b36e785f
commit a4abb49d6a
4 changed files with 81 additions and 29 deletions

View file

@ -1,8 +1,8 @@
package the.bytecode.club.bytecodeviewer.gui.components; package the.bytecode.club.bytecodeviewer.gui.components;
import the.bytecode.club.bytecodeviewer.BytecodeViewer;
import the.bytecode.club.bytecodeviewer.Constants; import the.bytecode.club.bytecodeviewer.Constants;
import javax.swing.*;
import java.io.PrintStream; import java.io.PrintStream;
import static the.bytecode.club.bytecodeviewer.Constants.nl; import static the.bytecode.club.bytecodeviewer.Constants.nl;
@ -35,6 +35,9 @@ public class JFrameConsolePrintStream extends JFrameConsole
{ {
private final JTextAreaOutputStream textAreaOutputStreamOut; private final JTextAreaOutputStream textAreaOutputStreamOut;
private final JTextAreaOutputStream textAreaOutputStreamErr; private final JTextAreaOutputStream textAreaOutputStreamErr;
private Thread updateThread;
private boolean finished;
private long lastUpdate = 0;
public JFrameConsolePrintStream(String title) public JFrameConsolePrintStream(String title)
{ {
@ -47,33 +50,82 @@ public class JFrameConsolePrintStream extends JFrameConsole
System.setErr(new PrintStream(textAreaOutputStreamErr)); System.setErr(new PrintStream(textAreaOutputStreamErr));
} }
@Override
public void setVisible(boolean b)
{
super.setVisible(b);
if(b && updateThread == null)
{
updateThread = new Thread(() ->
{
while (isVisible() && !finished)
{
update();
try {
Thread.sleep(10);
} catch (InterruptedException e) { }
}
lastUpdate = 0;
update();
}, "Lazy Console Update");
updateThread.start();
}
}
public void finished() public void finished()
{ {
finished = true;
System.setErr(Constants.ERR); System.setErr(Constants.ERR);
System.setOut(Constants.OUT); System.setOut(Constants.OUT);
} }
public void pretty() private void update()
{ {
textAreaOutputStreamOut.update(); if(System.currentTimeMillis()-lastUpdate <= 50)
textAreaOutputStreamErr.update(); return;
String[] test;
if (getTextArea().getText().split("\n").length >= 2)
test = getTextArea().getText().split("\n");
else
test = getTextArea().getText().split("\r");
StringBuilder replace = new StringBuilder(); lastUpdate = System.currentTimeMillis();
for (String s : test)
//update only if required
if(textAreaOutputStreamErr.noUpdateRequired() && textAreaOutputStreamOut.noUpdateRequired())
return;
SwingUtilities.invokeLater(()->
{ {
if (s.startsWith("File '")) //print output to the pane
textAreaOutputStreamOut.update();
//print error to the pane
textAreaOutputStreamErr.update();
//reformat the pane
String content = getTextArea().getText();
if(content.contains("File `"))
{ {
String[] split = s.split("'"); String[] test;
String start = split[0] + "'" + split[1] + "', "; if (content.split("\n").length >= 2)
s = s.substring(start.length()); test = content.split("\n");
else
test = content.split("\r");
StringBuilder replace = new StringBuilder();
for (String s : test)
{
if (s.startsWith("File '"))
{
String[] split = s.split("'");
String start = split[0] + "'" + split[1] + "', ";
s = s.substring(start.length());
}
replace.append(s).append(nl);
}
setText(replace.toString());
} }
replace.append(s).append(nl); });
}
setText(replace.toString());
} }
} }

View file

@ -1,7 +1,6 @@
package the.bytecode.club.bytecodeviewer.gui.components; package the.bytecode.club.bytecodeviewer.gui.components;
import javax.swing.*; import javax.swing.*;
import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.PrintStream; import java.io.PrintStream;
@ -29,7 +28,7 @@ import java.io.PrintStream;
*/ */
public class JTextAreaOutputStream extends OutputStream public class JTextAreaOutputStream extends OutputStream
{ {
private final StringBuilder sb = new StringBuilder(); private StringBuilder sb = new StringBuilder();
private final JTextArea textArea; private final JTextArea textArea;
private final PrintStream og; private final PrintStream og;
@ -39,13 +38,19 @@ public class JTextAreaOutputStream extends OutputStream
this.og = og; this.og = og;
} }
public boolean noUpdateRequired()
{
return sb.length() <= 0;
}
public void update() public void update()
{ {
textArea.append(sb.toString()); textArea.append(sb.toString());
sb = new StringBuilder();
} }
@Override @Override
public void write(int b) throws IOException public void write(int b)
{ {
sb.append((char) b); sb.append((char) b);
og.write(b); og.write(b);

View file

@ -99,7 +99,6 @@ public class ResourceViewPanel
e.printStackTrace(); e.printStackTrace();
} }
errConsole.pretty();
errConsole.setVisible(true); errConsole.setVisible(true);
errConsole.finished(); errConsole.finished();
return false; return false;

View file

@ -47,8 +47,8 @@ import static the.bytecode.club.bytecodeviewer.Constants.*;
* @author Konloch * @author Konloch
*/ */
public class EZInjection extends Plugin { public class EZInjection extends Plugin
{
public static ArrayList<BytecodeHook> hookArray = new ArrayList<>(); public static ArrayList<BytecodeHook> hookArray = new ArrayList<>();
private static final String version = "1.0"; private static final String version = "1.0";
private static final PluginConsole gui = new PluginConsole("EZ Injection v" + version); private static final PluginConsole gui = new PluginConsole("EZ Injection v" + version);
@ -134,11 +134,7 @@ public class EZInjection extends Plugin {
public static void print(String message) public static void print(String message)
{ {
if (printCmdL) System.out.println(message);
System.out.println(message);
if (gui.isVisible())
gui.appendText(message);
} }
@Override @Override