From 70af3ed7e1ea29094b1cdcb45f9e860f469b944c Mon Sep 17 00:00:00 2001 From: Risto Lahtela <24460436+Rsl1122@users.noreply.github.com> Date: Tue, 5 May 2020 23:26:37 +0300 Subject: [PATCH] Columns & better space compensation --- .../plan/commands/subcommands/DevCommand.java | 18 ++- .../plan/utilities/chat/ChatFormatter.java | 128 ++++++++++++++++-- .../plan/utilities/chat/DefaultFontInfo.java | 1 - 3 files changed, 134 insertions(+), 13 deletions(-) diff --git a/Plan/common/src/main/java/com/djrapitops/plan/commands/subcommands/DevCommand.java b/Plan/common/src/main/java/com/djrapitops/plan/commands/subcommands/DevCommand.java index 751f418c2..84431c654 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/commands/subcommands/DevCommand.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/commands/subcommands/DevCommand.java @@ -24,6 +24,7 @@ import com.djrapitops.plugin.command.CommandNode; import com.djrapitops.plugin.command.CommandType; import com.djrapitops.plugin.command.Sender; import com.djrapitops.plugin.utilities.Verify; +import org.apache.commons.lang3.StringUtils; import org.apache.commons.text.TextStringBuilder; import javax.inject.Inject; @@ -54,16 +55,25 @@ public class DevCommand extends CommandNode { Verify.isTrue(args.length >= 1, () -> new IllegalArgumentException(locale.getString(CommandLang.FAIL_REQ_ONE_ARG, Arrays.toString(this.getArguments())))); + sender.sendMessage(" |space"); + sender.sendMessage("§l §r|fat space"); + sender.sendMessage(" |space"); + sender.sendMessage("§l §r|fat space"); + Object actual = sender.getSender(); try { Method method = actual.getClass().getMethod("sendMessage", String.class); - int indent = 8; +// int indent = new Random().nextInt(25); String msg = new TextStringBuilder().appendWithSeparators(args, " ").toString(); - method.invoke(actual, "With indent: " + indent); - method.invoke(actual, ChatFormatter.indent(indent, msg)); - method.invoke(actual, "Centered:"); +// method.invoke(actual, "With indent: " + indent); +// method.invoke(actual, ChatFormatter.leftPad(msg, indent)); +// method.invoke(actual, "Centered:"); method.invoke(actual, ChatFormatter.center(msg)); + method.invoke(actual, "Table:"); + String[] split = StringUtils.split(msg, ':'); + int columnCount = split[0].length() - split[0].replace("-", "").length(); + method.invoke(actual, ChatFormatter.columns(columnCount, split, "-")); } catch (Exception e) { e.printStackTrace(); } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/utilities/chat/ChatFormatter.java b/Plan/common/src/main/java/com/djrapitops/plan/utilities/chat/ChatFormatter.java index 7bac3c230..9248356c4 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/utilities/chat/ChatFormatter.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/utilities/chat/ChatFormatter.java @@ -2,16 +2,19 @@ package com.djrapitops.plan.utilities.chat; import org.apache.commons.lang3.StringUtils; +import java.util.ArrayList; +import java.util.List; + /** * Formats chat messages in different ways. *

* Original code: https://hastebin.com/uziyajirag.avrasm */ public class ChatFormatter { - private final static int CENTER_PX = 154; - private final static int MAX_PX = 250; + private static final int CENTER_PX = 154; + private static final int MAX_PX = 260; - public static String indent(int spaces, String message) { + public static String leftPad(String message, int spaces) { StringBuilder returnMessage = new StringBuilder(); String padding = StringUtils.leftPad("", spaces); @@ -32,10 +35,104 @@ public class ChatFormatter { line = new StringBuilder(padding); } } - returnMessage.append(line.toString().trim()); + returnMessage.append(StringUtils.chop(line.toString())); return returnMessage.toString(); } + public static String columns(int columns, String[] lines, String separator) { + StringBuilder returnMessage = new StringBuilder(); + List table = new ArrayList<>(); + for (String line : lines) { + table.add(StringUtils.split(line, separator, columns)); + } + int[] biggestWidth = new int[columns]; + for (String[] line : table) { + for (int i = 0; i < line.length; i++) { + int width = getPxMessageWidth(line[i]); + if (biggestWidth[i] < width) { + biggestWidth[i] = width; + } + } + } + + for (String[] line : table) { + StringBuilder lineBuilder = new StringBuilder(); + + String currentStyle = ""; + for (int i = 0; i < line.length; i++) { + int columnWidth = getPxMessageWidth(line[i]); + int required = biggestWidth[i] + DefaultFontInfo.SPACE.getLength() + 1; + lineBuilder.append(line[i]); + currentStyle = getLastStyle(currentStyle + line[i]); + compensate(required, columnWidth, lineBuilder, currentStyle); + } + returnMessage.append(lineBuilder.toString()).append("\n"); + } + return returnMessage.toString(); + } + + public static String getLastStyle(String message) { + boolean wasColorChar = false; + char color = ' '; + boolean k = false; + boolean l = false; + boolean m = false; + boolean n = false; + boolean o = false; + for (char c : message.toCharArray()) { + if (c == '\u00a7') { // § + wasColorChar = true; + } else if (wasColorChar) { + switch (c) { + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + case 'a': + case 'b': + case 'c': + case 'd': + case 'e': + case 'f': + color = c; + break; + case 'k': + k = true; + break; + case 'l': + l = true; + break; + case 'm': + m = true; + break; + case 'n': + n = true; + break; + case 'o': + o = true; + break; + case 'r': + color = ' '; + k = false; + l = false; + m = false; + n = false; + o = false; + break; + default: + break; + } + } + } + return (color == ' ' ? "§" + color : "") + (k ? "§k" : "") + (l ? "§l" : "") + (m ? "§m" : "") + (n ? "§n" : "") + (o ? "§o" : ""); + } + public static String center(String message) { if (message == null) return null; if (message.isEmpty()) return ""; @@ -44,14 +141,29 @@ public class ChatFormatter { int halfOfWidth = messagePxWidth / 2; int toCompensate = CENTER_PX - halfOfWidth; - int spaceLength = DefaultFontInfo.SPACE.getLength() + 1; int compensated = 0; StringBuilder sb = new StringBuilder(); + compensate(toCompensate, compensated, sb, ""); + return sb.append(message).toString(); + } + + public static void compensate(int toCompensate, int compensated, StringBuilder builder, String currentStyle) { + int space = DefaultFontInfo.SPACE.getLength() + 1; + int boldSpace = DefaultFontInfo.SPACE.getBoldLength() + 1; while (compensated < toCompensate) { - sb.append(" "); - compensated += spaceLength; + int left = toCompensate - compensated; + if (left % 6 == 0) { + builder.append("§l"); + for (int i = 0; i < left / 6; i++) { + builder.append(" "); + compensated += boldSpace; + } + builder.append("§r").append(currentStyle); + } else { + builder.append(" "); + compensated += space; + } } - return sb.toString() + message; } public static int getPxMessageWidth(String message) { diff --git a/Plan/common/src/main/java/com/djrapitops/plan/utilities/chat/DefaultFontInfo.java b/Plan/common/src/main/java/com/djrapitops/plan/utilities/chat/DefaultFontInfo.java index daf7b32c0..64e77ee71 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/utilities/chat/DefaultFontInfo.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/utilities/chat/DefaultFontInfo.java @@ -128,7 +128,6 @@ public enum DefaultFontInfo { } public int getBoldLength() { - if (this == DefaultFontInfo.SPACE) return this.getLength(); return this.length + 1; } }