mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-08-28 21:46:10 +02:00
MINOR: Replaced usage of renamed classes with the new namespaced name. From: Andrew Short <andrewjshort@gmail.com> git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
81 lines
2.2 KiB
PHP
81 lines
2.2 KiB
PHP
<?php
|
|
/**
|
|
* Class to facilitate command-line output.
|
|
* Support less-trivial output stuff such as colours (on xterm-color)
|
|
*
|
|
* @package sapphire
|
|
* @subpackage dev
|
|
*/
|
|
class SS_Cli 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);
|
|
}
|
|
|
|
/**
|
|
* Return text encoded for CLI output, optionally coloured
|
|
* @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 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,
|
|
'green' => 2,
|
|
'yellow' => 3,
|
|
'blue' => 4,
|
|
'magenta' => 5,
|
|
'cyan' => 6,
|
|
'white' => 7,
|
|
);
|
|
|
|
$prefix = "";
|
|
|
|
if($fgColour || $bold) {
|
|
if(!$fgColour) $fgColour = "white";
|
|
$prefix .= "\033[" . ($bold ? "1;" :"") . "3" . $colours[$fgColour] . "m";
|
|
}
|
|
|
|
|
|
if($bgColour) {
|
|
$prefix .= "\033[4" . $colours[$bgColour] . "m";
|
|
}
|
|
|
|
return $prefix;
|
|
}
|
|
|
|
/**
|
|
* Send control codes for returning to normal colour
|
|
*/
|
|
static function end_colour() {
|
|
return self::supports_colour() ? "\033[0m" : "";
|
|
}
|
|
}
|
|
|
|
?>
|