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,33 +50,82 @@ 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()
{
textAreaOutputStreamOut.update();
textAreaOutputStreamErr.update();
String[] test;
if (getTextArea().getText().split("\n").length >= 2)
test = getTextArea().getText().split("\n");
else
test = getTextArea().getText().split("\r");
if(System.currentTimeMillis()-lastUpdate <= 50)
return;
StringBuilder replace = new StringBuilder();
for (String s : test)
lastUpdate = System.currentTimeMillis();
//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 start = split[0] + "'" + split[1] + "', ";
s = s.substring(start.length());
String[] test;
if (content.split("\n").length >= 2)
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;
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);
System.out.println(message);
}
@Override