Skip to content

Commit

Permalink
adding html colors and output for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan Tahler committed May 8, 2011
1 parent f133729 commit c788967
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 36 deletions.
129 changes: 95 additions & 34 deletions API/helper_functions/colors.php
Expand Up @@ -7,52 +7,101 @@ class Colors {

public function __construct() {
// Set up shell 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';
$this->foreground_colors[shell]['black'] = '0;30';
$this->foreground_colors[shell]['dark_gray'] = '1;30';
$this->foreground_colors[shell]['blue'] = '0;34';
$this->foreground_colors[shell]['light_blue'] = '1;34';
$this->foreground_colors[shell]['green'] = '0;32';
$this->foreground_colors[shell]['light_green'] = '1;32';
$this->foreground_colors[shell]['cyan'] = '0;36';
$this->foreground_colors[shell]['light_cyan'] = '1;36';
$this->foreground_colors[shell]['red'] = '0;31';
$this->foreground_colors[shell]['light_red'] = '1;31';
$this->foreground_colors[shell]['purple'] = '0;35';
$this->foreground_colors[shell]['light_purple'] = '1;35';
$this->foreground_colors[shell]['brown'] = '0;33';
$this->foreground_colors[shell]['yellow'] = '1;33';
$this->foreground_colors[shell]['light_gray'] = '0;37';
$this->foreground_colors[shell]['white'] = '1;37';

$this->foreground_colors['bold'] = '1;1';
$this->foreground_colors['underline'] = '1;4';
$this->foreground_colors['blink'] = '5;4';
$this->foreground_colors[shell]['bold'] = '1;1';
$this->foreground_colors[shell]['underline'] = '1;4';
$this->foreground_colors[shell]['blink'] = '5;4';

$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';
$this->background_colors[shell]['black'] = '40';
$this->background_colors[shell]['red'] = '41';
$this->background_colors[shell]['green'] = '42';
$this->background_colors[shell]['yellow'] = '43';
$this->background_colors[shell]['blue'] = '44';
$this->background_colors[shell]['magenta'] = '45';
$this->background_colors[shell]['cyan'] = '46';
$this->background_colors[shell]['light_gray'] = '47';

// Set up html colors (will be within a style= element of a span)
$this->foreground_colors[html]['black'] = 'color:black;';
$this->foreground_colors[html]['dark_gray'] = 'color:dark_gray;';
$this->foreground_colors[html]['blue'] = 'color:blue;';
$this->foreground_colors[html]['light_blue'] = 'color:light_blue;';
$this->foreground_colors[html]['green'] = 'color:green;';
$this->foreground_colors[html]['light_green'] = 'color:light_green;';
$this->foreground_colors[html]['cyan'] = 'color:cyan;';
$this->foreground_colors[html]['light_cyan'] = 'color:light_cyan;';
$this->foreground_colors[html]['red'] = 'color:red;';
$this->foreground_colors[html]['light_red'] = 'color:light_red;';
$this->foreground_colors[html]['purple'] = 'color:purple;';
$this->foreground_colors[html]['light_purple'] = 'color:light_purple;';
$this->foreground_colors[html]['brown'] = 'color:brown;';
$this->foreground_colors[html]['yellow'] = 'color:yellow;';
$this->foreground_colors[html]['light_gray'] = 'color:light_gray;';
$this->foreground_colors[html]['white'] = 'color:white;';

$this->foreground_colors[html]['bold'] = 'font-weight:bold;';
$this->foreground_colors[html]['underline'] = 'text-decoration:underline;';
$this->foreground_colors[html]['blink'] = 'text-decoration:blink;';

$this->background_colors[html]['black'] = 'background-color:black;';
$this->background_colors[html]['red'] = 'background-color:red;';
$this->background_colors[html]['green'] = 'background-color:green;';
$this->background_colors[html]['yellow'] = 'background-color:yellow;';
$this->background_colors[html]['blue'] = 'background-color:blue;';
$this->background_colors[html]['magenta'] = 'background-color:magenta;';
$this->background_colors[html]['cyan'] = 'background-color:cyan;';
$this->background_colors[html]['light_gray'] = 'background-color:light_gray;';
}

// Returns colored string
public function getColoredString($string, $foreground_color = null, $background_color = null) {
$colored_string = "";

$mode = "shell";
if($this->is_server()){$mode = "html";}

// begin
if ($mode == "shell")
{
$colored_string .= "\033[";
}
if ($mode == "html")
{$colored_string .= "<span style=\"";}

// Check if given foreground color found
if (isset($this->foreground_colors[$foreground_color])) {
$colored_string .= "\033[" . $this->foreground_colors[$foreground_color] . "m";
if (isset($this->foreground_colors[$mode][$foreground_color])) {
$colored_string .= $this->foreground_colors[$mode][$foreground_color];
}
// Check if given background color found
if (isset($this->background_colors[$background_color])) {
$colored_string .= "\033[" . $this->background_colors[$background_color] . "m";
if (isset($this->background_colors[$mode][$background_color])) {
$colored_string .= $this->background_colors[$mode][$background_color];
}

// Add string and end coloring
$colored_string .= $string . "\033[0m";

// end
if ($mode == "shell")
{
$colored_string .= "m".$string."\033[0m";
}
if ($mode == "html")
{
$colored_string .= "\">".$string."</span>";
}

return $colored_string;
}
Expand All @@ -66,6 +115,18 @@ public function getForegroundColors() {
public function getBackgroundColors() {
return array_keys($this->background_colors);
}

function is_server()
{
if (isset($_SERVER['SERVER_ADDR']))
{
return true;
}
else
{
return false;
}
}
}

?>
9 changes: 7 additions & 2 deletions SPEC/spec_helper.php
Expand Up @@ -351,15 +351,20 @@ public function log($line)
$line = date("Y-m-d H:i:s")." | ".$line."\r\n";
}

echo $line;

if(isset($CONFIG['TestLog']))
{
$fh = fopen($CONFIG['TestLog'], 'a');
fwrite($fh, $line);
fclose($fh);
}

if (isset($_SERVER['SERVER_ADDR']))
{
$line = str_replace("\r\n","<br />",$line);
$line = str_replace(" ","&nbsp;&nbsp;",$line);
}
echo $line;

return true;
}

Expand Down

0 comments on commit c788967

Please sign in to comment.