table

ASCII & Unicode terminal table renderer with alignment and border styles.

v1.0.0Tooling & CLIbundledimport "table";
Overview Installation Documentation Examples

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
Packagetable
Version1.0.0 (follows the compiler)
Entry filetable.hphp
Sourcepackages/lib/table ↗

Documentation

API Reference

Border Styles

Column Alignments

Table Class

Helper Functions

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

01Quick 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.