TableUtils.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package me.km.utils;
  2. public class TableUtils
  3. {
  4. public static String getTable(int width, String color, String... args)
  5. {
  6. //int leftSpace = (9 * width - 6 - 10 * args.length) % args.length;
  7. width = 9 * width - 1;
  8. StringBuilder sb = new StringBuilder(color + "│ ");
  9. //int bonus;
  10. for(String s : args)
  11. {
  12. //bonus = leftSpace > 0 ? 9 : 0;
  13. sb.append("§r");
  14. sb.append(getStringWithMaxLength(s, width));
  15. //leftSpace -= 9;
  16. sb.append(color);
  17. sb.append("│ ");
  18. }
  19. sb.deleteCharAt(sb.length() - 1);
  20. return sb.toString();
  21. }
  22. public static String getTableMiddle(int width, int columns, String color)
  23. {
  24. StringBuilder sb = new StringBuilder(color + "├");
  25. for(int i = 0; i < columns; i++)
  26. {
  27. for(int j = 0; j < width; j++)
  28. {
  29. sb.append("─");
  30. }
  31. sb.append("┼");
  32. }
  33. sb.deleteCharAt(sb.length() - 1);
  34. sb.append("┤");
  35. return sb.toString();
  36. }
  37. public static String getTableStart(int width, int columns, String color)
  38. {
  39. StringBuilder sb = new StringBuilder(color + "┌");
  40. for(int i = 0; i < columns; i++)
  41. {
  42. for(int j = 0; j < width; j++)
  43. {
  44. sb.append("─");
  45. }
  46. sb.append("┬");
  47. }
  48. sb.deleteCharAt(sb.length() - 1);
  49. sb.append("┐");
  50. return sb.toString();
  51. }
  52. public static String getTableEnd(int width, int columns, String color)
  53. {
  54. StringBuilder sb = new StringBuilder(color + "└");
  55. for(int i = 0; i < columns; i++)
  56. {
  57. for(int j = 0; j < width; j++)
  58. {
  59. sb.append("─");
  60. }
  61. sb.append("┴");
  62. }
  63. sb.deleteCharAt(sb.length() - 1);
  64. sb.append("┘");
  65. return sb.toString();
  66. }
  67. private static String getStringWithMaxLength(String s, int max)
  68. {
  69. // 9 Pixel: └,─,┬
  70. // 8 Pixel:
  71. // 7 Pixel:
  72. // 6 Pixel: normal, │
  73. // 5 Pixel: f, k
  74. // 4 Pixel: I, t
  75. // 3 Pixel: l
  76. // 2 Pixel: is
  77. int sum = 0;
  78. String r = "";
  79. int should;
  80. boolean onColor = false;
  81. for(char c : s.toCharArray())
  82. {
  83. switch(c)
  84. {
  85. case 'f':
  86. case 'k':
  87. should = 5;
  88. break;
  89. case 'I':
  90. case 't':
  91. case ' ':
  92. should = 4;
  93. break;
  94. case 'l':
  95. should = 3;
  96. break;
  97. case 'i':
  98. should = 2;
  99. break;
  100. case '§':
  101. should = 0;
  102. onColor = true;
  103. break;
  104. default:
  105. should = 6;
  106. }
  107. if(!onColor || should == 0)
  108. {
  109. sum += should;
  110. }
  111. else
  112. {
  113. onColor = false;
  114. }
  115. //System.out.println(sum + " " + max);
  116. if(sum > max)
  117. {
  118. sum -= should;
  119. r += "§0";
  120. while(sum < max)
  121. {
  122. if(max - sum == 1)
  123. {
  124. r += "ˈ";
  125. break;
  126. }
  127. if(max - sum >= 4)
  128. {
  129. r += " ";
  130. sum += 4;
  131. continue;
  132. }
  133. r += "˼";
  134. sum += 2;
  135. }
  136. return r;
  137. }
  138. else if(sum == max)
  139. {
  140. r += c;
  141. return r;
  142. }
  143. r += c;
  144. }
  145. r += "§0";
  146. while(sum < max)
  147. {
  148. if(max - sum == 1)
  149. {
  150. r += "ˈ";
  151. break;
  152. }
  153. if(max - sum >= 4)
  154. {
  155. r += " ";
  156. sum += 4;
  157. continue;
  158. }
  159. r += "˼";
  160. sum += 2;
  161. }
  162. return r;
  163. }
  164. }