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;
import the.bytecode.club.bytecodeviewer.BytecodeViewer;
import the.bytecode.club.bytecodeviewer.Constants;
import javax.swing.*;
import java.io.PrintStream;
import static the.bytecode.club.bytecodeviewer.Constants.nl;
@ -35,6 +35,9 @@ public class JFrameConsolePrintStream extends JFrameConsole
{
private final JTextAreaOutputStream textAreaOutputStreamOut;
private final JTextAreaOutputStream textAreaOutputStreamErr;
private Thread updateThread;
private boolean finished;
private long lastUpdate = 0;
public JFrameConsolePrintStream(String title)
{
@ -47,21 +50,67 @@ public class JFrameConsolePrintStream extends JFrameConsole
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()
{
finished = true;
System.setErr(Constants.ERR);
System.setOut(Constants.OUT);
}
public void pretty()
private void update()
{
if(System.currentTimeMillis()-lastUpdate <= 50)
return;
lastUpdate = System.currentTimeMillis();
//update only if required
if(textAreaOutputStreamErr.noUpdateRequired() && textAreaOutputStreamOut.noUpdateRequired())
return;
SwingUtilities.invokeLater(()->
{
//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[] test;
if (getTextArea().getText().split("\n").length >= 2)
test = getTextArea().getText().split("\n");
if (content.split("\n").length >= 2)
test = content.split("\n");
else
test = getTextArea().getText().split("\r");
test = content.split("\r");
StringBuilder replace = new StringBuilder();
for (String s : test)
@ -74,6 +123,9 @@ public class JFrameConsolePrintStream extends JFrameConsole
}
replace.append(s).append(nl);
}
setText(replace.toString());
}
});
}
}

View file

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

View file

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

View file

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