A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/bramus/ansi-php below:

bramus/ansi-php: ANSI Control Functions and ANSI Control Sequences (Colors, Erasing, etc.) for PHP CLI Apps

ANSI Control Functions and ANSI Control Sequences for PHP CLI Apps

Built by Bramus! - https://www.bram.us/

bramus/ansi-php is a set of classes to working with ANSI Control Functions and ANSI Control Sequences on text based terminals.

(Sidenote: An “ANSI Escape Sequence” is a special type of “ANSI Control Sequence” which starts with the ESC ANSI Control Function. The terms are not interchangeable.)

When it comes to ANSI Control Functions bramus/ansi-php supports:

When it comes to ANSI Escape Sequences bramus/ansi-php supports:

Other Control Sequences – such as DCH, NEL, etc. – are not (yet) supported.

An example library that uses bramus/ansi-php is bramus/monolog-colored-line-formatter. It uses bramus/ansi-php's SGR support to colorize the output:

Prerequisites/Requirements

Installation is possible using Composer

composer require bramus/ansi-php ~3.1

The easiest way to use ANSI PHP is to use the bundled Ansi helper class which provides easy shorthands to working with bramus/ansi-php. The Ansi class is written in such a way that you can chain calls to one another.

If you're feeling adventurous, you're of course free to use the raw ControlFunction and ControlSequence classes.

use \Bramus\Ansi\Ansi;
use \Bramus\Ansi\Writers\StreamWriter;
use \Bramus\Ansi\ControlSequences\EscapeSequences\Enums\SGR;

// Create Ansi Instance
$ansi = new Ansi(new StreamWriter('php://stdout'));

// Output some styled text on screen, along with a Line Feed and a Bell
$ansi->color(array(SGR::COLOR_FG_RED, SGR::COLOR_BG_WHITE))
     ->blink()
     ->text('I will be blinking red on a white background.')
     ->nostyle()
     ->text(' And I will be normally styled.')
     ->lf()
     ->text('Ooh, a bell is coming ...')
     ->bell();

See more examples further down on how to use these.

Since v3.0 bramus/ansi-php uses the concept of writers to write the data to. By default a StreamWriter writing to php://stdout is used.

The following writers are provided

The Ansi helper class functions ANSI Control Function shorthands:

These shorthands write a Control Character to the writer.

SGR ANSI Escape Sequence shorthands:

These shorthands write SGR ANSI Escape Sequences to the writer.

IMPORTANT: Select Graphic Rendition works in such a way that text styling you have set will remain active until you call nostyle() or reset() to return to the default styling.

ED ANSI Escape Sequence shorthands:

These shorthands write ED ANSI Escape Sequences to the writer.

EL ANSI Escape Sequence shorthands:

These shorthands write EL ANSI Escape Sequences to the writer.

CUB/CUD/CUF/CUP/CUU ANSI Escape Sequence shorthands:
// Create Ansi Instance
$ansi = new \Bramus\Ansi\Ansi();

// This will output a Bell
$ansi->bell();

// This will output some text
$ansi->text('Hello World!');

NOTE: As no $writer is passed into the constructor of \Bramus\Ansi\Ansi, the default StreamWriter writing to php://stdout is used.

Flushable Writers are writers that cache the data and only output it when flushed using its flush() function. The BufferWriter and ProxyWriter implement this interface.

// Create Ansi Instance
$ansi = new \Bramus\Ansi\Ansi(new \Bramus\Ansi\Writers\BufferWriter());

// This will append a bell to the buffer. It will not output it.
$ansi->bell();

// This will append a bell to the buffer. It will not output it.
$ansi->text('Hello World!');

// Now we'll output it
echo $ansi->get();

bramus/ansi-php's wrapper Ansi class supports chaining.

// Create Ansi Instance
$ansi = new \Bramus\Ansi\Ansi();

// This will output a Line Feed, some text, a Bell, and a Line Feed
$ansi->lf()->text('hello')->bell()->lf();
$ansi = new \Bramus\Ansi\Ansi();
$ansi->bold()->underline()->text('I will be bold and underlined')->lf();

