A simple PHP tool for formatting output text on a terminal.
Install using composer:
composer require psdi/motif [--no-dev]
Include the flag
--no-dev
if you want to omit installing PHPUnit (which isn't exactly a lightweight framework).
The Formatter
class has three main output functions:
format
takes a string as well as a style string
The style string has the following syntax:
[format,styles]|color:[color]|bg:[background]
. The order of the styles doesn't matter; you can even omit them.
formatLine
expands onformat
; however, it appends a line break to the textdisplay
accepts a text as well as a mode (success, warning, error, info). The function prints the text with the corresponding background color (green, yellow, red, blue).
<?php
// index.php
require 'vendor/autoload.php';
use Motif\Formatter;
// [1] Prints 'Hello, world!' (bold and underlined) in green on console
echo Formatter::formatLine('Hello, world!', 'bold|color:green');
// [2,3] Examples with and without line breaks
echo Formatter::format('Somebody once told me,', 'strike|color:black|bg:blue');
echo Formatter::formatLine(' the world was gonna roll me.', 'underline|color:cyan');
// [4] Prints 'Error downloading files.' in white with a red background
echo Formatter::display('Error downloading files.', 'error');
// [5,6,7] Some more examples
echo Formatter::formatLine('This is a sample text. ', 'bold,italic|color:yellow');
echo Formatter::formatLine(
'This is also a sample text, but in magenta.',
'italic|color:magenta'
);
echo Formatter::display('Success! •ᴗ•', 'success');
The above code would bring about:
If you spot bugs or have any suggestions, feel free to add them to the issue tracker.