{"id":23913026,"url":"https://github.com/chernegasergiy/table-magic","last_synced_at":"2026-02-13T08:06:16.125Z","repository":{"id":258662104,"uuid":"875358753","full_name":"ChernegaSergiy/table-magic","owner":"ChernegaSergiy","description":"A PHP library for creating and manipulating tables in a console output format.","archived":false,"fork":false,"pushed_at":"2025-01-04T14:26:12.000Z","size":85,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-25T10:51:08.899Z","etag":null,"topics":["ascii","ascii-tables","console-table-printer","php-library","php7","php8","prettytable"],"latest_commit_sha":null,"homepage":"https://packagist.org/packages/chernegasergiy/table-magic","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ChernegaSergiy.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-10-19T19:00:36.000Z","updated_at":"2025-01-04T14:25:05.000Z","dependencies_parsed_at":"2024-11-17T14:34:46.140Z","dependency_job_id":"77d2a58b-83b1-4a20-8b48-e5eabd4a9f10","html_url":"https://github.com/ChernegaSergiy/table-magic","commit_stats":null,"previous_names":["chernegasergiy/table-magic"],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ChernegaSergiy%2Ftable-magic","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ChernegaSergiy%2Ftable-magic/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ChernegaSergiy%2Ftable-magic/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ChernegaSergiy%2Ftable-magic/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ChernegaSergiy","download_url":"https://codeload.github.com/ChernegaSergiy/table-magic/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248420257,"owners_count":21100347,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ascii","ascii-tables","console-table-printer","php-library","php7","php8","prettytable"],"created_at":"2025-01-05T09:20:19.324Z","updated_at":"2026-02-13T08:06:16.109Z","avatar_url":"https://github.com/ChernegaSergiy.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# TableMagic\n\n[![Latest Stable Version](https://img.shields.io/packagist/v/chernegasergiy/table-magic.svg?label=Packagist\u0026logo=packagist)](https://packagist.org/packages/chernegasergiy/table-magic)\n[![Total Downloads](https://img.shields.io/packagist/dt/chernegasergiy/table-magic.svg?label=Downloads\u0026logo=packagist)](https://packagist.org/packages/chernegasergiy/table-magic)\n[![License](https://img.shields.io/packagist/l/chernegasergiy/table-magic.svg?label=Licence\u0026logo=open-source-initiative)](https://packagist.org/packages/chernegasergiy/table-magic)\n[![Tests](https://img.shields.io/github/actions/workflow/status/ChernegaSergiy/table-magic/phpunit.yml?label=Tests\u0026logo=github)](https://github.com/ChernegaSergiy/table-magic/actions/workflows/phpunit.yml)\n[![Test Coverage](https://img.shields.io/codecov/c/github/ChernegaSergiy/table-magic?label=Test%20Coverage\u0026logo=codecov)](https://app.codecov.io/gh/ChernegaSergiy/table-magic)\n[![Static Analysis](https://img.shields.io/github/actions/workflow/status/ChernegaSergiy/table-magic/phpstan.yml?label=PHPStan\u0026logo=github)](https://github.com/ChernegaSergiy/table-magic/actions/workflows/phpstan.yml)\n\n**TableMagic** is a powerful and flexible PHP library designed for creating and manipulating tables in console output. Inspired by Python's PrettyTable, TableMagic empowers developers to effortlessly display tabular data with customizable alignment, sorting, and styling options.\n\n## Features\n\n- **Easy Table Creation**: Quickly create tables with headers and rows.\n- **Dynamic Row Addition**: Add rows easily with support for automatic column width adjustment.\n- **Customizable Column Alignments**: Align columns to the left, right, or center.\n- **UTF-8 Support**: Properly display non-ASCII characters.\n- **Sorting Capability**: Sort tables by any column in ascending or descending order.\n- **Export and Import Options**: Import data from CSV, JSON, XML, and Markdown formats, and export to HTML, CSV, JSON, XML, and Markdown using a flexible, object-oriented approach with dedicated importer and exporter classes.\n- **Customizable Table Styles**: Apply various predefined styles or define your own for unique table appearances.\n- **Terminal Interaction**: Paginate through large tables in the console.\n\n## Repository Structure\n\nHere is the updated structure of the core `src/TableMagic` directory:\n\n```\nsrc/\n\\-- TableMagic/\n    +-- Table.php\n    +-- TableExporter.php\n    +-- TableImporter.php\n    +-- TableStyle.php\n    +-- TableStyleRegistry.php\n    +-- TerminalInteraction.php\n    +-- Interfaces/                # Defines contracts for importers and exporters\n    |   +-- TableImporterInterface.php\n    |   \\-- TableExporterInterface.php\n    +-- Importers/                 # Concrete implementations for importing various formats\n    |   +-- CsvTableImporter.php\n    |   +-- JsonTableImporter.php\n    |   +-- MarkdownTableImporter.php\n    |   \\-- XmlTableImporter.php\n    \\-- Exporters/                 # Concrete implementations for exporting to various formats\n        +-- CsvTableExporter.php\n        +-- HtmlTableExporter.php\n        +-- JsonTableExporter.php\n        +-- MarkdownTableExporter.php\n        \\-- XmlTableExporter.php\n```\n\n## Installation\n\nYou can install TableMagic using Composer:\n\n```bash\ncomposer require chernegasergiy/table-magic\n```\n\n## Usage\n\nHere's a quick example to get you started with TableMagic:\n\n```php\nrequire_once 'vendor/autoload.php';\n\nuse ChernegaSergiy\\TableMagic\\Table;\n\n// Define headers and alignments\n$headers = ['Employee ID', 'Name', 'Department', 'Performance Score', 'Review Date'];\n$alignments = [\n    'Employee ID' =\u003e 'r',\n    'Performance Score' =\u003e 'r',\n    'Review Date' =\u003e 'c',\n];\n\n// Create a new Table instance\n$table = new Table($headers, $alignments);\n\n// Add rows to the table\n$table-\u003eaddRow([1001, 'Alice Thompson', 'Marketing', 85, '2024-06-15']);\n$table-\u003eaddRow([1002, 'Brian Lee', 'Sales', 90, '2024-06-18']);\n$table-\u003eaddRow([1003, 'Carol Martinez', 'Engineering', 88, '2024-06-20']);\n\n// Display the table\necho $table;\n\n// Sort the table by 'Performance Score' (descending order)\n$table-\u003esortTable('Performance Score', 'desc');\necho \"\\n\\nSorted by Performance Score (Descending):\\n\";\necho $table;\n```\n\nThis will output:\n\n```\n+-------------+----------------+-------------+-------------------+-------------+\n| Employee ID |      Name      | Department  | Performance Score | Review Date |\n+-------------+----------------+-------------+-------------------+-------------+\n|        1001 | Alice Thompson | Marketing   |                85 | 2024-06-15  |\n|        1002 | Brian Lee      | Sales       |                90 | 2024-06-18  |\n|        1003 | Carol Martinez | Engineering |                88 | 2024-06-20  |\n+-------------+----------------+-------------+-------------------+-------------+\n\nSorted by Performance Score (Descending):\n+-------------+----------------+-------------+-------------------+-------------+\n| Employee ID |      Name      | Department  | Performance Score | Review Date |\n+-------------+----------------+-------------+-------------------+-------------+\n|        1002 | Brian Lee      | Sales       |                90 | 2024-06-18  |\n|        1003 | Carol Martinez | Engineering |                88 | 2024-06-20  |\n|        1001 | Alice Thompson | Marketing   |                85 | 2024-06-15  |\n+-------------+----------------+-------------+-------------------+-------------+\n```\n\n## Table Styling\n\nTableMagic offers extensive customization options for table appearance through its styling system. You can apply various predefined styles or define your own custom styles to match your application's aesthetic.\n\n### Predefined Styles\n\nTableMagic comes with several built-in styles, including: `default`, `compact`, `dots`, `rounded`, `unicode-single-line`, `unicode-double-line`, `github-markdown`, `reddit-markdown`, `restructured-text-grid`, and `restructured-text-simple`.\n\nHere's an example demonstrating some of these styles, along with a custom one:\n\n```php\nrequire_once 'vendor/autoload.php';\n\nuse ChernegaSergiy\\TableMagic\\Table;\nuse ChernegaSergiy\\TableMagic\\TableStyle;\nuse ChernegaSergiy\\TableMagic\\TableStyleRegistry;\n\n$table = new Table(\n    ['Product', 'Quantity', 'Price'],\n    [\n        'Quantity' =\u003e 'c', // Center align Quantity\n        'Price' =\u003e 'r'     // Right align Price\n    ]\n);\n$table-\u003eaddRow(['Laptop', 2, 1200.50]);\n$table-\u003eaddRow(['Mouse', 5, 25.00]);\n$table-\u003eaddRow(['Keyboard', 1, 75.99]);\n\necho \"Default Style:\\n\";\n$table-\u003esetStyle('default');\necho $table;\n\necho \"\\nCompact Style:\\n\";\n$table-\u003esetStyle('compact');\necho $table;\n\necho \"\\nUnicode Single Line Style:\\n\";\n$table-\u003esetStyle('unicode-single-line');\necho $table;\n\n// Define and apply a custom style\n$custom_style = new TableStyle(\n    '¦', // Vertical separator (light vertical bar)\n    ['╭', '─', '┬', '╮'], // Top border (rounded corners)\n    ['├', '─', '┼', '┤'], // Header separator (light lines)\n    ['¦', '┄', '┼', '¦'], // Row separator (dashed horizontal, light vertical)\n    ['╰', '─', '┴', '╯']  // Bottom border (rounded corners)\n);\nTableStyleRegistry::register('custom-fancy', $custom_style);\n\necho \"\\nCustom 'Fancy' Style:\\n\";\n$table-\u003esetStyle('custom-fancy');\necho $table;\n```\n\nThis will output:\n\n```\nDefault Style:\n+----------+----------+--------+\n| Product  | Quantity |  Price |\n+----------+----------+--------+\n| Laptop   |    2     | 1200.5 |\n| Mouse    |    5     |     25 |\n| Keyboard |    1     |  75.99 |\n+----------+----------+--------+\n\nCompact Style:\n  Product    Quantity    Price \n ---------- ---------- -------- \n  Laptop        2       1200.5 \n  Mouse         5           25 \n  Keyboard      1        75.99 \n\nUnicode Single Line Style:\n┌──────────┬──────────┬────────┐\n│ Product  │ Quantity │  Price │\n├──────────┼──────────┼────────┤\n│ Laptop   │    2     │ 1200.5 │\n│ Mouse    │    5     │     25 │\n│ Keyboard │    1     │  75.99 │\n└──────────┴──────────┴────────┘\n\nCustom 'Fancy' Style:\n╭──────────┬──────────┬────────╮\n¦ Product  ¦ Quantity ¦  Price ¦\n├──────────┼──────────┼────────┤\n¦ Laptop   ¦    2     │ 1200.5 ¦\n¦ Mouse    ¦    5     │     25 ¦\n¦ Keyboard │    1     │  75.99 ¦\n╰──────────┴──────────┴────────╯\n```\n\n### Importing Data\n\nData can be imported using the `TableImporter` factory, which delegates to specific importer classes based on the format:\n\n```php\nuse ChernegaSergiy\\TableMagic\\TableImporter;\n\n$importer = new TableImporter();\n$csv_data = \"Name,Age\\nAlice,30\\nBob,25\";\n$table_from_csv = $importer-\u003eimport($csv_data, 'csv');\n\n$json_data = '{\"headers\":[\"Name\",\"Age\"],\"rows\":[[\"Charlie\",30],[\"David\",25]]}';\n$table_from_json = $importer-\u003eimport($json_data, 'json');\n\n$xml_data = '\u003croot\u003e\u003cheaders\u003e\u003cheader\u003eName\u003c/header\u003e\u003cheader\u003eAge\u003c/header\u003e\u003c/headers\u003e\u003crows\u003e\u003crow\u003e\u003cName\u003eEve\u003c/Name\u003e\u003cAge\u003e30\u003c/Age\u003e\u003c/row\u003e\u003c/rows\u003e\u003c/root\u003e';\n$table_from_xml = $importer-\u003eimport($xml_data, 'xml');\n\n$markdown_data = \"| Name | Age |\\n|:---|---:|\\n| Frank | 40 |\\n| Grace | 35 |\";\n$table_from_markdown = $importer-\u003eimport($markdown_data, 'markdown');\n\n// You can also directly use specific importer classes:\nuse ChernegaSergiy\\TableMagic\\Importers\\CsvTableImporter;\n$csv_importer = new CsvTableImporter();\n$table_direct_csv = $csv_importer-\u003eimport($csv_data);\n```\n\n### Exporting Data\n\nTables can be exported using the `TableExporter` factory, which delegates to specific exporter classes based on the format:\n\n```php\nuse ChernegaSergiy\\TableMagic\\TableExporter;\n\n$table_to_export = new Table(['Col1'], ['Val1']);\n$exporter = new TableExporter($table_to_export);\n\n$html_output = $exporter-\u003eexport('html');\n$csv_output = $exporter-\u003eexport('csv');\n$json_output = $exporter-\u003eexport('json');\n$xml_output = $exporter-\u003eexport('xml');\n$markdown_output = $exporter-\u003eexport('markdown');\n\n// You can also directly use specific exporter classes:\nuse ChernegaSergiy\\TableMagic\\Exporters\\HtmlTableExporter;\n$html_exporter = new HtmlTableExporter();\n$direct_html_output = $html_exporter-\u003eexport($table_to_export);\n```\n\n### Terminal Interaction\n\nFor large datasets, you can interactively paginate through the table:\n\n```php\nuse ChernegaSergiy\\TableMagic\\TerminalInteraction;\n\n$terminal_interaction = new TerminalInteraction($table);\n$terminal_interaction-\u003erun();\n```\n\nAn example script demonstrating terminal interaction is available at `examples/run_terminal_interaction.php`.\n\n## Contributing\n\nContributions are welcome and appreciated! Here's how you can contribute:\n\n1. Fork the project\n2. Create your feature branch (`git checkout -b feature/AmazingFeature`)\n3. Commit your changes (`git commit -m 'Add some AmazingFeature'`)\n4. Push to the branch (`git push origin feature/AmazingFeature`)\n5. Open a Pull Request\n\nPlease make sure to update tests as appropriate and adhere to the existing coding style.\n\n## License\n\nThis project is licensed under the CSSM Unlimited License v2.0 (CSSM-ULv2). See the [LICENSE](LICENSE) file for details.\n\n## Acknowledgments\n\n- Inspired by the Python [PrettyTable](https://github.com/jazzband/prettytable) library\n- Thanks to all contributors who have helped shape TableMagic\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchernegasergiy%2Ftable-magic","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchernegasergiy%2Ftable-magic","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchernegasergiy%2Ftable-magic/lists"}