PHPackages                             josantonius/cli-printer - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [CLI &amp; Console](/categories/cli)
4. /
5. josantonius/cli-printer

ActiveLibrary[CLI &amp; Console](/categories/cli)

josantonius/cli-printer
=======================

v1.0.0(3y ago)33731MITPHPPHP ^8.1

Since Jan 13Pushed 3y ago1 watchersCompare

[ Source](https://github.com/josantonius/php-cli-printer)[ Packagist](https://packagist.org/packages/josantonius/cli-printer)[ GitHub Sponsors](https://github.com/Josantonius)[ RSS](/packages/josantonius-cli-printer/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (4)Versions (2)Used By (1)

PHP CliPrinter library
======================

[](#php-cliprinter-library)

[![Latest Stable Version](https://camo.githubusercontent.com/c25f9fa257f990f34baa74f8f1d4926c34c8b9e5446548af8355c97f66d304ab/68747470733a2f2f706f7365722e707567782e6f72672f6a6f73616e746f6e6975732f636c692d7072696e7465722f762f737461626c65)](https://packagist.org/packages/josantonius/cli-printer)[![License](https://camo.githubusercontent.com/19802074d0d73fa70703de8b557e04d658fcfd71da2cd9c3921c788cc973ffe7/68747470733a2f2f706f7365722e707567782e6f72672f6a6f73616e746f6e6975732f636c692d7072696e7465722f6c6963656e7365)](LICENSE)[![Total Downloads](https://camo.githubusercontent.com/d41f5a27f00de46c475d0c665546127b56756f03404385ebc35b1f97593b7221/68747470733a2f2f706f7365722e707567782e6f72672f6a6f73616e746f6e6975732f636c692d7072696e7465722f646f776e6c6f616473)](https://packagist.org/packages/josantonius/cli-printer)[![CI](https://github.com/josantonius/php-cli-printer/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/josantonius/php-cli-printer/actions/workflows/ci.yml)[![CodeCov](https://camo.githubusercontent.com/41acac213f9790174f858d9e57b7e9fa3d3d2a1436c76659ab8f442a0a6c6d46/68747470733a2f2f636f6465636f762e696f2f67682f6a6f73616e746f6e6975732f7068702d636c692d7072696e7465722f6272616e63682f6d61696e2f67726170682f62616467652e737667)](https://codecov.io/gh/josantonius/php-cli-printer)[![PSR1](https://camo.githubusercontent.com/b502a899c9aec217e98971160f816f87346be272cf1a25cfa4793f2ee724bfc8/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5053522d312d6635373034362e737667)](https://www.php-fig.org/psr/psr-1/)[![PSR4](https://camo.githubusercontent.com/d1c090de87e968254a6658528f3bfe9c9dad422d6047fd1323dc211560182c01/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5053522d342d3962353962362e737667)](https://www.php-fig.org/psr/psr-4/)[![PSR12](https://camo.githubusercontent.com/19c529c6dc0656dcc2a16c1a84af450b7bd0dc7b0571b8f17e4fc9f2414f8821/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5053522d31322d3161626339632e737667)](https://www.php-fig.org/psr/psr-12/)

PHP library for print messages in the command line interface (CLI). Optionally supports [Monolog\\Logger](https://github.com/Seldaek/monolog).

For development or test environments you can disable on-console message output by defining a constant named `DISABLE_CONSOLE_PRINTING`.

---

- [Requirements](#requirements)
- [Installation](#installation)
- [Available Classes](#available-classes)
    - [CliPrinter Class](#cliprinter-class)
    - [Color Class](#color-class)
- [Usage](#usage)
- [Tests](#tests)
- [TODO](#todo)
- [Changelog](#changelog)
- [Contribution](#contribution)
- [Sponsor](#sponsor)
- [License](#license)

---

Requirements
------------

[](#requirements)

- Operating System: Linux.
- PHP versions: 8.1 | 8.2.

Installation
------------

[](#installation)

The preferred way to install this extension is through [Composer](http://getcomposer.org/download/).

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
-----------------

[](#available-classes)

### CliPrinter Class

[](#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

[](#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
-----

[](#usage)

Example of use for this library:

### Displaying messages

[](#displaying-messages)

```
use Josantonius\CliPrinter\CliPrinter;

$printer = new CliPrinter();

$printer->display('alert', 'An alert message.');
$printer->display('error', 'An error message.');
```

[![Example](resources/example-1.png)](resources/example-1.png)

### Displaying messages without a tag

[](#displaying-messages-without-a-tag)

```
use Josantonius\CliPrinter\CliPrinter;

$printer = new CliPrinter();

$printer->display('', 'A message without a tag.');
```

[![Example](resources/example-8.png)](resources/example-8.png)

### Displaying preset messages via their key

[](#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');
```

[![Example](resources/example-2.png)](resources/example-2.png)

### Displaying formatted messages

[](#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']);
```

[![Example](resources/example-3.png)](resources/example-3.png)

### Changing the line breaks before and after each message

[](#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.');
```

[![Example](resources/example-5.png)](resources/example-5.png)

```
use Josantonius\CliPrinter\CliPrinter;

$printer = new CliPrinter();

$printer->setLineBreaks(before: 2, after: 1);

$printer->display('alert', 'Alert message.');
$printer->display('error', 'Error message.');
```

[![Example](resources/example-6.png)](resources/example-6.png)

### Adding extra line breaks

[](#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.');
```

[![Example](resources/example-9.png)](resources/example-9.png)

### Changing default color of tags

[](#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.');
```

[![Example](resources/example-4.png)](resources/example-4.png)

### Set a custom color for each tag

[](#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.');
```

[![Example](resources/example-7.png)](resources/example-7.png)

### Displaying messages using dynamic methods

[](#displaying-messages-using-dynamic-methods)

```
use Josantonius\CliPrinter\CliPrinter;

$printer = new CliPrinter();

$printer->request('A request message.');

$printer->response('A %s message.', ['response']);
```

[![Example](resources/example-11.png)](resources/example-11.png)

### Using [Monolog\\Logger](https://github.com/Seldaek/monolog)

[](#using-monologlogger)

```
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']);
```

[![Example](resources/example-10.png)](resources/example-10.png)

Tests
-----

[](#tests)

To run [tests](tests) you just need [composer](http://getcomposer.org/download/)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](https://phpunit.de/):

```
composer phpunit
```

Run code standard tests with [PHPCS](https://github.com/squizlabs/PHP_CodeSniffer):

```
composer phpcs
```

Run [PHP Mess Detector](https://phpmd.org/) tests to detect inconsistencies in code style:

```
composer phpmd
```

Run all previous tests:

```
composer tests
```

TODO
----

[](#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
---------

[](#changelog)

Detailed changes for each release are documented in the [release notes](https://github.com/josantonius/php-cli-printer/releases).

Contribution
------------

[](#contribution)

Please make sure to read the [Contributing Guide](.github/CONTRIBUTING.md), before making a pull request, start a discussion or report a issue.

Thanks to all [contributors](https://github.com/josantonius/php-cli-printer/graphs/contributors)! ❤️

Sponsor
-------

[](#sponsor)

If this project helps you to reduce your development time, [you can sponsor me](https://github.com/josantonius#sponsor) to support my open source work 😊

License
-------

[](#license)

This repository is licensed under the [MIT License](LICENSE).

Copyright © 2023-present, [Josantonius](https://github.com/josantonius#contact)

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

1219d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/4b221283501ec8a9cbaefaf27821a91ae8ddd33bddf1fccc6c6815b7ad216ff1?d=identicon)[Josantonius](/maintainers/Josantonius)

---

Top Contributors

[![josantonius](https://avatars.githubusercontent.com/u/18104336?v=4)](https://github.com/josantonius "josantonius (7 commits)")

---

Tags

phpcli-printer

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/josantonius-cli-printer/health.svg)

```
[![Health](https://phpackages.com/badges/josantonius-cli-printer/health.svg)](https://phpackages.com/packages/josantonius-cli-printer)
```

###  Alternatives

[nunomaduro/termwind

It's like Tailwind CSS, but for the console.

2.5k239.8M286](/packages/nunomaduro-termwind)[nunomaduro/laravel-console-task

Laravel Console Task is a output method for your Laravel/Laravel Zero commands.

2582.1M11](/packages/nunomaduro-laravel-console-task)[mehrancodes/laravel-harbor

A CLI tool to Quickly create On-Demand preview environment for your apps.

9989.0k](/packages/mehrancodes-laravel-harbor)[alecrabbit/php-cli-snake

Lightweight cli spinner with zero dependencies

29211.3k5](/packages/alecrabbit-php-cli-snake)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
