Advertisement
bobmarley12345

Java string Text Grid

Dec 11th, 2022
652
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.32 KB | None | 0 0
  1. package reghzy.api.utils.text;
  2.  
  3. import gnu.trove.list.array.TIntArrayList;
  4. import reghzy.api.linq.Linq;
  5. import reghzy.api.utils.Maths;
  6.  
  7. import javax.annotation.Nonnull;
  8. import java.util.ArrayList;
  9.  
  10. public class TextGrid {
  11.     // Rows<Columns<contents>>
  12.     // Outer list contains the rows. These contain the "column" parts inside
  13.     private final ArrayList<ArrayList<String>> rows;
  14.  
  15.     private boolean centerTopLeft = true;
  16.     private boolean centerContents = true;
  17.  
  18.     private char fillChar = ' ';
  19.     private String emptyCellString = "";
  20.  
  21.     public TextGrid() {
  22.         this.rows = new ArrayList<ArrayList<String>>();
  23.     }
  24.  
  25.     public boolean centerTopLeft() {
  26.         return this.centerTopLeft;
  27.     }
  28.  
  29.     public TextGrid centerTopLeft(boolean centerTopLeft) {
  30.         this.centerTopLeft = centerTopLeft;
  31.         return this;
  32.     }
  33.  
  34.     public boolean centerContents() {
  35.         return this.centerContents;
  36.     }
  37.  
  38.     public TextGrid centerContents(boolean center) {
  39.         this.centerContents = center;
  40.         return this;
  41.     }
  42.  
  43.     public char getFillChar() {
  44.         return this.fillChar;
  45.     }
  46.  
  47.     public TextGrid setFillChar(char fillChar) {
  48.         this.fillChar = fillChar;
  49.         return this;
  50.     }
  51.  
  52.     public String getEmptyCellString() {
  53.         return this.emptyCellString;
  54.     }
  55.  
  56.     public TextGrid setEmptyCellString(@Nonnull String emptyCellString) {
  57.         this.emptyCellString = StringUtils.nullToEmpty(emptyCellString);
  58.         return this;
  59.     }
  60.  
  61.     public TextGrid line(String... columns) {
  62.         ArrayList<String> list = nextList();
  63.         list.addAll(Linq.of(columns).map(StringUtils::nullToEmpty).toList());
  64.         return this;
  65.     }
  66.  
  67.     private int getTotalColumnCount() {
  68.         return Linq.wrap(this.rows).aggregate(-1, (a, b) -> Math.max(a.size(), b));
  69.     }
  70.  
  71.     private ArrayList<String> nextList() {
  72.         ArrayList<String> list = new ArrayList<String>();
  73.         this.rows.add(list);
  74.         return list;
  75.     }
  76.  
  77.     public String format(String rowSeparator, String columnSeparator) {
  78.         int columns = getTotalColumnCount();
  79.         if (columns == -1) {
  80.             return StringUtils.repeat('\n', this.rows.size());
  81.         }
  82.  
  83.         StringBuilder output = new StringBuilder();
  84.  
  85.         int[] column_sizes = getColumnSizeArray(columns);
  86.  
  87.         for (int i = 0, rowEnd = this.rows.size() - 1; i <= rowEnd; i++) {
  88.             StringBuilder row = formatRow(null, null, columnSeparator, columns, column_sizes, i);
  89.             output.append(row);
  90.             if (i != rowEnd) {
  91.                 output.append(rowSeparator);
  92.             }
  93.         }
  94.  
  95.         return output.toString();
  96.     }
  97.  
  98.     private int[] getColumnSizeArray(int columns) {
  99.         int[] column_sizes = new int[columns];
  100.         for (int i = 0; i < this.rows.size(); i++) {
  101.             ArrayList<String> row = this.rows.get(i);
  102.             for (int j = 0; j < columns; j++) {
  103.                 column_sizes[j] = Math.max(column_sizes[j], getCellAt(row, j).length());
  104.             }
  105.         }
  106.  
  107.         return column_sizes;
  108.     }
  109.  
  110.     private StringBuilder formatRow(String prefix, String postfix, String cellSeparator, int columns, int[] column_sizes, int rowIndex) {
  111.         StringBuilder sb = new StringBuilder();
  112.         if (prefix != null) {
  113.             sb.append(prefix);
  114.         }
  115.  
  116.         boolean centered = this.centerContents || (rowIndex == 0 && this.centerTopLeft);
  117.  
  118.         ArrayList<String> row = this.rows.get(rowIndex);
  119.         for (int j = 0, colEnd = columns - 1; j <= colEnd; j++) {
  120.             // append padding between the gaps
  121.             // if (j > 0) sb.append(' ');
  122.  
  123.             // allow fine control over centered parts
  124.             boolean center = (rowIndex == 0 || j == 0) ? this.centerTopLeft : this.centerContents;
  125.  
  126.             String cell = getCellAt(row, j);
  127.             int cellWidth = column_sizes[j];
  128.             int emptySpace = cellWidth - cell.length();
  129.  
  130.             if (center && StringUtils.isNotEmpty(cell)) {
  131.                 double half = (double) emptySpace / 2d;
  132.                 StringUtils.repeatInto(sb, this.fillChar, Maths.ifastfloor(half));
  133.                 sb.append(cell);
  134.                 StringUtils.repeatInto(sb, this.fillChar, Maths.ifastceil(half));
  135.             }
  136.             else {
  137.                 sb.append(cell);
  138.                 if (emptySpace > 0) {
  139.                     StringUtils.repeatInto(sb, this.fillChar, emptySpace);
  140.                 }
  141.             }
  142.  
  143.             // append padding between the gaps
  144.             // if (j != colEnd) sb.append(' ');
  145.  
  146.             if (j != colEnd) {
  147.                 // don't append separator to last column
  148.                 sb.append(cellSeparator);
  149.             }
  150.         }
  151.  
  152.         if (postfix != null) {
  153.             sb.append(postfix);
  154.         }
  155.  
  156.         return sb;
  157.     }
  158.  
  159.     private String getCellAt(int row, int col) {
  160.         if (col < 0 || row < 0 || row >= this.rows.size()) {
  161.             return null;
  162.         }
  163.  
  164.         return getCellAt(this.rows.get(col), col);
  165.     }
  166.  
  167.     private String getCellAt(ArrayList<String> row, int column) {
  168.         return (column < 0 || column >= row.size()) ? this.emptyCellString : row.get(column);
  169.     }
  170.  
  171.     private static void set(TIntArrayList list, int index, int value) {
  172.         list.set(index, value);
  173.     }
  174. }
  175.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement