Created
June 12, 2012 15:16
-
-
Save filp/2918129 to your computer and use it in GitHub Desktop.
wat is this
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 | |
// this is not good. | |
class ColorString | |
{ | |
protected $string; | |
protected static $codes = [ | |
'reset' => 0, | |
'bright' => 1, | |
'bold' => 1, | |
'dim' => 2, | |
'underline' => 4, | |
'underlined' => 4, | |
'blinking' => 5, | |
'blink' => 5, | |
'reverse' => 7, | |
'reversed' => 7, | |
'hidden' => 8, | |
'black' => 30, | |
'red' => 31, | |
'green' => 32, | |
'yellow' => 33, | |
'blue' => 34, | |
'magenta' => 35, | |
'cyan' => 36, | |
'white' => 37 | |
]; | |
public function __construct($string = "") | |
{ | |
$this->string = $string; | |
} | |
public function __get($property) | |
{ | |
$segments = explode('_', $property); | |
$map = []; | |
$adder = 0; | |
foreach($segments as $segment) { | |
if($segment == 'on') | |
$adder = 10; | |
else { | |
if(!isset(self::$codes[$segment])) | |
continue; | |
$code = self::$codes[$segment]; | |
if($adder > 0) { | |
$map[]= $code + $adder; | |
break; | |
} | |
$map[] = $code; | |
} | |
} | |
return new ColorString($this->wrapMap($map)); | |
} | |
protected function wrapMap($map = array()) | |
{ | |
return ("\033[" . join(';', $map) . 'm') . $this->string . "\033[0m"; | |
} | |
public function __toString() | |
{ | |
return $this->string; | |
} | |
} | |
$s = new ColorString('Hello, world!'); | |
print $s->bold_red_on_white . "\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment