Skip to content

Instantly share code, notes, and snippets.

@BoBkiNN
Created March 22, 2023 15:35
Show Gist options
  • Save BoBkiNN/8c5f5d3433e85880490dfdf57a49735c to your computer and use it in GitHub Desktop.
Save BoBkiNN/8c5f5d3433e85880490dfdf57a49735c to your computer and use it in GitHub Desktop.
Color fixer class for console color fixing in ignite launcher`s TerminalConsoleAppender
/*
* The MIT License (MIT)
*
* Copyright (c) 2017 Minecrell <https://github.com/Minecrell>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package net.minecrell.terminalconsole.util;
import java.util.HashMap;
import java.util.Map;
public class ColorFixer {
public static String ANSI_DEL = "\u007F";
public static Map<String, String> colorMap = new HashMap<>();
static final String ANSI_RESET = "\u001B[m";
private static final String COLOR_CHAR = "§";
private static final String[] ansiCodes = new String[] {
"\u001B[0;30m", // Black §0
"\u001B[0;34m", // Dark Blue §1
"\u001B[0;32m", // Dark Green §2
"\u001B[0;36m", // Dark Aqua §3
"\u001B[0;31m", // Dark Red §4
"\u001B[0;35m", // Dark Purple §5
"\u001B[0;33m", // Gold §6
"\u001B[0;37m", // Gray §7
"\u001B[0;30;1m", // Dark Gray §8
"\u001B[0;34;1m", // Blue §9
"\u001B[0;32;1m", // Green §a
"\u001B[0;36;1m", // Aqua §b
"\u001B[0;31;1m", // Red §c
"\u001B[0;35;1m", // Light Purple §d
"\u001B[0;33;1m", // Yellow §e
"\u001B[0;37;1m", // White §f
"\u001B[5m", // Obfuscated §k
"\u001B[21m", // Bold §l
"\u001B[9m", // Strikethrough §m
"\u001B[4m", // Underline §n
"\u001B[3m", // Italic §o
ANSI_RESET, // Reset §r
};
static {
String cols = "0123456789abcdefklmnor";
for (String color : cols.split("")){
int colorI = cols.indexOf(color);
String ansi = ansiCodes[colorI];
colorMap.put(COLOR_CHAR+color, ansi);
}
}
public static String fix(String original){
String noDel = original.replace(ANSI_DEL, COLOR_CHAR);
for (Map.Entry<String, String> entry : colorMap.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
noDel = noDel.replace(key, value);
}
return noDel + colorMap.get(COLOR_CHAR + "r");
}
}
@BoBkiNN
Copy link
Author

BoBkiNN commented May 31, 2024

Now no longer need due to new logging in ignite 1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment