Merge remote-tracking branch 'origin/pr/3' into dev
This commit is contained in:
1
pom.xml
1
pom.xml
@@ -73,6 +73,7 @@
|
|||||||
<artifactId>jansi</artifactId>
|
<artifactId>jansi</artifactId>
|
||||||
<version>1.11</version>
|
<version>1.11</version>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<!-- Build behavior -->
|
<!-- Build behavior -->
|
||||||
|
|||||||
@@ -0,0 +1,90 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016 Ronald Jack Jenkins Jr.
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
package info.ronjenkins.slf4bukkit;
|
||||||
|
|
||||||
|
import com.google.common.collect.ImmutableMap;
|
||||||
|
|
||||||
|
import org.bukkit.ChatColor;
|
||||||
|
import org.fusesource.jansi.Ansi;
|
||||||
|
import org.fusesource.jansi.Ansi.Attribute;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Maps {@link ChatColor} values to their Jansi equivalents.
|
||||||
|
*
|
||||||
|
* <p>This class might not always be instantiable as Jansi might not be present at runtime.</p>
|
||||||
|
*
|
||||||
|
* @author Ronald Jack Jenkins Jr.
|
||||||
|
*/
|
||||||
|
final class AnsiColorMapper implements ColorMapper {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new instance.
|
||||||
|
*
|
||||||
|
* <p>This class relies on Jansi. If this dependency is not present at runtime, it cannot be initialized as the JVM
|
||||||
|
* raises a {@code Throwable} due to missing dependencies.</p>
|
||||||
|
*
|
||||||
|
* <p>In this case callers of this constructor should fall back to an alternative {@code ColorMapper}
|
||||||
|
* implementation.</p>
|
||||||
|
*
|
||||||
|
* @throws Throwable if Jansi is not present at runtime
|
||||||
|
*/
|
||||||
|
AnsiColorMapper() throws Throwable {
|
||||||
|
//see javadoc
|
||||||
|
}
|
||||||
|
|
||||||
|
// @formatter:off
|
||||||
|
private final Map<ChatColor, String> MAP = ImmutableMap.<ChatColor, String>builder()
|
||||||
|
.put(ChatColor.BLACK, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.BLACK).boldOff().toString())
|
||||||
|
.put(ChatColor.DARK_BLUE, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.BLUE).boldOff().toString())
|
||||||
|
.put(ChatColor.DARK_GREEN, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.GREEN).boldOff().toString())
|
||||||
|
.put(ChatColor.DARK_AQUA, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.CYAN).boldOff().toString())
|
||||||
|
.put(ChatColor.DARK_RED, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.RED).boldOff().toString())
|
||||||
|
.put(ChatColor.DARK_PURPLE, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.MAGENTA).boldOff().toString())
|
||||||
|
.put(ChatColor.GOLD, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.YELLOW).boldOff().toString())
|
||||||
|
.put(ChatColor.GRAY, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.WHITE).boldOff().toString())
|
||||||
|
.put(ChatColor.DARK_GRAY, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.BLACK).bold().toString())
|
||||||
|
.put(ChatColor.BLUE, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.BLUE).bold().toString())
|
||||||
|
.put(ChatColor.GREEN, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.GREEN).bold().toString())
|
||||||
|
.put(ChatColor.AQUA, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.CYAN).bold().toString())
|
||||||
|
.put(ChatColor.RED, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.RED).bold().toString())
|
||||||
|
.put(ChatColor.LIGHT_PURPLE, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.MAGENTA).bold().toString())
|
||||||
|
.put(ChatColor.YELLOW, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.YELLOW).bold().toString())
|
||||||
|
.put(ChatColor.WHITE, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.WHITE).bold().toString())
|
||||||
|
.put(ChatColor.MAGIC, Ansi.ansi().a(Attribute.BLINK_SLOW).toString())
|
||||||
|
.put(ChatColor.BOLD, Ansi.ansi().a(Attribute.UNDERLINE_DOUBLE).toString())
|
||||||
|
.put(ChatColor.STRIKETHROUGH, Ansi.ansi().a(Attribute.STRIKETHROUGH_ON).toString())
|
||||||
|
.put(ChatColor.UNDERLINE, Ansi.ansi().a(Attribute.UNDERLINE).toString())
|
||||||
|
.put(ChatColor.ITALIC, Ansi.ansi().a(Attribute.ITALIC).toString())
|
||||||
|
.put(ChatColor.RESET, Ansi.ansi().a(Attribute.RESET).toString())
|
||||||
|
.build();
|
||||||
|
// @formatter:on
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String map(final String input) {
|
||||||
|
if (input == null) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
String output = input;
|
||||||
|
for (final Map.Entry<ChatColor, String> mapping : MAP.entrySet()) {
|
||||||
|
output = output.replace(mapping.getKey().toString(), mapping.getValue());
|
||||||
|
}
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -16,63 +16,21 @@
|
|||||||
*/
|
*/
|
||||||
package info.ronjenkins.slf4bukkit;
|
package info.ronjenkins.slf4bukkit;
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
import org.fusesource.jansi.Ansi;
|
|
||||||
import org.fusesource.jansi.Ansi.Attribute;
|
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableMap;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Utility class that maps {@link ChatColor} values to their JAnsi equivalents,
|
* Utility class that maps {@link ChatColor} values to their equivalents,
|
||||||
* so that messages logged to the console are formatted correctly.
|
* so that messages logged to the console are formatted correctly.
|
||||||
*
|
|
||||||
* @author Ronald Jack Jenkins Jr.
|
|
||||||
*/
|
*/
|
||||||
public final class ColorMapper {
|
public interface ColorMapper {
|
||||||
|
|
||||||
// @formatter:off
|
|
||||||
private static final Map<ChatColor, String> MAP = ImmutableMap.<ChatColor, String>builder()
|
|
||||||
.put(ChatColor.BLACK, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.BLACK).boldOff().toString())
|
|
||||||
.put(ChatColor.DARK_BLUE, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.BLUE).boldOff().toString())
|
|
||||||
.put(ChatColor.DARK_GREEN, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.GREEN).boldOff().toString())
|
|
||||||
.put(ChatColor.DARK_AQUA, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.CYAN).boldOff().toString())
|
|
||||||
.put(ChatColor.DARK_RED, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.RED).boldOff().toString())
|
|
||||||
.put(ChatColor.DARK_PURPLE, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.MAGENTA).boldOff().toString())
|
|
||||||
.put(ChatColor.GOLD, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.YELLOW).boldOff().toString())
|
|
||||||
.put(ChatColor.GRAY, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.WHITE).boldOff().toString())
|
|
||||||
.put(ChatColor.DARK_GRAY, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.BLACK).bold().toString())
|
|
||||||
.put(ChatColor.BLUE, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.BLUE).bold().toString())
|
|
||||||
.put(ChatColor.GREEN, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.GREEN).bold().toString())
|
|
||||||
.put(ChatColor.AQUA, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.CYAN).bold().toString())
|
|
||||||
.put(ChatColor.RED, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.RED).bold().toString())
|
|
||||||
.put(ChatColor.LIGHT_PURPLE, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.MAGENTA).bold().toString())
|
|
||||||
.put(ChatColor.YELLOW, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.YELLOW).bold().toString())
|
|
||||||
.put(ChatColor.WHITE, Ansi.ansi().a(Attribute.RESET).fg(Ansi.Color.WHITE).bold().toString())
|
|
||||||
.put(ChatColor.MAGIC, Ansi.ansi().a(Attribute.BLINK_SLOW).toString())
|
|
||||||
.put(ChatColor.BOLD, Ansi.ansi().a(Attribute.UNDERLINE_DOUBLE).toString())
|
|
||||||
.put(ChatColor.STRIKETHROUGH, Ansi.ansi().a(Attribute.STRIKETHROUGH_ON).toString())
|
|
||||||
.put(ChatColor.UNDERLINE, Ansi.ansi().a(Attribute.UNDERLINE).toString())
|
|
||||||
.put(ChatColor.ITALIC, Ansi.ansi().a(Attribute.ITALIC).toString())
|
|
||||||
.put(ChatColor.RESET, Ansi.ansi().a(Attribute.RESET).toString())
|
|
||||||
.build();
|
|
||||||
// @formatter:on
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Translates {@link ChatColor} directives to their JAnsi equivalents.
|
* Translates {@link ChatColor} directives to their equivalents.
|
||||||
*
|
*
|
||||||
* @param input
|
* @param input
|
||||||
* null is coerced to the empty string.
|
* null is coerced to the empty string.
|
||||||
* @return never null.
|
* @return never null.
|
||||||
*/
|
*/
|
||||||
public static String map(final String input) {
|
String map(String input);
|
||||||
if (input == null) { return ""; }
|
|
||||||
String output = input;
|
|
||||||
for (final Map.Entry<ChatColor, String> mapping : ColorMapper.MAP.entrySet()) {
|
|
||||||
output = output.replace(mapping.getKey().toString(), mapping.getValue());
|
|
||||||
}
|
|
||||||
return output;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package info.ronjenkins.slf4bukkit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates {@code ColorMapper} instances.
|
||||||
|
*
|
||||||
|
* @see ColorMapper
|
||||||
|
*/
|
||||||
|
public final class ColorMapperFactory {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an new {@code ColorMapper} instance.
|
||||||
|
*
|
||||||
|
* @return a new instance
|
||||||
|
*/
|
||||||
|
public static ColorMapper create() {
|
||||||
|
try {
|
||||||
|
return new AnsiColorMapper();
|
||||||
|
} catch (Throwable throwable) {
|
||||||
|
return new NotSupportedColorMapper();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package info.ronjenkins.slf4bukkit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Does not do any mapping but simply returns the given string or, if {@code null} is given, an empty string.
|
||||||
|
*/
|
||||||
|
final class NotSupportedColorMapper implements ColorMapper {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String map(final String input) {
|
||||||
|
return input == null ? "" : input;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -45,6 +45,7 @@
|
|||||||
package org.slf4j.impl;
|
package org.slf4j.impl;
|
||||||
|
|
||||||
import info.ronjenkins.slf4bukkit.ColorMapper;
|
import info.ronjenkins.slf4bukkit.ColorMapper;
|
||||||
|
import info.ronjenkins.slf4bukkit.ColorMapperFactory;
|
||||||
import info.ronjenkins.slf4bukkit.ColorMarker;
|
import info.ronjenkins.slf4bukkit.ColorMarker;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -218,6 +219,7 @@ public final class BukkitLoggerAdapter implements Logger {
|
|||||||
// The logger name.
|
// The logger name.
|
||||||
private final String name;
|
private final String name;
|
||||||
// The short name of this simple log instance
|
// The short name of this simple log instance
|
||||||
|
private final ColorMapper mapper = ColorMapperFactory.create();
|
||||||
private transient String shortLogName = null;
|
private transient String shortLogName = null;
|
||||||
|
|
||||||
// NOTE: BukkitPluginLoggerAdapter constructor should have only package access
|
// NOTE: BukkitPluginLoggerAdapter constructor should have only package access
|
||||||
@@ -1043,6 +1045,6 @@ public final class BukkitLoggerAdapter implements Logger {
|
|||||||
|
|
||||||
// Log the message.
|
// Log the message.
|
||||||
logger.log(BukkitLoggerAdapter.slf4jLevelIntToBukkitJULLevel(level),
|
logger.log(BukkitLoggerAdapter.slf4jLevelIntToBukkitJULLevel(level),
|
||||||
ColorMapper.map(buf.toString()));
|
mapper.map(buf.toString()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user