IMPORTANT Select Graphic Rendition works in such a way that text styling you have set will remain active until you call nostyle() or reset() to return to the default styling.

$ansi = new \Bramus\Ansi\Ansi();

$ansi->bold()->underline()->text('I will be bold and underlined')->lf();
$ansi->text('I will also be bold because nostyle() has not been called yet')->lf();
$ansi->nostyle()->blink()->text('I will be blinking')->nostyle()->lf();
$ansi->text('I will be normal because nostyle() was called on the previous line');

Colors, and other text styling options, are defined as contants on \Bramus\Ansi\ControlSequences\EscapeSequences\Enums\SGR.

Pass one of these into $ansi->color() and the color will be set.

use \Bramus\Ansi\ControlSequences\EscapeSequences\Enums\SGR;

$ansi = new \Bramus\Ansi\Ansi();

$ansi->color(SGR::COLOR_FG_RED)
     ->text('I will be red')
     ->nostyle();

To set the foreground and background color in one call, pass them using an array to $ansi->color()

use \Bramus\Ansi\ControlSequences\EscapeSequences\Enums\SGR;

$ansi = new \Bramus\Ansi\Ansi();

$ansi->color(array(SGR::COLOR_FG_RED, SGR::COLOR_BG_WHITE))
     ->blink()
     ->text('I will be blinking red on a wrhite background.')
     ->nostyle();
Creating a loading Spinner

By manipulating the cursor position one can create an in-place spinner

use \Bramus\Ansi\Ansi;
use \Bramus\Ansi\Writers\StreamWriter;
use \Bramus\Ansi\ControlSequences\EscapeSequences\Enums\EL;
use \Bramus\Ansi\ControlSequences\EscapeSequences\Enums\SGR;

// Create Ansi Instance
$ansi = new Ansi(new StreamWriter('php://stdout'));

// Parts of our spinner
$spinnerParts = ['','','','','','','',''];

$ansi->text('Loading Data')->lf();
for ($i = 0; $i < 100; $i++) {
    $ansi
        // Erase entire line
        ->el(EL::ALL)
        // Go back to very first position on current line
        ->cursorBack(9999)
        // Add a blue spinner
        ->color(SGR::COLOR_FG_BLUE)->text($spinnerParts[$i % sizeof($spinnerParts)])
        // Write percentage
        ->nostyle()->text(' ' . str_pad($i, 3, 0, STR_PAD_LEFT) . '%');

    usleep(50000);
}
$ansi
    ->el(EL::ALL)
    ->cursorBack(9999)
    ->color(SGR::COLOR_FG_GREEN)->text('')
    ->nostyle()->text(' 100%')
    ->lf();

This snippet will output a little loading spinner icon + the current percentage (e.g. ⣯ 009%) that constantly updates in-place. When 100% is reached, the line will read ✔ 100%.

As all raw ControlFunction and ControlSequence classes are provided with a __toString() function it's perfectly possible to directly echo some bramus/ansi-php instance.

// Output a Bell Control Character
echo new \Bramus\Ansi\ControlFunctions\Bell();

// Output an ED instruction, to erase the entire screen
echo new \Bramus\Ansi\ControlSequences\EscapeSequences\ED(
    \Bramus\Ansi\ControlSequences\EscapeSequences\Enums\ED::ALL
);

To fetch their contents, use the get() function:

// Get ANSI string for a Bell Control Character
$bell = (new \Bramus\Ansi\ControlFunctions\Bell())->get();

// Get ANSI string for an ED instruction, to erase the entire screen
$eraseDisplay = (new \Bramus\Ansi\ControlSequences\EscapeSequences\ED(
    \Bramus\Ansi\ControlSequences\EscapeSequences\Enums\ED::ALL
))->get();

echo $bell . $bell . $eraseDisplay . $bell;

bramus/ansi-php ships with unit tests using PHPUnit.

Unit tests are also automatically run on Travis CI

bramus/ansi-php is released under the MIT public license. See the enclosed LICENSE for details.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4