├── README.md └── TableGenerator.java /README.md: -------------------------------------------------------------------------------- 1 | # TableGenerator 2 | -------------------------------------------------------------------------------- /TableGenerator.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Arrays; 3 | import java.util.List; 4 | import java.util.regex.Matcher; 5 | import java.util.regex.Pattern; 6 | 7 | // Copyright by FisheyLP, Version 1.3 (12.08.16) 8 | public class TableGenerator { 9 | 10 | private static String delimiter = " | "; 11 | private static List char7 = Arrays.asList('°', '~', '@'); 12 | private static List char5 = Arrays.asList('"', '{', '}', '(', ')', '*', 'f', 'k', '<', '>'); 13 | private static List char4 = Arrays.asList('I', 't', ' ', '[', ']', '€'); 14 | private static List char3 = Arrays.asList('l', '`', '³', '\''); 15 | private static List char2 = Arrays.asList(',', '.', '!', 'i', '´', ':', ';', '|'); 16 | private static char char1 = '\u17f2'; 17 | private static Pattern regex = Pattern.compile(char1+"(?:§r)?(\\s*)" 18 | + "(?:§r§8)?"+char1+"(?:§r)?(\\s*)" 19 | + "(?:§r§8)?"+char1+"(?:§r)?(\\s*)" 20 | + "(?:§r§8)?"+char1); 21 | private static String colors = "[&§][0-9a-fA-Fk-oK-OrR]"; 22 | private Alignment[] alignments; 23 | private List table = new ArrayList<>(); 24 | private int columns; 25 | 26 | public TableGenerator(Alignment... alignments) { 27 | if (alignments == null || alignments.length < 1) 28 | throw new IllegalArgumentException("Must atleast provide 1 alignment."); 29 | 30 | this.columns = alignments.length; 31 | this.alignments = alignments; 32 | } 33 | 34 | public List generate(Receiver receiver, boolean ignoreColors, 35 | boolean coloredDistances) { 36 | if (receiver == null) { 37 | throw new IllegalArgumentException("Receiver must not be null."); 38 | } 39 | 40 | Integer[] columWidths = new Integer[columns]; 41 | 42 | for (Row r : table) { 43 | for (int i = 0; i < columns; i++) { 44 | String text = r.texts.get(i); 45 | int length; 46 | 47 | if (ignoreColors) 48 | length = getCustomLength(text.replaceAll(colors, ""), 49 | receiver); 50 | else length = getCustomLength(text, receiver); 51 | 52 | if (columWidths[i] == null) { 53 | columWidths[i] = length; 54 | } 55 | 56 | else if (length > columWidths[i]) { 57 | columWidths[i] = length; 58 | } 59 | } 60 | } 61 | 62 | List lines = new ArrayList(); 63 | 64 | for (Row r : table) { 65 | StringBuilder sb = new StringBuilder(); 66 | 67 | if (r.empty) { 68 | lines.add(""); 69 | continue; 70 | } 71 | 72 | for (int i = 0; i < columns; i++) { 73 | Alignment agn = alignments[i]; 74 | String text = r.texts.get(i); 75 | int length; 76 | 77 | if (ignoreColors) 78 | length = getCustomLength(text.replaceAll(colors, ""), 79 | receiver); 80 | else length = getCustomLength(text, 81 | receiver); 82 | 83 | int empty = columWidths[i] - length; 84 | int spacesAmount = empty; 85 | if (receiver == Receiver.CLIENT) 86 | spacesAmount = (int) Math.floor(empty / 4d); 87 | int char1Amount = 0; 88 | if (receiver == Receiver.CLIENT) 89 | char1Amount = empty - 4 * spacesAmount; 90 | 91 | String spaces = concatChars(' ', spacesAmount); 92 | String char1s = concatChars(char1, char1Amount); 93 | 94 | if (coloredDistances) 95 | char1s = "§r§8" + char1s + "§r"; 96 | 97 | if (agn == Alignment.LEFT) { 98 | sb.append(text); 99 | if (i < columns - 1) 100 | sb.append(char1s).append(spaces); 101 | } 102 | if (agn == Alignment.RIGHT) { 103 | sb.append(spaces).append(char1s).append(text); 104 | } 105 | if (agn == Alignment.CENTER) { 106 | int leftAmount = empty / 2; 107 | int rightAmount = empty - leftAmount; 108 | 109 | int spacesLeftAmount = leftAmount; 110 | int spacesRightAmount = rightAmount; 111 | if (receiver == Receiver.CLIENT) { 112 | spacesLeftAmount = (int) Math.floor(spacesLeftAmount / 4d); 113 | spacesRightAmount = (int) Math.floor(spacesRightAmount / 4d); 114 | } 115 | 116 | int char1LeftAmount = 0; 117 | int char1RightAmount = 0; 118 | if (receiver == Receiver.CLIENT) { 119 | char1LeftAmount = leftAmount - 4 * spacesLeftAmount; 120 | char1RightAmount = rightAmount - 4 * spacesRightAmount; 121 | } 122 | 123 | String spacesLeft = concatChars(' ', spacesLeftAmount); 124 | String spacesRight = concatChars(' ', spacesRightAmount); 125 | String char1Left = concatChars(char1, char1LeftAmount); 126 | String char1Right = concatChars(char1, char1RightAmount); 127 | 128 | if (coloredDistances) { 129 | char1Left = "§r§8" + char1Left + "§r"; 130 | char1Right = "§r§8" + char1Right + "§r"; 131 | } 132 | 133 | sb.append(spacesLeft).append(char1Left).append(text); 134 | if (i < columns - 1) 135 | sb.append(char1Right).append(spacesRight); 136 | } 137 | 138 | if (i < columns - 1) sb.append("§r" + delimiter); 139 | } 140 | 141 | String line = sb.toString(); 142 | if (receiver == Receiver.CLIENT) { 143 | for (int i = 0; i < 2; i++) { 144 | Matcher matcher = regex.matcher(line); 145 | line = matcher.replaceAll("$1$2$3 ").replace("§r§8§r", "§r") 146 | .replaceAll("§r(\\s*)§r", "§r$1"); 147 | } 148 | } 149 | lines.add(line); 150 | } 151 | return lines; 152 | } 153 | 154 | protected static int getCustomLength(String text, Receiver receiver) { 155 | if (text == null) { 156 | throw new IllegalArgumentException("Text must not be null."); 157 | } 158 | if (receiver == null) { 159 | throw new IllegalArgumentException("Receiver must not be null."); 160 | } 161 | if (receiver == Receiver.CONSOLE) return text.length(); 162 | 163 | int length = 0; 164 | for (char c : text.toCharArray()) 165 | length += getCustomCharLength(c); 166 | 167 | return length; 168 | } 169 | 170 | protected static int getCustomCharLength(char c) { 171 | if (char1 == c) return 1; 172 | if (char2.contains(c)) return 2; 173 | if (char3.contains(c)) return 3; 174 | if (char4.contains(c)) return 4; 175 | if (char5.contains(c)) return 5; 176 | if (char7.contains(c)) return 7; 177 | 178 | return 6; 179 | } 180 | 181 | protected String concatChars(char c, int length) { 182 | String s = ""; 183 | if (length < 1) return s; 184 | 185 | for (int i = 0; i < length; i++) 186 | s += Character.toString(c); 187 | return s; 188 | } 189 | 190 | public void addRow(String... texts) { 191 | if (texts == null) { 192 | throw new IllegalArgumentException("Texts must not be null."); 193 | } 194 | if (texts != null && texts.length > columns) { 195 | throw new IllegalArgumentException("Too big for the table."); 196 | } 197 | 198 | Row r = new Row(texts); 199 | 200 | table.add(r); 201 | } 202 | 203 | private class Row { 204 | 205 | public List texts = new ArrayList<>(); 206 | public boolean empty = true; 207 | 208 | public Row(String... texts) { 209 | if (texts == null) { 210 | for (int i = 0; i < columns; i++) 211 | this.texts.add(""); 212 | return; 213 | } 214 | 215 | for (String text : texts) { 216 | if (text != null && !text.isEmpty()) empty = false; 217 | 218 | this.texts.add(text); 219 | } 220 | 221 | for (int i = 0; i < columns; i++) { 222 | if (i >= texts.length) this.texts.add(""); 223 | } 224 | } 225 | } 226 | 227 | public enum Receiver { 228 | 229 | CONSOLE, CLIENT 230 | } 231 | 232 | public enum Alignment { 233 | 234 | CENTER, LEFT, RIGHT 235 | } 236 | } 237 | --------------------------------------------------------------------------------