|
| 1 | +#!/usr/bin/env php |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * Table Alignment Examples |
| 5 | + * |
| 6 | + * This example demonstrates the table column alignment feature. |
| 7 | + * You can align columns to the left, right, or center. |
| 8 | + */ |
| 9 | + |
| 10 | +if (file_exists(__DIR__ . '/../vendor/autoload.php')) { |
| 11 | + require_once __DIR__ . '/../vendor/autoload.php'; |
| 12 | +} elseif (file_exists(__DIR__ . '/../../../autoload.php')) { |
| 13 | + require_once __DIR__ . '/../../../autoload.php'; |
| 14 | +} else { |
| 15 | + throw new Exception('Unable to locate autoloader; please run "composer install"'); |
| 16 | +} |
| 17 | + |
| 18 | +cli\line(); |
| 19 | +cli\line('%G===%n %CTable Column Alignment Examples%n %G===%n'); |
| 20 | +cli\line(); |
| 21 | + |
| 22 | +// Example 1: Default (Left) Alignment |
| 23 | +cli\line('%Y## Example 1: Default Left Alignment%n'); |
| 24 | +cli\line(); |
| 25 | +$table = new cli\Table(); |
| 26 | +$table->setHeaders(['Product', 'Price', 'Stock']); |
| 27 | +$table->addRow(['Widget', '$19.99', '150']); |
| 28 | +$table->addRow(['Gadget', '$29.99', '75']); |
| 29 | +$table->addRow(['Tool', '$9.99', '200']); |
| 30 | +$table->display(); |
| 31 | +cli\line(); |
| 32 | + |
| 33 | +// Example 2: Right Alignment for Numeric Columns |
| 34 | +cli\line('%Y## Example 2: Right Alignment for Numeric Columns%n'); |
| 35 | +cli\line('Notice how the numeric values are much easier to compare when right-aligned.'); |
| 36 | +cli\line(); |
| 37 | +$table = new cli\Table(); |
| 38 | +$table->setHeaders(['Product', 'Price', 'Stock']); |
| 39 | +$table->setAlignments([ |
| 40 | + 'Product' => cli\table\Column::ALIGN_LEFT, |
| 41 | + 'Price' => cli\table\Column::ALIGN_RIGHT, |
| 42 | + 'Stock' => cli\table\Column::ALIGN_RIGHT, |
| 43 | +]); |
| 44 | +$table->addRow(['Widget', '$19.99', '150']); |
| 45 | +$table->addRow(['Gadget', '$29.99', '75']); |
| 46 | +$table->addRow(['Tool', '$9.99', '200']); |
| 47 | +$table->display(); |
| 48 | +cli\line(); |
| 49 | + |
| 50 | +// Example 3: Center Alignment |
| 51 | +cli\line('%Y## Example 3: Center Alignment%n'); |
| 52 | +cli\line(); |
| 53 | +$table = new cli\Table(); |
| 54 | +$table->setHeaders(['Left', 'Center', 'Right']); |
| 55 | +$table->setAlignments([ |
| 56 | + 'Left' => cli\table\Column::ALIGN_LEFT, |
| 57 | + 'Center' => cli\table\Column::ALIGN_CENTER, |
| 58 | + 'Right' => cli\table\Column::ALIGN_RIGHT, |
| 59 | +]); |
| 60 | +$table->addRow(['Text', 'Centered', 'More']); |
| 61 | +$table->addRow(['Data', 'Values', 'Here']); |
| 62 | +$table->addRow(['A', 'B', 'C']); |
| 63 | +$table->display(); |
| 64 | +cli\line(); |
| 65 | + |
| 66 | +// Example 4: Real-world Database Table Sizes |
| 67 | +cli\line('%Y## Example 4: Database Table Sizes (Real-world Use Case)%n'); |
| 68 | +cli\line('This example shows how the alignment feature makes database'); |
| 69 | +cli\line('statistics much more readable and easier to compare.'); |
| 70 | +cli\line(); |
| 71 | +$table = new cli\Table(); |
| 72 | +$table->setHeaders(['Table Name', 'Rows', 'Data Size', 'Index Size']); |
| 73 | +$table->setAlignments([ |
| 74 | + 'Table Name' => cli\table\Column::ALIGN_LEFT, |
| 75 | + 'Rows' => cli\table\Column::ALIGN_RIGHT, |
| 76 | + 'Data Size' => cli\table\Column::ALIGN_RIGHT, |
| 77 | + 'Index Size' => cli\table\Column::ALIGN_RIGHT, |
| 78 | +]); |
| 79 | +$table->addRow(['wp_posts', '1,234', '5.2 MB', '1.1 MB']); |
| 80 | +$table->addRow(['wp_postmeta', '45,678', '23.4 MB', '8.7 MB']); |
| 81 | +$table->addRow(['wp_comments', '9,012', '2.3 MB', '0.8 MB']); |
| 82 | +$table->addRow(['wp_options', '456', '1.5 MB', '0.2 MB']); |
| 83 | +$table->addRow(['wp_users', '89', '0.1 MB', '0.05 MB']); |
| 84 | +$table->display(); |
| 85 | +cli\line(); |
| 86 | + |
| 87 | +// Example 5: Alignment Constants |
| 88 | +cli\line('%Y## Alignment Constants%n'); |
| 89 | +cli\line(); |
| 90 | +cli\line('You can use the following constants from %Ccli\table\Column%n:'); |
| 91 | +cli\line(' %G*%n %CALIGN_LEFT%n - Left align (default)'); |
| 92 | +cli\line(' %G*%n %CALIGN_RIGHT%n - Right align (good for numbers)'); |
| 93 | +cli\line(' %G*%n %CALIGN_CENTER%n - Center align'); |
| 94 | +cli\line(); |
| 95 | +cli\line('Example usage:'); |
| 96 | +cli\line(' %c$table->setAlignments([%n'); |
| 97 | +cli\line(' %c\'Column1\' => cli\table\Column::ALIGN_LEFT,%n'); |
| 98 | +cli\line(' %c\'Column2\' => cli\table\Column::ALIGN_RIGHT,%n'); |
| 99 | +cli\line(' %c]);%n'); |
| 100 | +cli\line(); |
| 101 | + |
| 102 | +cli\line('%GDone!%n'); |
| 103 | +cli\line(); |
0 commit comments