Macrame allows for the creation of ascii-style tables, similar to the output from the mysql client, from data arrays.

Warning concerning emojis
Due to changes in PHP 8.1's multibyte string extension, emojis may not be aligned properly in table output when using older versions of PHP.

Quickref

$headers = [
    'Artist',
    'Album',
];

$data = [
    ['The Velvet Underground', 'And Nico' ],
    ['Monk, Thelonious', 'Misterioso' ],
];

$macrame = new Macrame();

$macrame->table($headers, $data)->write();              // void. Output table to screen
$table = $macrame->table($headers, $data)->get();       // string. Get table as string

// column indexes start at 0
$macrame->table($headers, $data)->centre(1)->write();   // void. Centre align column 1
$macrame->table($headers, $data)->center(1)->write();   // void. Alias of centre()
$macrame->table($headers, $data)->right(1)->write();    // void. Right align column 1
$macrame->table($headers, $data)->left(0)->write();     // void. Left align column 1

// style tables
$macrame->table($headers, $data)->solid()->write();     // void. Solid border
$macrame->table($headers, $data)->double()->write();    // void. Double-lined border
$macrame->table($headers, $data)->standard()->write();  // void. standard border

Creating a table

Tables are created as an object of class MacrameTable.

The easiest way to create a table object is with the table() method. The table() method takes two arguments:

  • $headers: An array of strings representing the column headers
  • $data: An array of arrays of strings representing the column data
$headers = [...];
$data = [...];

$myTable = $macrame->table($headers, $data);

Output

The table object has two output options:

  • get(): Get the table as a string
  • write(): Write the table directly to screen
$headers = [...];
$data = [...];

$macrame->table($headers, $data)->write();                 // void. Write table to screen
$myTableString = $macrame->table($headers, $data)->get();  // string. Get table as string

$myTable = $macrame->table($headers, $data);               // MacrameTable. Get table object
$myTable->write();

Validating input

Macrame tables demand that the number of columns in the $headers must match the number of columns in each of the arrays in $data.

In the event of a column mismatch, Macrame will output a warning to screen and will return null in place of the table.

$headers = [
    'Artist',
    'Album',
];

$data = [
    ['The Velvet Underground', 'And Nico', '1967' ], // too many columns
    ['Monk, Thelonious'],                            // too few columns
];

// outputs a warning
$myTableString = $macrame->table($headers, $data)->get();

is_null($myTableString); // true

Aligning columns

Columns can be aligned by passing the numerical column index to one of the left(), right() or centre() alignment methods. Columns are indexed starting at zero.

$headers = [
    'Artist',
    'Album',
];

$data = [
    ['The Velvet Underground', 'And Nico' ],
    ['Monk, Thelonious', 'Misterioso' ],
];

// Align centre the 'Artist' column
$macrame->table($headers, $data)->centre(0)->write();
$macrame->table($headers, $data)->center(0)->write(); // American spelling works, too.

// Align right the 'Album' column
$macrame->table($headers, $data)->right(1)->write();

// Align left 'Artist' and align right 'Album'
$macrame->table($headers, $data)->left(0)->right(1)->write();

// Alternate method
$myTable = $macrame->table($headers, $data);
$myTable->right(0);
$myTable->center(1);
$myTable->write();
πŸ‡ΊπŸ‡Έ Note
center() is provided as an alias for centre()

The default alignment is left.

If multiple different alignments are applied to a column, the last alignment applied will be used

Alignment example

Script

$headers = [
    'Artist',
    'Album',
];

$data = [
    ['The Velvet Underground', 'And Nico' ],
    ['Monk, Thelonious', 'Misterioso' ],
];

// Align centre 'Artist' and align right 'Album'
$macrame->table($headers, $data)->centre(0)->right(1)->write();

Output

+------------------------+------------+
|         Artist         |      Album | 
+------------------------+------------+
| The Velvet Underground |   And Nico | 
|    Monk, Thelonious    | Misterioso | 
+------------------------+------------+

Applying styles

Note
The example here uses Macrames text() feature for styling and outputting text.
Styles can be applied to tables by using strings created by MacrameText.
$headers = [
    $macrame->text('Artist')->style('bold')->get(), // bold text
    $macrame->text('Album')->color('red')->get(),   // red text
];

$data = [
    ['The Velvet Underground', 'And Nico' ],
    ['Monk, Thelonious', 'Misterioso' ],
];

$macrame->table($headers, $data)->write();

The output of the above code will look like:

+------------------------+------------+
| Artist                 | Album      |
+------------------------+------------+
| The Velvet Underground | And Nico   |
| Monk, Thelonious       | Misterioso |
+------------------------+------------+

Styles can be applied to strings used in both the $headers and $data arrays.

Setting border styles

Three different border styles can be applied to tables:

  • standard(): A MySql-style dashed border using ASCII characters. This is the default
  • solid(): A solid-line border
  • double(): A solid-line border with double lines
$macrame->table($headers, $data)->solid()->write();
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Artist                 β”‚ Album      β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ The Velvet Underground β”‚ And Nico   β”‚
β”‚ Monk, Thelonious       β”‚ Misterioso β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
$macrame->table($headers, $data)->double()->write();
╔════════════════════════╦════════════╗
β•‘ Artist                 β•‘ Album      β•‘
╠════════════════════════╬════════════╣
β•‘ The Velvet Underground β•‘ And Nico   β•‘
β•‘ Monk, Thelonious       β•‘ Misterioso β•‘
β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•©β•β•β•β•β•β•β•β•β•β•β•β•β•
$macrame->table($headers, $data)->standard()->write();
// or
$macrame->table($headers, $data)->write();
+------------------------+------------+
| Artist                 | Album      |
+------------------------+------------+
| The Velvet Underground | And Nico   |
| Monk, Thelonious       | Misterioso |
+------------------------+------------+