Class AsciiTable

java.lang.Object
it.polimi.ingsw.gc14.View.TUI.AsciiTable

public class AsciiTable extends Object
General-purpose helper for building fixed-width ASCII/Unicode tables in a TUI.

A table is built by adding rows one at a time, then calling build() to obtain the fully rendered string. Column width is computed automatically from the widest cell across all rows.

Cell widths are measured in terminal columns, not Java char units: wide characters (emoji, CJK) count as 2 columns each.

Example usage:

AsciiTable table = new AsciiTable(BorderStyle.UNICODE, 3);
table.addHeader("Name", "🍖", "⭐");
table.addRow("Alice", "4", "12");
table.addRow("Bob",   "2", "8");
System.out.println(table.build());
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final int
    Number of columns in this table.
    private final List<List<String>>
    All rows of the table, in insertion order.
    private final BorderStyle
    Border style used to draw corners, lines, and junctions.
    private final List<Integer>
    Indices of rows after which a horizontal separator line is drawn.
  • Constructor Summary

    Constructors
    Constructor
    Description
    AsciiTable(BorderStyle s, int cols)
    Creates a new empty table with the given border style and column count.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addHeader(String... cells)
    Inserts a header row at position 0 and marks it with a separator line below it.
    void
    addRow(String... cells)
    Appends a row using varargs cells.
    void
    addRow(List<String> cells)
    Appends a row from an existing list.
    void
    Marks a separator line to be drawn after the last row added so far.
    Renders the table to a multi-line string.
    static int
    Returns the number of terminal columns required to display s.
    private String
    hline(String l, String m, String r, int maxWidth)
    Builds a single horizontal borderline across all columns.
    private static boolean
    isWide(int cp)
    Returns true when cp is a wide (2-column) character.
    private static String
    rpad(String s, int w)
    Pads s with trailing spaces so its display width equals w.
    static String
    sideBySide(List<String> left, List<String> right, int gap)
    Places two pre-rendered text blocks side by side, separated by a gap.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • s

      private final BorderStyle s
      Border style used to draw corners, lines, and junctions.
    • cols

      private final int cols
      Number of columns in this table.
    • rows

      private final List<List<String>> rows
      All rows of the table, in insertion order.
    • separators

      private final List<Integer> separators
      Indices of rows after which a horizontal separator line is drawn.
  • Constructor Details

    • AsciiTable

      public AsciiTable(BorderStyle s, int cols)
      Creates a new empty table with the given border style and column count.
      Parameters:
      s - the border style to use when rendering.
      cols - the number of columns.
  • Method Details

    • addRow

      public void addRow(String... cells)
      Appends a row using varargs cells.
      Parameters:
      cells - one value per column.
    • addRow

      public void addRow(List<String> cells)
      Appends a row from an existing list.
      Parameters:
      cells - one value per column.
    • addHeader

      public void addHeader(String... cells)
      Inserts a header row at position 0 and marks it with a separator line below it.
      Parameters:
      cells - one header label per column.
    • addSeparator

      public void addSeparator()
      Marks a separator line to be drawn after the last row added so far.
    • build

      public String build()
      Renders the table to a multi-line string. Column width is the widest cell in display columns (wide chars = 2), plus 1.
      Returns:
      the fully rendered table as a multi-line string.
    • hline

      private String hline(String l, String m, String r, int maxWidth)
      Builds a single horizontal borderline across all columns.
      Parameters:
      l - left-end character.
      m - middle junction character (between columns).
      r - right-end character.
      maxWidth - width in display columns of each cell (including padding).
      Returns:
      the rendered horizontal line string.
    • rpad

      private static String rpad(String s, int w)
      Pads s with trailing spaces so its display width equals w. If the string is already at or over w columns, it is returned as-is.
    • sideBySide

      public static String sideBySide(List<String> left, List<String> right, int gap)
      Places two pre-rendered text blocks side by side, separated by a gap. Left-block lines are padded to a uniform display width so the right block always starts at the same column. Uses displayWidth(String) for measurement.
      Parameters:
      left - lines of the left block.
      right - lines of the right block.
      gap - number of space characters between the two blocks.
      Returns:
      the combined multi-line string.
    • displayWidth

      public static int displayWidth(String s)
      Returns the number of terminal columns required to display s. Wide characters (emoji, CJK, full-width) count as 2; all others as 1.
      Parameters:
      s - the string to measure; ANSI escape sequences are stripped before counting.
      Returns:
      the display width in terminal columns.
    • isWide

      private static boolean isWide(int cp)
      Returns true when cp is a wide (2-column) character. Covers CJK blocks, Hangul, full-width forms, and emoji (including the specific emoji used in this project: 🍖 U+1F357, ⭐ U+2B50).