Add truncateColor

This commit is contained in:
games647 2017-05-25 13:13:31 +02:00
parent 2896f6d767
commit 297c28d97d
No known key found for this signature in database
GPG Key ID: E9E962F08F621538
9 changed files with 44 additions and 18 deletions

View File

@ -8,7 +8,7 @@
<packaging>jar</packaging>
<name>ColorConsole</name>
<version>2.0</version>
<version>2.1</version>
<inceptionYear>2016</inceptionYear>
<url>http://dev.bukkit.org/bukkit-plugins/colorconsole/</url>
<description>

View File

@ -2,11 +2,9 @@ package com.github.games647.colorconsole.bukkit;
import com.github.games647.colorconsole.common.CommonLogInstaller;
import com.google.common.collect.Maps;
import java.io.Serializable;
import java.util.Map;
import java.util.logging.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.Layout;
@ -84,7 +82,6 @@ public class ColorConsoleBukkit extends JavaPlugin {
getLogger().log(Level.WARNING, "Cannot install log format", ex);
}
if (getConfig().getBoolean("colorPluginTag")) {
Logger rootLogger = ((Logger) LogManager.getRootLogger());

View File

@ -16,7 +16,10 @@ import org.bukkit.configuration.file.FileConfiguration;
public class ColorPluginAppender extends ColorAppender {
public ColorPluginAppender(Appender oldAppender, FileConfiguration config) {
super(oldAppender, config.getStringList("hide-messages"), config.getBoolean("colorPluginTag"));
super(oldAppender
, config.getStringList("hide-messages")
, config.getBoolean("colorPluginTag")
, config.getBoolean("truncateColor"));
}
@Override

View File

@ -32,7 +32,8 @@ public class ColorLogFormatter extends Formatter {
List<String> ignoreMessages = plugin.getConfiguration().getStringList("hide-messages");
boolean colorizeTag = plugin.getConfiguration().getBoolean("colorPluginTag");
this.formatter = new CommonFormatter(ignoreMessages, colorizeTag);
boolean truncateColor = plugin.getConfiguration().getBoolean("truncateColor", false);
this.formatter = new CommonFormatter(ignoreMessages, colorizeTag, truncateColor);
}
@Override

View File

@ -17,11 +17,12 @@ public abstract class ColorAppender extends AbstractAppender {
protected final CommonFormatter formatter;
protected ColorAppender(Appender oldAppender, Collection<String> hideMessages, boolean colorizeTag) {
protected ColorAppender(Appender oldAppender, Collection<String> hideMessages
, boolean colorizeTag, boolean truncateColor) {
super(oldAppender.getName(), null, oldAppender.getLayout());
this.oldAppender = oldAppender;
this.formatter = new CommonFormatter(hideMessages, colorizeTag);
this.formatter = new CommonFormatter(hideMessages, colorizeTag, truncateColor);
for (Method method : LogEvent.class.getDeclaredMethods()) {
String methodName = method.getName();

View File

@ -2,11 +2,12 @@ package com.github.games647.colorconsole.common;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import java.util.Collection;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.regex.Pattern;
import org.apache.logging.log4j.core.pattern.AnsiEscape;
import org.fusesource.jansi.Ansi;
import org.fusesource.jansi.Ansi.Attribute;
@ -15,15 +16,16 @@ import org.fusesource.jansi.AnsiRenderer;
public class CommonFormatter {
private final String reset = Ansi.ansi().a(Ansi.Attribute.RESET).toString();
private final Pattern tagPattern = Pattern.compile("\\[.{1,}\\]");
private Map<String, String> pluginColors;
private final Set<String> ignoreMessages;
private final boolean colorizeTag;
private final boolean truncateColor;
private Map<String, String> pluginColors;
public CommonFormatter(Collection<String> ignoreMessages, boolean colorizeTag) {
public CommonFormatter(Collection<String> ignoreMessages, boolean colorizeTag, boolean truncateColor) {
this.ignoreMessages = ImmutableSet.copyOf(ignoreMessages);
this.colorizeTag = colorizeTag;
this.truncateColor = truncateColor;
}
public boolean shouldIgnore(String message) {
@ -55,15 +57,27 @@ public class CommonFormatter {
}
public String colorizePluginTag(String message) {
if (!message.contains("[") || !message.contains("]") || message.startsWith(AnsiEscape.CSI.getCode())) {
if (!message.contains("[") || !message.contains("]")) {
return message;
}
int startTag = message.indexOf('[') + 1;
int endTag = message.indexOf(']', startTag);
String newMessage = message;
String pluginName = colorizePluginName(message.substring(startTag, endTag));
return '[' + pluginName + ']' + message.substring(endTag + 1);
String startingColorCode = "";
if (message.startsWith(AnsiEscape.CSI.getCode())) {
int endColor = message.indexOf(AnsiEscape.SUFFIX.getCode());
newMessage = message.substring(endColor + 1, message.length());
if (!truncateColor) {
startingColorCode = message.substring(0, endColor + 1);
}
}
int startTag = newMessage.indexOf('[') + 1;
int endTag = newMessage.indexOf(']', startTag);
String pluginName = colorizePluginName(newMessage.substring(startTag, endTag));
return '[' + pluginName + ']' + startingColorCode + newMessage.substring(endTag + 1);
}
public String colorizePluginName(String pluginName) {

View File

@ -65,6 +65,9 @@ public class ColorConsoleConfig {
+ "The texts are case-sensitive")
private Set<String> hideMessages;
@Setting(comment = "Removes color formatting if the complete message has color formatting")
private boolean truncateColor;
public boolean isColorPluginTag() {
return colorPluginTag;
}
@ -96,4 +99,8 @@ public class ColorConsoleConfig {
public String getDateStyle() {
return dateStyle;
}
public boolean isTruncateColor() {
return truncateColor;
}
}

View File

@ -12,7 +12,7 @@ import org.spongepowered.api.Sponge;
public class ColorPluginAppender extends ColorAppender {
public ColorPluginAppender(Appender oldAppender, ColorConsoleConfig config) {
super(oldAppender, config.getHideMessages(), config.isColorPluginTag());
super(oldAppender, config.getHideMessages(), config.isColorPluginTag(), config.isTruncateColor());
}
@Override

View File

@ -75,3 +75,6 @@ dateStyle: cyan
hide-messages:
- 'ThisIsATest'
- 'SecondTest'
# Removes color formatting if the complete message has color formatting
truncateColor: false