mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-08-27 04:56:02 +02:00
API CHANGE: Added SSCli class to help with outputting coloured text on the command line git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@63637 467b73ca-7a2a-4603-9d3b-597d59a354a9
50 lines
1.2 KiB
PHP
50 lines
1.2 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Class to facilitate command-line output.
|
|
* Support less-trivial output stuff such as colours (on xterm-color)
|
|
*/
|
|
class SSCli extends Object {
|
|
/**
|
|
* 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(!isset($_SERVER['TERM']) || $_SERVER['TERM'] != 'xterm-color') return $text;
|
|
|
|
$colours = array(
|
|
'black' => 0,
|
|
'red' => 1,
|
|
'green' => 2,
|
|
'yellow' => 3,
|
|
'blue' => 4,
|
|
'magenta' => 5,
|
|
'cyan' => 6,
|
|
'white' => 7,
|
|
);
|
|
|
|
$prefix = $suffix = "";
|
|
|
|
if($fgColour || $bgColour || $bold) {
|
|
$suffix .= "\033[0m";
|
|
|
|
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 . $text . $suffix;
|
|
|
|
|
|
}
|
|
}
|
|
|
|
?>
|