All Projects → bramus → ansi-php

bramus / ansi-php

Licence: MIT license
ANSI Control Functions and ANSI Control Sequences (Colors, Erasing, etc.) for PHP CLI Apps

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to ansi-php

ansicolor
A JavaScript ANSI color/style management. ANSI parsing. ANSI to CSS. Small, clean, no dependencies.
Stars: ✭ 91 (+10.98%)
Mutual labels:  ansi, ansi-colors, ansi-escape-sequences
ansiart2utf8
Processes legacy BBS-style ANSI art (ACiDDraw, PabloDraw, etc.) to UTF-8. Escape codes and line endings are processed for terminal friendliness.
Stars: ✭ 32 (-60.98%)
Mutual labels:  ansi, ansi-colors
kotter
A declarative, Kotlin-idiomatic API for writing dynamic console applications.
Stars: ✭ 355 (+332.93%)
Mutual labels:  ansi, ansi-colors
Jcolor
An easy syntax to format your strings with colored fonts and backgrounds.
Stars: ✭ 255 (+210.98%)
Mutual labels:  ansi, ansi-colors
log-utils
Basic logging utils: colors, symbols and timestamp.
Stars: ✭ 24 (-70.73%)
Mutual labels:  ansi, ansi-colors
pytermgui
Python TUI framework with mouse support, modular widget system, customizable and rapid terminal markup language and more!
Stars: ✭ 1,270 (+1448.78%)
Mutual labels:  ansi, ansi-escape-sequences
gitlab-job-log-viewer
Browser extension for code highlighting raw logs in Gitlab CI
Stars: ✭ 21 (-74.39%)
Mutual labels:  ansi, ansi-colors
kolorist
A tiny utility to colorize stdin/stdout
Stars: ✭ 160 (+95.12%)
Mutual labels:  ansi, ansi-colors
Ansi Colors
Easily add ANSI colors to your text and symbols in the terminal. ansi-colors is the official ansi styling library for gulp, and is used by hundreds of other projects, including mocha and enquirer.
Stars: ✭ 300 (+265.85%)
Mutual labels:  ansi, ansi-colors
Paint
Ruby gem for ANSI terminal colors 🎨︎ VERY FAST
Stars: ✭ 317 (+286.59%)
Mutual labels:  ansi, ansi-colors
Mordant
Full-featured text styling for Kotlin command-line applications
Stars: ✭ 382 (+365.85%)
Mutual labels:  ansi, ansi-colors
Picocli
Picocli is a modern framework for building powerful, user-friendly, GraalVM-enabled command line apps with ease. It supports colors, autocompletion, subcommands, and more. In 1 source file so apps can include as source & avoid adding a dependency. Written in Java, usable from Groovy, Kotlin, Scala, etc.
Stars: ✭ 3,286 (+3907.32%)
Mutual labels:  ansi, ansi-colors
Ansi Econsole
Eclipse plugin that understands ANSI escape sequences to color the Eclipse console output.
Stars: ✭ 72 (-12.2%)
Mutual labels:  ansi, ansi-colors
Ololog
A better console.log for the log-driven debugging junkies
Stars: ✭ 141 (+71.95%)
Mutual labels:  ansi, ansi-colors
django-concurrency-talk
🎭 Database Integrity in Django: Safely Handling Critical Data in Distributed Systems
Stars: ✭ 49 (-40.24%)
Mutual labels:  ansi
ckwin
C-Kermit for Windows - scriptable internet and serial communications with terminal emulation
Stars: ✭ 35 (-57.32%)
Mutual labels:  ansi
yachalk
🖍️ Terminal string styling done right
Stars: ✭ 131 (+59.76%)
Mutual labels:  ansi
FACE
Fortran Ansi Colors (and Styles) Environment
Stars: ✭ 36 (-56.1%)
Mutual labels:  ansi-colors
pixelizer
An efficient image pixelizer written in go
Stars: ✭ 103 (+25.61%)
Mutual labels:  ansi-colors
VTXEdit
VTX ANSI Editor.
Stars: ✭ 17 (-79.27%)
Mutual labels:  ansi

