mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-27 08:14:45 +00:00
Fall back to ASCII bullets on non-Unicode consoles
This commit is contained in:
1 parent
b545500abe
commit
01874b1e6f
3 files changed
+98
-1
No files matched your search
@@ -49,6 +49,7 @@ import art.arcane.iris.integration.IrisPapiState;
|
||||
import art.arcane.iris.integration.MultiverseCoreLink;
|
||||
import art.arcane.iris.integration.MultiverseSVC;
|
||||
import art.arcane.iris.localization.C;
|
||||
import art.arcane.iris.localization.ConsoleGlyphs;
|
||||
import art.arcane.iris.localization.IrisLanguage;
|
||||
import art.arcane.iris.pack.datapack.DatapackIngestService;
|
||||
import art.arcane.iris.pack.datapack.DatapackIngestService.StartupValidationOutcome;
|
||||
@@ -604,7 +605,8 @@ public final class IrisRuntime implements IrisPluginRuntime {
|
||||
};
|
||||
}
|
||||
|
||||
private static void diagnostic(Level level, String message) {
|
||||
private static void diagnostic(Level level, String rawMessage) {
|
||||
String message = ConsoleGlyphs.console().fit(rawMessage);
|
||||
if (shadesConsole()) {
|
||||
logShaded(level, logPrefix() + C.escapeTags(message));
|
||||
return;
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
package art.arcane.iris.localization;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.IllegalCharsetNameException;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/** Swaps the non-ASCII glyphs Iris prints for ASCII ones when the console charset cannot encode them. */
|
||||
public final class ConsoleGlyphs {
|
||||
private static final Map<String, String> ASCII_FALLBACKS = asciiFallbacks();
|
||||
private static final String[] CONSOLE_CHARSET_PROPERTIES = {"stdout.encoding", "native.encoding"};
|
||||
private static final ConsoleGlyphs CONSOLE = forCharset(consoleCharset());
|
||||
|
||||
private final Map<String, String> replacements;
|
||||
|
||||
private ConsoleGlyphs(Map<String, String> replacements) {
|
||||
this.replacements = replacements;
|
||||
}
|
||||
|
||||
public static ConsoleGlyphs console() {
|
||||
return CONSOLE;
|
||||
}
|
||||
|
||||
public static ConsoleGlyphs forCharset(Charset charset) {
|
||||
Map<String, String> unencodable = new LinkedHashMap<>();
|
||||
for (Map.Entry<String, String> fallback : ASCII_FALLBACKS.entrySet()) {
|
||||
if (!charset.newEncoder().canEncode(fallback.getKey())) {
|
||||
unencodable.put(fallback.getKey(), fallback.getValue());
|
||||
}
|
||||
}
|
||||
return new ConsoleGlyphs(unencodable);
|
||||
}
|
||||
|
||||
public String fit(String text) {
|
||||
if (text == null || replacements.isEmpty()) {
|
||||
return text;
|
||||
}
|
||||
String fitted = text;
|
||||
for (Map.Entry<String, String> replacement : replacements.entrySet()) {
|
||||
fitted = fitted.replace(replacement.getKey(), replacement.getValue());
|
||||
}
|
||||
return fitted;
|
||||
}
|
||||
|
||||
private static Map<String, String> asciiFallbacks() {
|
||||
Map<String, String> fallbacks = new LinkedHashMap<>();
|
||||
fallbacks.put("\u00bb", ">");
|
||||
fallbacks.put("\u2039", "<");
|
||||
fallbacks.put("\u203a", ">");
|
||||
return fallbacks;
|
||||
}
|
||||
|
||||
private static Charset consoleCharset() {
|
||||
for (String property : CONSOLE_CHARSET_PROPERTIES) {
|
||||
String name = System.getProperty(property);
|
||||
if (name == null || name.isBlank()) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
if (Charset.isSupported(name)) {
|
||||
return Charset.forName(name);
|
||||
}
|
||||
} catch (IllegalCharsetNameException ignored) {
|
||||
// An unusable property value falls through to the next source.
|
||||
}
|
||||
}
|
||||
return Charset.defaultCharset();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package art.arcane.iris.localization;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class ConsoleGlyphsTest {
|
||||
private static final String REPORT_LINE = " \u00bb Biome \u2039caves\u203a";
|
||||
|
||||
@Test
|
||||
public void utf8ConsoleKeepsGlyphs() {
|
||||
assertEquals(REPORT_LINE, ConsoleGlyphs.forCharset(StandardCharsets.UTF_8).fit(REPORT_LINE));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void asciiConsoleGetsAsciiGlyphs() {
|
||||
assertEquals(" > Biome <caves>", ConsoleGlyphs.forCharset(StandardCharsets.US_ASCII).fit(REPORT_LINE));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void latin1ConsoleKeepsWhatItCanEncode() {
|
||||
assertEquals(" \u00bb Biome <caves>", ConsoleGlyphs.forCharset(StandardCharsets.ISO_8859_1).fit(REPORT_LINE));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user