psdi/motif

There is no license information available for the latest version (v1.0) of this package.

PHP CLI output formatter

v1.0 2020-03-23 17:25 UTC

This package is auto-updated.

Last update: 2024-06-24 03:16:13 UTC


README

Motif

A simple PHP tool for formatting output text on a terminal.

Installation

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

Usage

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 on format; however, it appends a line break to the text
  • display 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:

Contributions

If you spot bugs or have any suggestions, feel free to add them to the issue tracker.