mirror of
https://github.com/ME1312/SubServers-2.git
synced 2024-11-22 10:15:52 +01:00
Update SubServers.Console
This commit is contained in:
parent
94a95de495
commit
3635fea2f5
@ -323,19 +323,20 @@ public class InternalSubCreator extends SubCreator {
|
||||
}).getPort();
|
||||
}
|
||||
|
||||
CreatorTask task = new CreatorTask(player, name, template, version, port, server -> {
|
||||
if (callback != null && server != null) try {
|
||||
callback.run(server);
|
||||
} catch (Throwable e) {
|
||||
Throwable ew = new InvocationTargetException(e);
|
||||
ew.setStackTrace(origin);
|
||||
ew.printStackTrace();
|
||||
}
|
||||
});
|
||||
this.thread.put(name.toLowerCase(), task);
|
||||
|
||||
final SubCreateEvent event = new SubCreateEvent(player, host, name, template, version, port);
|
||||
host.plugin.getPluginManager().callEvent(event);
|
||||
if (!event.isCancelled()) {
|
||||
CreatorTask task = new CreatorTask(player, name, template, version, port, server -> {
|
||||
if (callback != null && server != null) try {
|
||||
callback.run(server);
|
||||
} catch (Throwable e) {
|
||||
Throwable ew = new InvocationTargetException(e);
|
||||
ew.setStackTrace(origin);
|
||||
ew.printStackTrace();
|
||||
}
|
||||
});
|
||||
this.thread.put(name.toLowerCase(), task);
|
||||
task.start();
|
||||
return true;
|
||||
} else {
|
||||
|
@ -151,7 +151,7 @@ public final class SubCommand implements CommandExecutor {
|
||||
if (plugin.config.get().getSection("Settings").getBoolean("Show-Addresses", false)) {
|
||||
message += " (" + host.getAddress() + ((host.getName().equals(host.getDisplayName()))?"":ChatColor.stripColor(div)+host.getName()) + ")";
|
||||
} else if (!host.getName().equals(host.getDisplayName())) {
|
||||
message += " (" + host + ")";
|
||||
message += " (" + host.getName() + ")";
|
||||
}
|
||||
message += plugin.api.getLang("SubServers", "Command.List.Header");
|
||||
for (SubServer subserver : host.getSubServers().values()) {
|
||||
|
@ -17,6 +17,13 @@
|
||||
</repositories>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.fusesource.jansi</groupId>
|
||||
<artifactId>jansi</artifactId>
|
||||
<version>1.17.1</version>
|
||||
<scope>compile</scope>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.intellij</groupId>
|
||||
<artifactId>forms_rt</artifactId>
|
||||
|
@ -1,170 +0,0 @@
|
||||
package net.ME1312.SubServers.Console;
|
||||
|
||||
import org.fusesource.jansi.AnsiOutputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class AnsiHTMLColorStream extends AnsiOutputStream {
|
||||
private static final String[] ANSI_COLOR_MAP = new String[]{"000000", "cd0000", "25bc24", "e1e100", "0000ee", "cd00cd", "00e1e1", "ffffff"};
|
||||
private static final byte[] BYTES_NBSP = " ".getBytes();
|
||||
private static final byte[] BYTES_QUOT = """.getBytes();
|
||||
private static final byte[] BYTES_AMP = "&".getBytes();
|
||||
private static final byte[] BYTES_LT = "<".getBytes();
|
||||
private static final byte[] BYTES_GT = ">".getBytes();
|
||||
private LinkedList<String> closingAttributes = new LinkedList<String>();
|
||||
private boolean underline = false;
|
||||
private boolean strikethrough = false;
|
||||
|
||||
public void close() throws IOException {
|
||||
this.closeAttributes();
|
||||
super.close();
|
||||
}
|
||||
|
||||
public AnsiHTMLColorStream(OutputStream os) {
|
||||
super(os);
|
||||
}
|
||||
|
||||
private void write(String s) throws IOException {
|
||||
super.out.write(s.getBytes());
|
||||
}
|
||||
|
||||
private void writeAttribute(String s) throws IOException {
|
||||
this.write("<" + s + ">");
|
||||
this.closingAttributes.add(0, s);
|
||||
}
|
||||
|
||||
private void closeAttribute(String s) throws IOException {
|
||||
LinkedList<String> closedAttributes = new LinkedList<String>();
|
||||
LinkedList<String> closingAttributes = new LinkedList<String>();
|
||||
LinkedList<String> unclosedAttributes = new LinkedList<String>();
|
||||
|
||||
closingAttributes.addAll(this.closingAttributes);
|
||||
for (String attr : closingAttributes) {
|
||||
if (attr.toLowerCase().startsWith(s.toLowerCase())) {
|
||||
for (String a : unclosedAttributes) {
|
||||
closedAttributes.add(0, a);
|
||||
this.write("</" + a.split(" ", 2)[0] + ">");
|
||||
}
|
||||
this.closingAttributes.removeFirstOccurrence(attr);
|
||||
unclosedAttributes.clear();
|
||||
this.write("</" + attr.split(" ", 2)[0] + ">");
|
||||
} else {
|
||||
unclosedAttributes.add(attr);
|
||||
}
|
||||
}
|
||||
for (String attr : closedAttributes) {
|
||||
this.write("<" + attr + ">");
|
||||
}
|
||||
}
|
||||
|
||||
private void closeAttributes() throws IOException {
|
||||
for (String attr : closingAttributes) {
|
||||
this.write("</" + attr.split(" ", 2)[0] + ">");
|
||||
}
|
||||
|
||||
this.underline = false;
|
||||
this.strikethrough = false;
|
||||
this.closingAttributes.clear();
|
||||
}
|
||||
|
||||
private boolean nbsp = true;
|
||||
@Override public void write(int data) throws IOException {
|
||||
if (data == 32) {
|
||||
if (nbsp) this.out.write(BYTES_NBSP);
|
||||
else super.write(data);
|
||||
nbsp = !nbsp;
|
||||
} else {
|
||||
nbsp = false;
|
||||
switch(data) {
|
||||
case 34:
|
||||
this.out.write(BYTES_QUOT);
|
||||
break;
|
||||
case 38:
|
||||
this.out.write(BYTES_AMP);
|
||||
break;
|
||||
case 60:
|
||||
this.out.write(BYTES_LT);
|
||||
break;
|
||||
case 62:
|
||||
this.out.write(BYTES_GT);
|
||||
break;
|
||||
default:
|
||||
super.write(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void writeLine(byte[] buf, int offset, int len) throws IOException {
|
||||
this.write(buf, offset, len);
|
||||
this.closeAttributes();
|
||||
}
|
||||
|
||||
private String parseTextDecoration() {
|
||||
String dec = "";
|
||||
if (underline) dec += " underline";
|
||||
if (strikethrough) dec += " line-through";
|
||||
if (dec.length() <= 0) dec += " none";
|
||||
|
||||
return dec.substring(1);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void processSetAttribute(int attribute) throws IOException {
|
||||
switch(attribute) {
|
||||
case 1:
|
||||
this.writeAttribute("b");
|
||||
break;
|
||||
case 3:
|
||||
this.writeAttribute("i");
|
||||
break;
|
||||
case 4:
|
||||
this.closeAttribute("span class=\"ansi-decoration");
|
||||
this.underline = true;
|
||||
this.writeAttribute("span class=\"ansi-decoration\" style=\"text-decoration: " + parseTextDecoration() + ";\"");
|
||||
break;
|
||||
case 9:
|
||||
this.closeAttribute("span class=\"ansi-decoration");
|
||||
this.strikethrough = true;
|
||||
this.writeAttribute("span class=\"ansi-decoration\" style=\"text-decoration: " + parseTextDecoration() + ";\"");
|
||||
break;
|
||||
case 22:
|
||||
this.closeAttribute("b");
|
||||
break;
|
||||
case 23:
|
||||
this.closeAttribute("i");
|
||||
break;
|
||||
case 24:
|
||||
this.closeAttribute("span class=\"ansi-decoration");
|
||||
this.underline = false;
|
||||
this.writeAttribute("span class=\"ansi-decoration\" style=\"text-decoration: " + parseTextDecoration() + ";\"");
|
||||
break;
|
||||
case 29:
|
||||
this.closeAttribute("span class=\"ansi-decoration");
|
||||
this.strikethrough = false;
|
||||
this.writeAttribute("span class=\"ansi-decoration\" style=\"text-decoration: " + parseTextDecoration() + ";\"");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void processAttributeRest() throws IOException {
|
||||
this.closeAttributes();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void processSetForegroundColor(int color) throws IOException {
|
||||
this.writeAttribute("span class=\"ansi-foreground\" style=\"color: #" + ANSI_COLOR_MAP[color] + ";\"");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void processSetBackgroundColor(int color) throws IOException {
|
||||
this.writeAttribute("span class=\"ansi-background\" style=\"background-color: #" + ANSI_COLOR_MAP[color] + ";\"");
|
||||
}
|
||||
}
|
Binary file not shown.
@ -5,8 +5,6 @@ import net.ME1312.SubServers.Bungee.Host.SubLogFilter;
|
||||
import net.ME1312.SubServers.Bungee.Host.SubLogger;
|
||||
import net.ME1312.SubServers.Bungee.Host.SubServer;
|
||||
import net.ME1312.SubServers.Bungee.Library.Util;
|
||||
import net.md_5.bungee.api.ProxyServer;
|
||||
import org.fusesource.jansi.AnsiOutputStream;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.swing.*;
|
||||
@ -19,16 +17,15 @@ import javax.swing.text.html.HTMLEditorKit;
|
||||
import javax.swing.text.html.StyleSheet;
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.*;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
|
||||
public final class ConsoleWindow implements SubLogFilter {
|
||||
private static String RESET_VALUE = "\n\u00A0\n\u00A0";
|
||||
private static final int MAX_SCROLLBACK = (Integer.getInteger("subservers.console.max_scrollback", 15000) >= 128)?Integer.getInteger("subservers.console.max_scrollback", 15000):15000;
|
||||
private static final String RESET_VALUE = "\n\u00A0\n\u00A0";
|
||||
private ConsolePlugin plugin;
|
||||
private JFrame window;
|
||||
private JPanel panel;
|
||||
@ -49,25 +46,50 @@ public final class ConsoleWindow implements SubLogFilter {
|
||||
private boolean open = false;
|
||||
private SubLogger logger;
|
||||
private int fontSize = 12;
|
||||
private boolean ansi = true;
|
||||
private AnsiOutputStream stream = new AnsiHTMLColorStream(new OutputStream() {
|
||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||
private File file = null;
|
||||
private FileOutputStream filewriter = null;
|
||||
private ByteArrayOutputStream scache = new ByteArrayOutputStream();
|
||||
private AnsiUIOutputStream stream = AnsiUIOutputStream.wrap(new OutputStream() {
|
||||
|
||||
private int countLines(String str) {
|
||||
int count = 0;
|
||||
for (int i = 0; i < str.length(); i++) if (str.charAt(i) == '\n') count++;
|
||||
return count;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(int b) throws IOException {
|
||||
stream.write(b);
|
||||
if (((char) b) == '\n') {
|
||||
scache.write(b);
|
||||
if (b == '\n') {
|
||||
try {
|
||||
int lines;
|
||||
String content;
|
||||
while (log.getSelectionStart() == log.getSelectionEnd() && (lines = countLines(content = log.getDocument().getText(0, log.getDocument().getLength()))) > MAX_SCROLLBACK) {
|
||||
int lineBreak = 1;
|
||||
for (lines -= MAX_SCROLLBACK; lines > 0; lines--) lineBreak = content.indexOf('\n', lineBreak + 1);
|
||||
if (lineBreak >= 2) {
|
||||
log.getDocument().remove(2, lineBreak);
|
||||
} else break;
|
||||
}
|
||||
} catch (Exception e) {} try {
|
||||
HTMLEditorKit kit = (HTMLEditorKit) log.getEditorKit();
|
||||
HTMLDocument doc = (HTMLDocument) log.getDocument();
|
||||
kit.insertHTML(doc, doc.getLength() - 2, new String(stream.toByteArray(), "UTF-8"), 0, 0, null);
|
||||
hScroll();
|
||||
} catch (BadLocationException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
stream = new ByteArrayOutputStream();
|
||||
kit.insertHTML(doc, doc.getLength() - 2, new String(scache.toByteArray(), "UTF-8"), 0, 0, null);
|
||||
EventQueue.invokeLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
hScroll();
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {}
|
||||
scache = new ByteArrayOutputStream();
|
||||
}
|
||||
}
|
||||
}, new HTMLogger.HTMConstructor<AnsiUIOutputStream>() {
|
||||
@Override
|
||||
public AnsiUIOutputStream construct(OutputStream raw, OutputStream wrapped) {
|
||||
return new AnsiUIOutputStream(raw, wrapped);
|
||||
}
|
||||
});
|
||||
private boolean[] kpressed = new boolean[65535];
|
||||
private KeyEventDispatcher keys = new KeyEventDispatcher() {
|
||||
@ -116,6 +138,14 @@ public final class ConsoleWindow implements SubLogFilter {
|
||||
this.plugin = plugin;
|
||||
this.logger = logger;
|
||||
|
||||
try {
|
||||
file = File.createTempFile("SubServers.Console.", ".log");
|
||||
file.deleteOnExit();
|
||||
filewriter = new FileOutputStream(file, false);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
window = new JFrame();
|
||||
|
||||
JMenuBar jMenu = new JMenuBar();
|
||||
@ -135,6 +165,7 @@ public final class ConsoleWindow implements SubLogFilter {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent event) {
|
||||
log.setText(RESET_VALUE);
|
||||
ConsoleWindow.this.loadContent();
|
||||
}
|
||||
});
|
||||
menu.add(item);
|
||||
@ -241,13 +272,14 @@ public final class ConsoleWindow implements SubLogFilter {
|
||||
});
|
||||
menu.add(item);
|
||||
menu.addSeparator();
|
||||
item = new JCheckBoxMenuItem("Show Text Colors");
|
||||
item = new JCheckBoxMenuItem("Use ANSI Formatting");
|
||||
item.setSelected(true);
|
||||
item.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent event) {
|
||||
ansi = ((AbstractButton) event.getSource()).getModel().isSelected();
|
||||
stream.ansi(((AbstractButton) event.getSource()).getModel().isSelected());
|
||||
log.setText(RESET_VALUE);
|
||||
ConsoleWindow.this.loadContent();
|
||||
}
|
||||
});
|
||||
menu.add(item);
|
||||
@ -321,7 +353,16 @@ public final class ConsoleWindow implements SubLogFilter {
|
||||
log.setContentType("text/html");
|
||||
log.setEditorKit(new HTMLEditorKit());
|
||||
StyleSheet style = new StyleSheet();
|
||||
style.addRule("body {color: #dcdcdc; font-family: courier; font-size: 12;}\n");
|
||||
String font;
|
||||
try {
|
||||
Font f = Font.createFont(Font.TRUETYPE_FONT, ConsoleWindow.class.getResourceAsStream("/net/ME1312/SubServers/Console/ConsoleFont.ttf"));
|
||||
GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(f);
|
||||
font = f.getFontName();
|
||||
input.setFont(f.deriveFont(14f));
|
||||
} catch (Exception e) {
|
||||
font = "Courier";
|
||||
}
|
||||
style.addRule("body {color: #dcdcdc; font-family: " + font + "; font-size: 12;}\n");
|
||||
log.setDocument(new HTMLDocument(style));
|
||||
log.setBorder(BorderFactory.createLineBorder(new Color(40, 44, 45)));
|
||||
new TextFieldPopup(log, false);
|
||||
@ -515,6 +556,7 @@ public final class ConsoleWindow implements SubLogFilter {
|
||||
|
||||
logger.registerFilter(this);
|
||||
log.setText(RESET_VALUE);
|
||||
loadContent();
|
||||
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(keys);
|
||||
if (logger.isLogging() && !open) open();
|
||||
}
|
||||
@ -531,7 +573,9 @@ public final class ConsoleWindow implements SubLogFilter {
|
||||
|
||||
public void log(Date date, String message) {
|
||||
try {
|
||||
stream.write(('\u00A0' + new SimpleDateFormat("hh:mm:ss").format(date) + ' ' + ((ansi)?message:message.replaceAll("\u001B\\[[;\\d]*m", "")) + "\u00A0\n").getBytes("UTF-8"));
|
||||
byte[] msg = ('\u00A0' + new SimpleDateFormat("hh:mm:ss").format(date) + ' ' + message + "\u00A0\n").getBytes("UTF-8");
|
||||
filewriter.write(msg);
|
||||
stream.write(msg);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -569,10 +613,33 @@ public final class ConsoleWindow implements SubLogFilter {
|
||||
return open;
|
||||
}
|
||||
|
||||
private void loadContent() {
|
||||
if (file != null) {
|
||||
try (FileInputStream reader = new FileInputStream(file)) {
|
||||
int b;
|
||||
while ((b = reader.read()) != -1) {
|
||||
stream.write(b);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
hScroll();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
close();
|
||||
clear();
|
||||
if (filewriter != null) try {
|
||||
filewriter.close();
|
||||
} catch (Exception e) {}
|
||||
if (file != null) try {
|
||||
filewriter = new FileOutputStream(file, false);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
public void close() {
|
||||
if (open) {
|
||||
@ -590,6 +657,10 @@ public final class ConsoleWindow implements SubLogFilter {
|
||||
public void destroy() {
|
||||
close();
|
||||
logger.unregisterFilter(this);
|
||||
if (filewriter != null) try {
|
||||
filewriter.close();
|
||||
} catch (Exception e) {}
|
||||
if (file != null) file.delete();
|
||||
KeyboardFocusManager.getCurrentKeyboardFocusManager().removeKeyEventDispatcher(keys);
|
||||
}
|
||||
|
||||
@ -753,6 +824,51 @@ public final class ConsoleWindow implements SubLogFilter {
|
||||
}
|
||||
}
|
||||
}
|
||||
private class AnsiUIOutputStream extends HTMLogger {
|
||||
public AnsiUIOutputStream(OutputStream raw, OutputStream wrapped) {
|
||||
super(raw, wrapped);
|
||||
}
|
||||
|
||||
public void ansi(boolean value) {
|
||||
ansi = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void processChangeWindowTitle(String label) {
|
||||
window.setTitle(logger.getName() + " \u2014 SubServers 2" + ((label.length() <= 0)?"":" \u2014 " + label));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void processEraseLine(int mode) throws IOException {
|
||||
processDeleteLine(1);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void processEraseScreen(int mode) throws IOException {
|
||||
if (ansi) log.setText(RESET_VALUE);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void processDeleteLine(int amount) throws IOException {
|
||||
if (ansi) try {
|
||||
String content = log.getDocument().getText(0, log.getDocument().getLength());
|
||||
while (amount > 0) {
|
||||
int lastLineBreak = content.lastIndexOf('\n');
|
||||
int length = log.getDocument().getLength() - lastLineBreak - 2;
|
||||
if (lastLineBreak >= 0 && length > 0) {
|
||||
log.getDocument().remove(lastLineBreak, length);
|
||||
}
|
||||
amount--;
|
||||
}
|
||||
} catch (Exception e) {}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
open = false;
|
||||
super.close();
|
||||
}
|
||||
}
|
||||
private class SmartScroller implements AdjustmentListener {
|
||||
public final static int HORIZONTAL = 0;
|
||||
public final static int VERTICAL = 1;
|
||||
|
@ -0,0 +1,304 @@
|
||||
package net.ME1312.SubServers.Console;
|
||||
|
||||
import net.ME1312.SubServers.Bungee.Library.Container;
|
||||
import org.fusesource.jansi.AnsiOutputStream;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.net.URLDecoder;
|
||||
import java.util.LinkedList;
|
||||
|
||||
/**
|
||||
* HTML Log Stream Class
|
||||
*/
|
||||
public class HTMLogger extends AnsiOutputStream {
|
||||
private static final String[] ANSI_COLOR_MAP = new String[]{"000000", "cd0000", "25bc24", "d7d700", "0000c3", "be00be", "00a5dc", "cccccc"};
|
||||
private static final String[] ANSI_BRIGHT_COLOR_MAP = new String[]{"808080", "ff0000", "31e722", "ffff00", "0000ff", "ff00ff", "00c8ff", "ffffff"};
|
||||
private static final byte[] BYTES_NBSP = " ".getBytes();
|
||||
private static final byte[] BYTES_QUOT = """.getBytes();
|
||||
private static final byte[] BYTES_AMP = "&".getBytes();
|
||||
private static final byte[] BYTES_LT = "<".getBytes();
|
||||
private static final byte[] BYTES_GT = ">".getBytes();
|
||||
private LinkedList<String> closingAttributes = new LinkedList<String>();
|
||||
private OutputStream raw;
|
||||
protected boolean ansi = true;
|
||||
private boolean underline = false;
|
||||
private boolean strikethrough = false;
|
||||
|
||||
/**
|
||||
* Parse data from an OutputStream
|
||||
*
|
||||
* @param raw OutputStream
|
||||
* @return HTMLogger
|
||||
*/
|
||||
public static HTMLogger wrap(OutputStream raw) {
|
||||
return wrap(raw, new HTMConstructor<HTMLogger>() {
|
||||
@Override
|
||||
public HTMLogger construct(OutputStream raw1, OutputStream wrapped) {
|
||||
return new HTMLogger(raw1, wrapped);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse data from an OutputStream
|
||||
*
|
||||
* @param raw OutputStream
|
||||
* @param constructor Implementing Constructor
|
||||
* @param <T> Logger Type
|
||||
* @return HTMLogger
|
||||
*/
|
||||
public static <T extends HTMLogger> T wrap(final OutputStream raw, HTMConstructor<T> constructor) {
|
||||
final Container<T> html = new Container<T>(null);
|
||||
html.set(constructor.construct(raw, new OutputStream() {
|
||||
private boolean nbsp = false;
|
||||
|
||||
@Override
|
||||
public void write(int data) throws IOException {
|
||||
if (data == 32) {
|
||||
if (nbsp) raw.write(BYTES_NBSP);
|
||||
else raw.write(data);
|
||||
nbsp = !nbsp;
|
||||
} else {
|
||||
nbsp = false;
|
||||
switch(data) {
|
||||
case 34:
|
||||
raw.write(BYTES_QUOT);
|
||||
break;
|
||||
case 38:
|
||||
raw.write(BYTES_AMP);
|
||||
break;
|
||||
case 60:
|
||||
raw.write(BYTES_LT);
|
||||
break;
|
||||
case 62:
|
||||
raw.write(BYTES_GT);
|
||||
break;
|
||||
case 10:
|
||||
html.get().closeAttributes();
|
||||
default:
|
||||
raw.write(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
}));
|
||||
return html.get();
|
||||
}
|
||||
protected HTMLogger(final OutputStream raw, OutputStream wrapped) {
|
||||
super(wrapped);
|
||||
this.raw = raw;
|
||||
}
|
||||
public interface HTMConstructor<T extends HTMLogger> {
|
||||
T construct(OutputStream raw, OutputStream wrapped);
|
||||
}
|
||||
|
||||
private void write(String s) throws IOException {
|
||||
raw.write(s.getBytes());
|
||||
}
|
||||
|
||||
private void writeAttribute(String s) throws IOException {
|
||||
write("<" + s + ">");
|
||||
closingAttributes.add(0, s);
|
||||
}
|
||||
|
||||
protected void closeAttribute(String s) throws IOException {
|
||||
LinkedList<String> closedAttributes = new LinkedList<String>();
|
||||
LinkedList<String> closingAttributes = new LinkedList<String>();
|
||||
LinkedList<String> unclosedAttributes = new LinkedList<String>();
|
||||
|
||||
closingAttributes.addAll(closingAttributes);
|
||||
for (String attr : closingAttributes) {
|
||||
if (attr.toLowerCase().startsWith(s.toLowerCase())) {
|
||||
for (String a : unclosedAttributes) {
|
||||
closedAttributes.add(0, a);
|
||||
write("</" + a.split(" ", 2)[0] + ">");
|
||||
}
|
||||
closingAttributes.removeFirstOccurrence(attr);
|
||||
unclosedAttributes.clear();
|
||||
write("</" + attr.split(" ", 2)[0] + ">");
|
||||
} else {
|
||||
unclosedAttributes.add(attr);
|
||||
}
|
||||
}
|
||||
for (String attr : closedAttributes) {
|
||||
write("<" + attr + ">");
|
||||
}
|
||||
}
|
||||
|
||||
protected void closeAttributes() throws IOException {
|
||||
for (String attr : closingAttributes) {
|
||||
write("</" + attr.split(" ", 2)[0] + ">");
|
||||
}
|
||||
|
||||
underline = false;
|
||||
strikethrough = false;
|
||||
closingAttributes.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void processDeleteLine(int amount) throws IOException {
|
||||
super.processDeleteLine(amount);
|
||||
}
|
||||
|
||||
private String parseTextDecoration() {
|
||||
String dec = "";
|
||||
if (underline) dec += " underline";
|
||||
if (strikethrough) dec += " line-through";
|
||||
if (dec.length() <= 0) dec += " none";
|
||||
|
||||
return dec.substring(1);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void processSetAttribute(int attribute) throws IOException {
|
||||
if (ansi) switch(attribute) {
|
||||
case 1:
|
||||
closeAttribute("b");
|
||||
writeAttribute("b");
|
||||
break;
|
||||
case 3:
|
||||
closeAttribute("i");
|
||||
writeAttribute("i");
|
||||
break;
|
||||
case 4:
|
||||
closeAttribute("span class=\"ansi-decoration");
|
||||
underline = true;
|
||||
writeAttribute("span class=\"ansi-decoration\" style=\"text-decoration: " + parseTextDecoration() + ";\"");
|
||||
break;
|
||||
case 9:
|
||||
closeAttribute("span class=\"ansi-decoration");
|
||||
strikethrough = true;
|
||||
writeAttribute("span class=\"ansi-decoration\" style=\"text-decoration: " + parseTextDecoration() + ";\"");
|
||||
break;
|
||||
case 22:
|
||||
closeAttribute("b");
|
||||
break;
|
||||
case 23:
|
||||
closeAttribute("i");
|
||||
break;
|
||||
case 24:
|
||||
closeAttribute("span class=\"ansi-decoration");
|
||||
underline = false;
|
||||
writeAttribute("span class=\"ansi-decoration\" style=\"text-decoration: " + parseTextDecoration() + ";\"");
|
||||
break;
|
||||
case 29:
|
||||
closeAttribute("span class=\"ansi-decoration");
|
||||
strikethrough = false;
|
||||
writeAttribute("span class=\"ansi-decoration\" style=\"text-decoration: " + parseTextDecoration() + ";\"");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void processUnknownOperatingSystemCommand(int label, String arg) {
|
||||
try {
|
||||
if (ansi) switch (label) {
|
||||
case 99900: // Galaxi Console Exclusives 99900-99999
|
||||
closeAttribute("a");
|
||||
writeAttribute("a href=\"" + arg + "\" target=\"_blank\"");
|
||||
break;
|
||||
case 99901:
|
||||
closeAttribute("a");
|
||||
break;
|
||||
}
|
||||
} catch (Exception e) {}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void processAttributeRest() throws IOException {
|
||||
closeAttributes();
|
||||
}
|
||||
|
||||
private String parse8BitColor(int color) throws IOException {
|
||||
if (color < 8) {
|
||||
return ANSI_COLOR_MAP[color];
|
||||
} else if (color < 16) {
|
||||
return ANSI_BRIGHT_COLOR_MAP[color - 8];
|
||||
} else if (color < 232) {
|
||||
int r = (int) (Math.floor((color - 16) / 36d) * (255 / 5));
|
||||
int g = (int) (Math.floor(((color - 16) % 36d) / 6d) * (255 / 5));
|
||||
int b = (int) (Math.floor(((color - 16) % 36d) % 6d) * (255 / 5));
|
||||
return ((r >= 16)?"":"0") + Integer.toString(r, 16) + ((g >= 16)?"":"0") + Integer.toString(g, 16) + ((b >= 16)?"":"0") + Integer.toString(b, 16);
|
||||
} else if (color < 256) {
|
||||
int gray = (int) ((255 / 25d) * (color - 232 + 1));
|
||||
return ((gray >= 16)?"":"0") + Integer.toString(gray, 16) + ((gray >= 16)?"":"0") + Integer.toString(gray, 16) + ((gray >= 16)?"":"0") + Integer.toString(gray, 16);
|
||||
} else {
|
||||
throw new IOException("Invalid 8 Bit Color: " + color);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void processDefaultTextColor() throws IOException {
|
||||
closeAttribute("span class=\"ansi-foreground");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void processSetForegroundColor(int color) throws IOException {
|
||||
processSetForegroundColor(color, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void processSetForegroundColor(int color, boolean bright) throws IOException {
|
||||
if (ansi) {
|
||||
processDefaultTextColor();
|
||||
writeAttribute("span class=\"ansi-foreground\" style=\"color: #" + ((!bright)?ANSI_COLOR_MAP:ANSI_BRIGHT_COLOR_MAP)[color] + ";\"");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void processSetForegroundColorExt(int index) throws IOException {
|
||||
if (ansi) {
|
||||
processDefaultTextColor();
|
||||
writeAttribute("span class=\"ansi-foreground\" style=\"color: #" + parse8BitColor(index) + ";\"");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void processSetForegroundColorExt(int r, int g, int b) throws IOException {
|
||||
if (ansi) {
|
||||
processDefaultTextColor();
|
||||
writeAttribute("span class=\"ansi-foreground\" style=\"color: #" + ((r >= 16)?"":"0") + Integer.toString(r, 16) + ((g >= 16)?"":"0") + Integer.toString(g, 16) + ((b >= 16)?"":"0") + Integer.toString(b, 16) + ";\"");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void processDefaultBackgroundColor() throws IOException {
|
||||
closeAttribute("span class=\"ansi-background");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void processSetBackgroundColor(int color) throws IOException {
|
||||
processSetBackgroundColor(color, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void processSetBackgroundColor(int color, boolean bright) throws IOException {
|
||||
if (ansi) {
|
||||
processDefaultBackgroundColor();
|
||||
writeAttribute("span class=\"ansi-background\" style=\"background-color: #" + ((!bright)?ANSI_COLOR_MAP:ANSI_BRIGHT_COLOR_MAP)[color] + ";\"");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void processSetBackgroundColorExt(int index) throws IOException {
|
||||
if (ansi) {
|
||||
processDefaultBackgroundColor();
|
||||
writeAttribute("span class=\"ansi-background\" style=\"background-color: #" + parse8BitColor(index) + ";\"");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void processSetBackgroundColorExt(int r, int g, int b) throws IOException {
|
||||
if (ansi) {
|
||||
processDefaultBackgroundColor();
|
||||
writeAttribute("span class=\"ansi-background\" style=\"background-color: #" + ((r >= 16)?"":"0") + Integer.toString(r, 16) + ((g >= 16)?"":"0") + Integer.toString(g, 16) + ((b >= 16)?"":"0") + Integer.toString(b, 16) + ";\"");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
closeAttributes();
|
||||
super.close();
|
||||
raw.close();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user