Libraries / table
Overview
Pretty ASCII & Unicode terminal table renderer for HolyPHP with headers, column alignment (left, right, center), border styles (ascii, unicode, markdown, compact), and automatic column sizing.
bundled — works offline
terminal
ascii
unicode
Quick start
import "table";
$out = table_render(
["Name", "Role", "Score"],
[
["Alice", "Admin", 98],
["Bob", "User", 7],
["Charlie", "Moderator", 142]
],
"unicode",
["left", "center", "right"]
);
println($out);
Output:
┌─────────┬───────────┬───────┐
│ Name │ Role │ Score │
├─────────┼───────────┼───────┤
│ Alice │ Admin │ 98 │
│ Bob │ User │ 7 │
│ Charlie │ Moderator │ 142 │
└─────────┴───────────┴───────┘
Using the fluent Table class:
import "table";
$t = new Table(["Item", "Qty", "Price"]);
$t->setStyle("ascii")
->setAlign(1, "right")
->setAlign(2, "right")
->addRow(["Apple", 3, "$1.50"])
->addRow(["Banana", 12, "$4.00"]);
println($t->render());
Installation
In a project
Inside a project (a folder with hphp.yaml, see Projects & packages ):
hphp install table
This adds the library to hphp.yaml, installs it into .hphp/packages/table/ and pins the exact version in hphp.lock:
require:
table: "^1.0"
Use it
import "table";
table is bundled with the compiler, so import "table"; also works in a single file without installing anything, and hphp install works offline.
Outside a project
hphp install table # for your user, shared by all scripts
hphp create myapp # or start a project, then: hphp install table
Documentation
API Reference
Border Styles
"ascii" — Classic +---+ and | borders (default)
"unicode" — Box-drawing ┌─┬┐, │, ├─┼┤, └─┴┘ borders
"markdown" — GitHub-flavored Markdown pipe tables with alignment markers (---, :---:, ---:)
"compact" — Minimal borderless columns separated by spaces with a dashed header underline
Column Alignments
"left" (default)
"right"
"center"
Table Class
new Table(array $headers = [], array $rows = [], string $style = "ascii") — Create a new table instance.
setHeaders(array $headers) — Set column headers (returns $this).
addRow(array $row) — Append a single row (returns $this).
addRows(array $rows) — Append multiple rows (returns $this).
setStyle(string $style) — Set border style (ascii, unicode, markdown, compact) (returns $this).
setAlign(int $col, string $align) — Set alignment for a 0-based column index (returns $this).
setAlignments(array $alignments) — Set alignments for columns starting at index 0 (returns $this).
setPadding(int $padding) — Set horizontal cell padding in spaces (default 1, must be >= 0) (returns $this).
render(): string — Render the table to a multi-line string. Returns "" if the table has no columns.
Helper Functions
table_render(array $headers, array $rows, string $style = "ascii", array $alignments = []): string — One-shot helper to render a table.
table_create(array $headers = [], array $rows = [], string $style = "ascii"): Table — Factory function returning a Table instance.
Error Handling
All invalid configurations throw an Exception with a clear table: ... message:
try {
table_render(["A"], [["1"]], "invalid");
} catch (Exception $e) {
// "table: invalid border style 'invalid' (expected one of: ascii, unicode, markdown, compact)"
println($e->getMessage());
}
try {
$t = new Table(["A"]);
$t->setAlign(0, "justify");
} catch (Exception $e) {
// "table: invalid alignment 'justify' (expected one of: left, right, center)"
println($e->getMessage());
}
Examples
01 Quick start
Bundled with the compiler — import "table"; works offline in any file, or add it to a project with hphp install table.
import "table";
$out = table_render(
["Name", "Role", "Score"],
[
["Alice", "Admin", 98],
["Bob", "User", 7],
["Charlie", "Moderator", 142]
],
"unicode",
["left", "center", "right"]
);
println($out);
Output:
┌─────────┬───────────┬───────┐
│ Name │ Role │ Score │
├─────────┼───────────┼───────┤
│ Alice │ Admin │ 98 │
│ Bob │ User │ 7 │
│ Charlie │ Moderator │ 142 │
└─────────┴───────────┴───────┘
Using the fluent Table class:
import "table";
$t = new Table(["Item", "Qty", "Price"]);
$t->setStyle("ascii")
->setAlign(1, "right")
->setAlign(2, "right")
->addRow(["Apple", 3, "$1.50"])
->addRow(["Banana", 12, "$4.00"]);
println($t->render());
Try it hphp install table
hphp run main.hphp
Covered by the official test suite in tests/positive/lib_table.hphp.