Created
January 18, 2015 07:09
-
-
Save Lewiscowles1986/fc43b874bddbc900679c to your computer and use it in GitHub Desktop.
PHP Bash colour re-factor...
This file contains 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 | |
namespace { | |
use \lewiscowles\nix\terminal\bash\TextColorHelper; | |
$bashTxt = new TextColorHelper(); | |
$bashTxt->setString( 'Normal Text' ) | |
->output() | |
->setString( 'Less Normal Text', 'light_purple' ) | |
->output() | |
->setString( 'Success', 'light_green' ) | |
->output() | |
->setString( 'Warning!', 'light_gray', 'yellow' ) | |
->output() | |
->setColor( 'red', TextColorHelper::BG ) | |
->setString( 'Error :(' ) | |
->output() | |
->setString( 'Info!', 'white', 'blue' ) | |
->output() | |
->setString( 'More Weirdness!', 'light_cyan', 'magenta' ) | |
->output(); | |
} | |
namespace lewiscowles\nix\terminal\bash { | |
class TextColorHelper { | |
const FG = 'f'; | |
const BG = 'b'; | |
private static $_fg_colors = [ | |
'black' => '0;30', | |
'dark_gray' => '1;30', | |
'blue' => '0;34', | |
'light_blue' => '1;34', | |
'green' => '0;32', | |
'light_green' => '1;32', | |
'cyan' => '0;36', | |
'light_cyan' => '1;36', | |
'red' => '0;31', | |
'light_red' => '1;31', | |
'purple' => '0;35', | |
'light_purple' => '1;35', | |
'brown' => '0;33', | |
'yellow' => '1;33', | |
'light_gray' => '0;37', | |
'white' => '1;37', | |
]; | |
private static $_bg_colors = [ | |
'black' => '40', | |
'red' => '41', | |
'green' => '42', | |
'yellow' => '43', | |
'blue' => '44', | |
'magenta' => '45', | |
'cyan' => '46', | |
'light_gray' => '47', | |
]; | |
protected $fg = 'white'; | |
protected $bg = 'black'; | |
protected $str = ''; | |
public function __construct( $fgColor=null, $bgColor=null ) { | |
$this->setColors( $fgColor, $bgColor ); | |
} | |
protected function getC( $flag=self::FG ) { | |
$out = ''; | |
switch( $flag ) { | |
case self::FG: | |
if( isset( self::$_fg_colors[ $this->fg ] ) ) { | |
$out = self::$_fg_colors[ $this->fg ]; | |
} else { | |
throw new Exception('Invalid Color String Exception'); | |
} | |
break; | |
case self::BG: | |
if( isset( self::$_bg_colors[ $this->bg ] ) ) { | |
$out = self::$_bg_colors[ $this->bg ]; | |
} else { | |
throw new Exception('Invalid Color String Exception'); | |
} | |
break; | |
} | |
return $out; | |
} | |
// Returns colored string | |
public function setString( $string, $fg_color = null, $bg_color = null ) { | |
$colored_string = ""; // start with a blank string | |
$this->setColor( $fg_color, self::FG ); // override fg if set | |
$this->setColor( $bg_color, self::BG ); // override bg if set | |
$this->str = sprintf( "\033[%sm\033[%sm%s\033[0m", $this->getC( self::FG ), $this->getC( self::BG ), $string ); | |
/* | |
$colored_string .= "\033[" . self::$_fg_colors[ $this->fg ] . "m"; | |
$colored_string .= "\033[" . self::$_bg_colors[ $this->bg ] . "m"; | |
$colored_string .= $string . "\033[0m"; | |
*/ | |
return $this; // chain | |
} | |
public function output() { | |
echo $this."\n"; | |
return $this; | |
} | |
public function __toString() { | |
return $this->str; | |
} | |
public function setColors($fg_color = null, $bg_color = null) { | |
$this->setColor( $fg_color, self::FG ); | |
$this->setColor( $bg_color, self::BG ); | |
return $this; | |
} | |
public function setColor( $color_str, $flag=self::FG ) { | |
if( isset( $color_str ) ) { | |
switch( $flag ) { | |
case self::FG: | |
if( isset( self::$_fg_colors[ strtolower( $color_str ) ] ) ) { | |
$this->fg = strtolower( $color_str ); | |
} else { | |
throw new \Exception('Invalid Color String Exception'); | |
} | |
break; | |
case self::BG: | |
if( isset( self::$_bg_colors[ strtolower( $color_str ) ] ) ) { | |
$this->bg = strtolower( $color_str ); | |
} else { | |
throw new \Exception('Invalid Color String Exception'); | |
} | |
break; | |
} | |
} | |
return $this; | |
} | |
// Returns all foreground color names | |
public static function getForegroundColors() { | |
return array_keys( self::$_fg_colors ); | |
} | |
// Returns all background color names | |
public static function getBackgroundColors() { | |
return array_keys( self::$_bg_colors ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment