Class AsciiTable
java.lang.Object
it.polimi.ingsw.gc14.View.TUI.AsciiTable
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
FieldsModifier and TypeFieldDescriptionprivate final intNumber of columns in this table.All rows of the table, in insertion order.private final BorderStyleBorder style used to draw corners, lines, and junctions.Indices of rows after which a horizontal separator line is drawn. -
Constructor Summary
ConstructorsConstructorDescriptionAsciiTable(BorderStyle s, int cols) Creates a new empty table with the given border style and column count. -
Method Summary
Modifier and TypeMethodDescriptionvoidInserts a header row at position 0 and marks it with a separator line below it.voidAppends a row using varargs cells.voidAppends a row from an existing list.voidMarks a separator line to be drawn after the last row added so far.build()Renders the table to a multi-line string.static intReturns the number of terminal columns required to displays.private StringBuilds a single horizontal borderline across all columns.private static booleanisWide(int cp) Returnstruewhencpis a wide (2-column) character.private static StringPadsswith trailing spaces so its display width equalsw.static StringsideBySide(List<String> left, List<String> right, int gap) Places two pre-rendered text blocks side by side, separated by a gap.
-
Field Details
-
s
Border style used to draw corners, lines, and junctions. -
cols
private final int colsNumber of columns in this table. -
rows
-
separators
-
-
Constructor Details
-
AsciiTable
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
Appends a row using varargs cells.- Parameters:
cells- one value per column.
-
addRow
-
addHeader
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
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
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
-
sideBySide
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. UsesdisplayWidth(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
Returns the number of terminal columns required to displays. 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) Returnstruewhencpis 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).
-