Added colouring to SapphireREPL output

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@67258 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sam Minnee 2008-12-03 01:37:25 +00:00
parent 5c32415664
commit 5dab7fff55
2 changed files with 42 additions and 16 deletions

View File

@ -5,6 +5,9 @@
* Support less-trivial output stuff such as colours (on xterm-color)
*/
class SSCli extends Object {
/**
* Returns true if the current STDOUT supports the use of colour control codes.
*/
static function supports_colour() {
if(!defined('STDOUT')) define('STDOUT', fopen("php://stdout","w"));
return @posix_isatty(STDOUT);
@ -18,7 +21,27 @@ class SSCli extends Object {
*/
static function text($text, $fgColour = null, $bgColour = null, $bold = false) {
if(!self::supports_colour()) return $text;
if($fgColour || $bgColour || $bold) {
$prefix = self::start_colour($fgColour, $bgColour, $bold);
$suffix = self::end_colour();
} else {
$prefix = $suffix = "";
}
return $prefix . $text . $suffix;
}
/**
* Send control codes for changing text to the given colour
* @param string $fgColour The foreground colour - black, red, green, yellow, blue, magenta, cyan, white. Null is default.
* @param string $bgColour The foreground colour - black, red, green, yellow, blue, magenta, cyan, white. Null is default.
* @param string $bold A boolean variable - bold or not.
*/
static function start_colour($fgColour = null, $bgColour = null, $bold = false) {
if(!self::supports_colour()) return "";
$colours = array(
'black' => 0,
'red' => 1,
@ -30,25 +53,26 @@ class SSCli extends Object {
'white' => 7,
);
$prefix = $suffix = "";
$prefix = "";
if($fgColour || $bgColour || $bold) {
$suffix .= "\033[0m";
if($fgColour || $bold) {
if(!$fgColour) $fgColour = "white";
$prefix .= "\033[" . ($bold ? "1;" :"") . "3" . $colours[$fgColour] . "m";
}
if($fgColour || $bold) {
if(!$fgColour) $fgColour = "white";
$prefix .= "\033[" . ($bold ? "1;" :"") . "3" . $colours[$fgColour] . "m";
}
if($bgColour) {
$prefix .= "\033[4" . $colours[$bgColour] . "m";
}
if($bgColour) {
$prefix .= "\033[4" . $colours[$bgColour] . "m";
}
return $prefix . $text . $suffix;
return $prefix;
}
/**
* Send control codes for returning to normal colour
*/
static function end_colour() {
return self::supports_colour() ? "\033[0m" : "";
}
}

View File

@ -6,8 +6,10 @@ class SapphireREPL extends Controller {
echo "Sapphire Interactive Command-line (REPL interface)\n\n";
while(true) {
echo "?> ";
echo SSCli::text("?> ", "cyan");
echo SSCli::start_colour("yellow");
$command = trim(fgets(STDIN, 4096));
echo SSCli::end_colour();
// Simple processing
if(substr($command,-1) == ';') $command = substr($command,0,-1);