ANSI PHP

Build Status Source Version Downloads License

ANSI Control Functions and ANSI Control Sequences for PHP CLI Apps

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

About

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

  • ANSI Control Functions control an action such as line spacing, paging, or data flow.
  • ANSI Control Sequences allow one to clear the screen, move the cursor, set text colors, etc.

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

Features

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

  • BS: Backspace
  • BEL: Bell
  • CR: Carriage Return
  • ESC: Escape
  • LF: Line Feed
  • TAB: Tab

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

  • CUB (Cursor Back): Move cursor back.
  • CUD (Cursor Down): Move cursor down.
  • CUF (Cursor Forward): Move cursor forward.
  • CUP (Cursor Position): Move cursor to position.
  • CUU (Cursor Up): Move cursor up.
  • ED (Erase Display): Erase (parts of) the display.
  • EL (Erase In Line): Erase (parts of) the current line.
  • SGR (Select Graphic Rendition): Manipulate text styling (bold, underline, blink, colors, etc.).

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:

Monolog Colored Line Formatter

Prerequisites/Requirements

  • PHP 5.4.0 or greater

Installation

Installation is possible using Composer

composer require bramus/ansi-php ~3.1

Usage

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.

Quick example

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.

Concepts

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

  • StreamWriter: Writes the data to a stream. Just pass in the path to a file and it will open a stream for you. Defaults to writing to php://stdout.
  • BufferWriter: Writes the data to a buffer. When calling flush() the contents of the buffer will be returned.
  • ProxyWriter: Acts as a proxy to another writer. Writes the data to an internal buffer. When calling flush() the writer will first write the data to the other writer before returning it.

The Ansi helper class functions

Core functions:

  • text($text): Write a piece of data to the writer
  • setWriter(\Bramus\Ansi\Writers\WriterInterface $writer): Sets the writer
  • getWriter(): Gets the writer

ANSI Control Function shorthands:

These shorthands write a Control Character to the writer.

  • bell(): Bell Control Character (\a)
  • backspace(): Backspace Control Character (\b)
  • tab(): Tab Control Character (\t)
  • lf(): Line Feed Control Character (\n)
  • cr(): Carriage Return Control Character (\r)
  • esc(): Escape Control Character

SGR ANSI Escape Sequence shorthands:

These shorthands write SGR ANSI Escape Sequences to the writer.

  • nostyle() or reset(): Remove all text styling (colors, bold, etc)
  • color(): Set the foreground and/or backgroundcolor of the text. (see further)
  • bold() or bright(): Bold: On. On some systems "Intensity: Bright"
  • normal(): Bold: Off. On some systems "Intensity: Normal"
  • faint(): Intensity: Faint. (Not widely supported)
  • italic(): Italic: On. (Not widely supported)
  • underline(): Underline: On.
  • blink(): Blink: On.
  • negative(): Inverse or Reverse. Swap foreground and background.
  • strikethrough(): Strikethrough: On. (Not widely supported)

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.

  • eraseDisplay(): Erase the entire screen and moves the cursor to home.
  • eraseDisplayUp(): Erase the screen from the current line up to the top of the screen.
  • eraseDisplayDown(): Erase the screen from the current line down to the bottom of the screen.

EL ANSI Escape Sequence shorthands:

These shorthands write EL ANSI Escape Sequences to the writer.

  • eraseLine(): Erase the entire current line.
  • eraseLineToEOL(): Erase from the current cursor position to the end of the current line.
  • eraseLineToSOL(): Erases from the current cursor position to the start of the current line.

