josantonius / cli-printer
Fund package maintenance!
Josantonius
Requires
- php: ^8.1
Requires (Dev)
- monolog/monolog: ^3.2
- phpmd/phpmd: ^2.6
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: ^3.7
README
PHP library for print messages in the command line interface (CLI). Optionally supports Monolog\Logger.
For development or test environments you can disable on-console message
output by defining a constant named DISABLE_CONSOLE_PRINTING
.
Requirements
-
Operating System: Linux.
-
PHP versions: 8.1 | 8.2.
Installation
The preferred way to install this extension is through Composer.
To install PHP CliPrinter library, simply:
composer require josantonius/cli-printer
The previous command will only install the necessary files, if you prefer to download the entire source code you can use:
composer require josantonius/cli-printer --prefer-source
You can also clone the complete repository with Git:
git clone https://github.com/josantonius/php-cli-printer.git
Available Classes
CliPrinter Class
Josantonius\CliPrinter\CliPrinter
Create new instance:
/** * @param array $messages The messages to be printed, in the format ['id' => 'message']. */ public function __construct(protected array $messages = []);
Display a message on the console:
/** * Formatter: The $message and $params work in a similar way as the sprintf method: * display('tag', 'The %s %d', ['message', 8]). * * Messages: If $messages was provided, the message can be replaced by the ID: * display('tag', 'user.error'). * * Logger: If used and the tag name of the message matches a level, * it shall be used when adding the record. Otherwise use the debug level. * * @param string $tagName Message tag name. * @param string $message The message or message ID. * @param array $params Additional context for format $message or/and passing it to the log. */ public function display(string $tagName, string $message, array $params = []): self;
Dynamically display a message using the message tag as the method name:
/** * @param string $tagName Message tag name. * @param string $arguments The first value is the message and the second the parameters. */ public function __call(string $tagName, array $arguments): self;
Add an instance of Monolog\Logger to add a log for each message displayed:
/** * @param Logger $logger Instance of the Logger class. */ public function useLogger(Logger $logger): self;
Print new lines on the console:
/** * @param int $times Number of new lines to print. */ public function newLine(int $times = 1): self;
Set the number of line breaks before and after a message:
/** * @param int $before Number of line breaks before a message. * @param int $after Number of line breaks after a message. */ public function setLineBreaks(int $before, int $after): self;
Set the default color of the message tags:
/** * @param Color $color Default message tag color. */ public function setDefaultTagColor(Color $color): self;
Set the color for a message tag:
/** * @param string $tagName Message tag name. * @param Color $tagColor Message tag color. */ public function setTagColor(string $tagName, Color $tagColor): self;
Color Class
Josantonius\CliPrinter\Color
Available colors:
enum Color: int { case BLACK = 40; case RED = 41; case GREEN = 42; case YELLOW = 43; case BLUE = 44; case PURPLE = 45; case CYAN = 46; case WHITE = 47; }
Usage
Example of use for this library:
Displaying messages
use Josantonius\CliPrinter\CliPrinter; $printer = new CliPrinter(); $printer->display('alert', 'An alert message.'); $printer->display('error', 'An error message.');
Displaying messages without a tag
use Josantonius\CliPrinter\CliPrinter; $printer = new CliPrinter(); $printer->display('', 'A message without a tag.');
Displaying preset messages via their key
use Josantonius\CliPrinter\CliPrinter; $printer = new CliPrinter([ 'user.created' => 'User created.', 'user.changed' => 'User changed.', ]); $printer->display('create', 'user.created'); $printer->display('change', 'user.changed');
Displaying formatted messages
use Josantonius\CliPrinter\CliPrinter; $printer = new CliPrinter([ 'msg.notice' => 'A %s message.', ]); $printer->display('notice', 'msg.notice', ['notice']); $printer->display('detail', 'A %s message.', ['detail']);
Changing the line breaks before and after each message
use Josantonius\CliPrinter\CliPrinter; $printer = new CliPrinter(); $printer->setLineBreaks(before: 1, after: 0); $printer->display('alert', 'Alert message.'); $printer->display('error', 'Error message.');
use Josantonius\CliPrinter\CliPrinter; $printer = new CliPrinter(); $printer->setLineBreaks(before: 2, after: 1); $printer->display('alert', 'Alert message.'); $printer->display('error', 'Error message.');
Adding extra line breaks
use Josantonius\CliPrinter\CliPrinter; $printer = new CliPrinter(); $printer->display('request', 'Message.'); $printer->newLine(); $printer->display('response', 'Message.'); $printer->newLine(times: 3); $printer->display('warning', 'Message.');
Changing default color of tags
use Josantonius\CliPrinter\Color; use Josantonius\CliPrinter\CliPrinter; $printer = new CliPrinter(); $printer->setDefaultTagColor(Color::PURPLE); $printer->display('alert', 'An alert message.'); $printer->display('error', 'An error message.');
Set a custom color for each tag
use Josantonius\CliPrinter\Color; use Josantonius\CliPrinter\CliPrinter; $printer = new CliPrinter(); $printer->setTagColor('info', Color::BLUE) ->setTagColor('alert', Color::BLACK) ->setTagColor('error', Color::RED) ->setTagColor('debug', Color::GREEN) ->setTagColor('notice', Color::CYAN) ->setTagColor('warning', Color::YELLOW) ->setTagColor('critical', Color::PURPLE) ->setTagColor('emergency', Color::WHITE); $printer->display('info', 'A message.'); $printer->display('alert', 'A message.'); $printer->display('error', 'A message.'); $printer->display('debug', 'A message.'); $printer->display('notice', 'A message.'); $printer->display('warning', 'A message.'); $printer->display('critical', 'A message.'); $printer->display('emergency', 'A message.');
Displaying messages using dynamic methods
use Josantonius\CliPrinter\CliPrinter; $printer = new CliPrinter(); $printer->request('A request message.'); $printer->response('A %s message.', ['response']);
Using Monolog\Logger
use Monolog\Logger; use Monolog\Handler\StreamHandler; use Josantonius\CliPrinter\CliPrinter; $logger = new Logger('LOG'); $logger->pushHandler(new StreamHandler(__DIR__ . '/log.txt')); $printer = new CliPrinter(); $printer->useLogger($logger); /** * It will create a "notice" log. * * [2023-01-13T19:50:47.954791+00:00] LOG.NOTICE: A notice message. {"foo":"bar"} [] */ $printer->display('notice', 'A notice message.', ['foo' => 'bar']); /** * It will create a "debug" log as there is no "create" level. * * [2023-01-13T19:50:47.954319+00:00] LOG.DEBUG: A create message. ["create"] [] */ $printer->display('create', 'A %s message.', ['create']);
Tests
To run tests you just need composer and to execute the following:
git clone https://github.com/josantonius/php-cli-printer.git
cd php-cli-printer
composer install
Run unit tests with PHPUnit:
composer phpunit
Run code standard tests with PHPCS:
composer phpcs
Run PHP Mess Detector tests to detect inconsistencies in code style:
composer phpmd
Run all previous tests:
composer tests
TODO
- Add new feature
- Improve tests
- Improve documentation
- Improve English translation in the README file
- Refactor code for disabled code style rules (see phpmd.xml and phpcs.xml)
Changelog
Detailed changes for each release are documented in the release notes.
Contribution
Please make sure to read the Contributing Guide, before making a pull request, start a discussion or report a issue.
Thanks to all contributors! ❤️
Sponsor
If this project helps you to reduce your development time, you can sponsor me to support my open source work 😊
License
This repository is licensed under the MIT License.
Copyright © 2023-present, Josantonius