Created
September 27, 2016 05:05
-
-
Save jhyland87/69f13b6626fdc80e29174960a56c2531 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @link https://www.if-not-true-then-false.com/2010/php-class-for-coloring-php-command-line-cli-scripts-output-php-output-colorizing-using-bash-shell-colors/ | |
* @author (original) JR | |
* @method Str val( string $val ) Creates new string modifier | |
* @method Str fg( string $fg ) Font foreground color | |
* @method Str bg( string $bg ) Font background color | |
* @method Str st( string $st ) Font style (italic, bold, etc) | |
* @method string __toString() Outputs modified string | |
* @example echo Str::val('Hello World')->fg('red')->bg('light_gray')->st('bold').PHP_EOL; | |
*/ | |
class Str { | |
private $foreground_colors = array(); | |
private $background_colors = array(); | |
private $font_styles = array(); | |
private $_text; | |
private $_result; | |
private $_modifiers = array(); | |
public function __construct( $text ) { | |
$this->_text = $text; | |
// Set up font styles | |
//$this->font_styles['plain'] = '1'; | |
$this->font_styles['bold'] = '1'; | |
$this->font_styles['italic'] = '3'; // "\033[3mtester\e[0m" | |
$this->font_styles['underline'] = '4'; // "\033[4mtester\e[0m" | |
//$this->font_styles['strikethrough'] = ''; | |
//$this->font_styles[''] = ''; | |
//$this->font_styles[''] = ''; | |
// Set up shell font colors | |
$this->foreground_colors['black'] = '0;30'; | |
$this->foreground_colors['dark_gray'] = '1;30'; | |
$this->foreground_colors['blue'] = '0;34'; | |
$this->foreground_colors['light_blue'] = '1;34'; | |
$this->foreground_colors['green'] = '0;32'; | |
$this->foreground_colors['light_green'] = '1;32'; | |
$this->foreground_colors['cyan'] = '0;36'; | |
$this->foreground_colors['light_cyan'] = '1;36'; | |
$this->foreground_colors['red'] = '0;31'; | |
$this->foreground_colors['light_red'] = '1;31'; | |
$this->foreground_colors['purple'] = '0;35'; | |
$this->foreground_colors['light_purple'] = '1;35'; | |
$this->foreground_colors['brown'] = '0;33'; | |
$this->foreground_colors['yellow'] = '1;33'; | |
$this->foreground_colors['light_gray'] = '0;37'; | |
$this->foreground_colors['white'] = '1;37'; | |
// Set up shell background colors | |
$this->background_colors['black'] = '40'; | |
$this->background_colors['red'] = '41'; | |
$this->background_colors['green'] = '42'; | |
$this->background_colors['yellow'] = '43'; | |
$this->background_colors['blue'] = '44'; | |
$this->background_colors['magenta'] = '45'; | |
$this->background_colors['cyan'] = '46'; | |
$this->background_colors['light_gray'] = '47'; | |
} | |
/** | |
* @description Set the value of the string | |
* @param string $val Value of string to modify | |
* @return Str | |
*/ | |
public static function val( $val ){ | |
if( ! $val OR ! is_string( $val ) ) | |
throw new Exception( 'Value needs to be a string' ); | |
return new Str( $val ); | |
} | |
/** | |
* @param string $fg Forground color (Must be a key element in $this->foreground_colors) | |
* @return Str | |
*/ | |
public function fg( $fg ){ | |
if( isset( $this->foreground_colors[ $fg ] ) ) | |
$this->_modifiers[] = $this->foreground_colors[ $fg ]; | |
return $this; | |
} | |
/** | |
* @param string $style Font style (Must be a key element in $this->font_styles) | |
* @return Str | |
*/ | |
public function st( $style ){ | |
if( isset( $this->font_styles[ $style ] ) ) | |
$this->_modifiers[] = $this->font_styles[ $style ]; | |
return $this; | |
} | |
/** | |
* @param string $bg Background color (Must be a key element in $this->background_colors) | |
* @return Str | |
*/ | |
public function bg( $bg ){ | |
if( isset( $this->background_colors[ $bg ] ) ) | |
$this->_modifiers[] = $this->background_colors[ $bg ]; | |
return $this; | |
} | |
/** | |
* @return Formatted string with color modifiers appended | |
*/ | |
public function __toString() | |
{ | |
return "\033[" . join( ';', $this->_modifiers ) . "m" . $this->_text . "\033[0m"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment