Documentation

Colors
in package

Class Colors

Handles color output on (Linux) terminals

Tags
author

Andreas Gohr andi@splitbrain.org

license

MIT

Table of Contents

Constants

C_BLACK  = 'black'
C_BLUE  = 'blue'
C_BROWN  = 'brown'
C_CODE_REGEX  = "/(\x1b\\[[0-9;]+m)/"
C_CYAN  = 'cyan'
C_DARKGRAY  = 'darkgray'
C_GREEN  = 'green'
C_LIGHTBLUE  = 'lightblue'
C_LIGHTCYAN  = 'lightcyan'
C_LIGHTGRAY  = 'lightgray'
C_LIGHTGREEN  = 'lightgreen'
C_LIGHTPURPLE  = 'lightpurple'
C_LIGHTRED  = 'lightred'
C_PURPLE  = 'purple'
C_RED  = 'red'
C_RESET  = 'reset'
C_WHITE  = 'white'
C_YELLOW  = 'yellow'

Properties

$colors  : array<string|int, mixed>
$enabled  : bool

Methods

__construct()  : mixed
Constructor
disable()  : mixed
disable color output
enable()  : mixed
enable color output
getColorCode()  : string
Gets the appropriate terminal code for the given color
isEnabled()  : bool
ptln()  : mixed
Convenience function to print a line in a given color
reset()  : mixed
reset the terminal color
set()  : mixed
Set the given color for consecutive output
wrap()  : string
Returns the given text wrapped in the appropriate color and reset code

Constants

C_BLACK

public mixed C_BLACK = 'black'

C_BLUE

public mixed C_BLUE = 'blue'

C_BROWN

public mixed C_BROWN = 'brown'

C_CODE_REGEX

public mixed C_CODE_REGEX = "/(\x1b\\[[0-9;]+m)/"

C_CYAN

public mixed C_CYAN = 'cyan'

C_DARKGRAY

public mixed C_DARKGRAY = 'darkgray'

C_GREEN

public mixed C_GREEN = 'green'

C_LIGHTBLUE

public mixed C_LIGHTBLUE = 'lightblue'

C_LIGHTCYAN

public mixed C_LIGHTCYAN = 'lightcyan'

C_LIGHTGRAY

public mixed C_LIGHTGRAY = 'lightgray'

C_LIGHTGREEN

public mixed C_LIGHTGREEN = 'lightgreen'

C_LIGHTPURPLE

public mixed C_LIGHTPURPLE = 'lightpurple'

C_LIGHTRED

public mixed C_LIGHTRED = 'lightred'

C_PURPLE

public mixed C_PURPLE = 'purple'

C_RED

public mixed C_RED = 'red'

C_RESET

public mixed C_RESET = 'reset'

C_WHITE

public mixed C_WHITE = 'white'

C_YELLOW

public mixed C_YELLOW = 'yellow'

Properties

$colors

protected array<string|int, mixed> $colors = array(self::C_RESET => "\x1b[0m", self::C_BLACK => "\x1b[0;30m", self::C_DARKGRAY => "\x1b[1;30m", self::C_BLUE => "\x1b[0;34m", self::C_LIGHTBLUE => "\x1b[1;34m", self::C_GREEN => "\x1b[0;32m", self::C_LIGHTGREEN => "\x1b[1;32m", self::C_CYAN => "\x1b[0;36m", self::C_LIGHTCYAN => "\x1b[1;36m", self::C_RED => "\x1b[0;31m", self::C_LIGHTRED => "\x1b[1;31m", self::C_PURPLE => "\x1b[0;35m", self::C_LIGHTPURPLE => "\x1b[1;35m", self::C_BROWN => "\x1b[0;33m", self::C_YELLOW => "\x1b[1;33m", self::C_LIGHTGRAY => "\x1b[0;37m", self::C_WHITE => "\x1b[1;37m")

known color names

$enabled

protected bool $enabled = true

should colors be used?

Methods

__construct()

Constructor

public __construct() : mixed

Tries to disable colors for non-terminals

disable()

disable color output

public disable() : mixed

enable()

enable color output

public enable() : mixed

getColorCode()

Gets the appropriate terminal code for the given color

public getColorCode(string $color) : string
Parameters
$color : string

one of the available color names

Tags
throws
Exception
Return values
string

color code

isEnabled()

public isEnabled() : bool
Return values
bool

is color support enabled?

ptln()

Convenience function to print a line in a given color

public ptln(string $line, string $color[, resource $channel = STDOUT ]) : mixed
Parameters
$line : string

the line to print, a new line is added automatically

$color : string

one of the available color names

$channel : resource = STDOUT

file descriptor to write to

Tags
throws
Exception

reset()

reset the terminal color

public reset([resource $channel = STDOUT ]) : mixed
Parameters
$channel : resource = STDOUT

file descriptor to write to

Tags
throws
Exception

set()

Set the given color for consecutive output

public set(string $color[, resource $channel = STDOUT ]) : mixed
Parameters
$color : string

one of the supported color names

$channel : resource = STDOUT

file descriptor to write to

Tags
throws
Exception

wrap()

Returns the given text wrapped in the appropriate color and reset code

public wrap(string $text, string $color) : string
Parameters
$text : string

string to wrap

$color : string

one of the available color names

Tags
throws
Exception
Return values
string

the wrapped string


        
On this page

Search results