CUB/CUD/CUF/CUP/CUU ANSI Escape Sequence shorthands:

  • cursorBack($n): Move cursor back $n positions (default: 1)
  • cursorForward($n): Move cursor forward $n positions (default: 1)
  • cursorDown($n): Move cursor down $n positions (default: 1)
  • cursorUp($n): Move cursor up $n positions (default: 1)
  • cursorPosition($n, $m): Move cursor to position $n,$m (default: 1,1)

Extra functions

  • flush() or get(): Retrieve contents of a FlushableWriter writer.
  • e(): Echo the contents of a FlushableWriter writer.

Examples

The Basics

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

Using a FlushableWriter

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();

Chaining

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();

Styling Text: The Basics

$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');

Styling Text: Colors

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

Foreground (Text) Colors

  • SGR::COLOR_FG_BLACK: Black Foreground Color
  • SGR::COLOR_FG_RED: Red Foreground Color
  • SGR::COLOR_FG_GREEN: Green Foreground Color
  • SGR::COLOR_FG_YELLOW: Yellow Foreground Color
  • SGR::COLOR_FG_BLUE: Blue Foreground Color
  • SGR::COLOR_FG_PURPLE: Purple Foreground Color
  • SGR::COLOR_FG_CYAN: Cyan Foreground Color
  • SGR::COLOR_FG_WHITE: White Foreground Color
  • SGR::COLOR_FG_BLACK_BRIGHT: Black Foreground Color (Bright)
  • SGR::COLOR_FG_RED_BRIGHT: Red Foreground Color (Bright)
  • SGR::COLOR_FG_GREEN_BRIGHT: Green Foreground Color (Bright)
  • SGR::COLOR_FG_YELLOW_BRIGHT: Yellow Foreground Color (Bright)
  • SGR::COLOR_FG_BLUE_BRIGHT: Blue Foreground Color (Bright)
  • SGR::COLOR_FG_PURPLE_BRIGHT: Purple Foreground Color (Bright)
  • SGR::COLOR_FG_CYAN_BRIGHT: Cyan Foreground Color (Bright)
  • SGR::COLOR_FG_WHITE_BRIGHT: White Foreground Color (Bright)
  • SGR::COLOR_FG_RESET: Default Foreground Color

Background Colors

  • SGR::COLOR_BG_BLACK: Black Background Color
  • SGR::COLOR_BG_RED: Red Background Color
  • SGR::COLOR_BG_GREEN: Green Background Color
  • SGR::COLOR_BG_YELLOW: Yellow Background Color
  • SGR::COLOR_BG_BLUE: Blue Background Color
  • SGR::COLOR_BG_PURPLE: Purple Background Color
  • SGR::COLOR_BG_CYAN: Cyan Background Color
  • SGR::COLOR_BG_WHITE: White Background Color
  • SGR::COLOR_BG_BLACK_BRIGHT: Black Background Color (Bright)
  • SGR::COLOR_BG_RED_BRIGHT: Red Background Color (Bright)
  • SGR::COLOR_BG_GREEN_BRIGHT: Green Background Color (Bright)
  • SGR::COLOR_BG_YELLOW_BRIGHT: Yellow Background Color (Bright)
  • SGR::COLOR_BG_BLUE_BRIGHT: Blue Background Color (Bright)
  • SGR::COLOR_BG_PURPLE_BRIGHT: Purple Background Color (Bright)
  • SGR::COLOR_BG_CYAN_BRIGHT: Cyan Background Color (Bright)
  • SGR::COLOR_BG_WHITE_BRIGHT: White Background Color (Bright)
  • SGR::COLOR_BG_RESET: Default Background Color

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

Using the raw classes

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;

Unit Testing

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

  • If PHPUnit is installed globally run phpunit to run the tests.

  • If PHPUnit is not installed globally, install it locally throuh composer by running composer install --dev. Run the tests themselves by calling vendor/bin/phpunit or composer test

Unit tests are also automatically run on Travis CI

License

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

ANSI References

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].