2008-10-04 07:41:15 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Class to facilitate command-line output.
|
|
|
|
* Support less-trivial output stuff such as colours (on xterm-color)
|
2009-03-22 23:59:14 +01:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2009-03-22 23:59:14 +01:00
|
|
|
* @subpackage dev
|
2008-10-04 07:41:15 +02:00
|
|
|
*/
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
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
2009-10-26 04:06:31 +01:00
|
|
|
class SS_Cli extends Object {
|
2008-12-03 02:37:25 +01:00
|
|
|
/**
|
|
|
|
* Returns true if the current STDOUT supports the use of colour control codes.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function supports_colour() {
|
2010-10-12 23:52:19 +02:00
|
|
|
// Special case for buildbot
|
|
|
|
if(isset($_ENV['_']) && strpos($_ENV['_'],'buildbot') !== false) return false;
|
|
|
|
|
2008-10-13 03:40:42 +02:00
|
|
|
if(!defined('STDOUT')) define('STDOUT', fopen("php://stdout","w"));
|
2010-05-25 06:18:00 +02:00
|
|
|
return function_exists('posix_isatty') ? @posix_isatty(STDOUT) : false;
|
2008-10-09 02:49:53 +02:00
|
|
|
}
|
|
|
|
|
2008-10-04 07:41:15 +02:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function text($text, $fgColour = null, $bgColour = null, $bold = false) {
|
2008-10-09 02:49:53 +02:00
|
|
|
if(!self::supports_colour()) return $text;
|
2008-12-03 02:37:25 +01:00
|
|
|
|
|
|
|
if($fgColour || $bgColour || $bold) {
|
|
|
|
$prefix = self::start_colour($fgColour, $bgColour, $bold);
|
|
|
|
$suffix = self::end_colour();
|
|
|
|
} else {
|
|
|
|
$prefix = $suffix = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
return $prefix . $text . $suffix;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2008-10-04 07:41:15 +02:00
|
|
|
|
2008-12-03 02:37:25 +01:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function start_colour($fgColour = null, $bgColour = null, $bold = false) {
|
2008-12-03 02:37:25 +01:00
|
|
|
if(!self::supports_colour()) return "";
|
2008-10-04 07:41:15 +02:00
|
|
|
$colours = array(
|
|
|
|
'black' => 0,
|
|
|
|
'red' => 1,
|
|
|
|
'green' => 2,
|
|
|
|
'yellow' => 3,
|
|
|
|
'blue' => 4,
|
|
|
|
'magenta' => 5,
|
|
|
|
'cyan' => 6,
|
|
|
|
'white' => 7,
|
|
|
|
);
|
|
|
|
|
2008-12-03 02:37:25 +01:00
|
|
|
$prefix = "";
|
2008-10-04 07:41:15 +02:00
|
|
|
|
2008-12-03 02:37:25 +01:00
|
|
|
if($fgColour || $bold) {
|
|
|
|
if(!$fgColour) $fgColour = "white";
|
|
|
|
$prefix .= "\033[" . ($bold ? "1;" :"") . "3" . $colours[$fgColour] . "m";
|
|
|
|
}
|
|
|
|
|
2008-10-04 07:41:15 +02:00
|
|
|
|
2008-12-03 02:37:25 +01:00
|
|
|
if($bgColour) {
|
|
|
|
$prefix .= "\033[4" . $colours[$bgColour] . "m";
|
2008-10-04 07:41:15 +02:00
|
|
|
}
|
|
|
|
|
2008-12-03 02:37:25 +01:00
|
|
|
return $prefix;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send control codes for returning to normal colour
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function end_colour() {
|
2008-12-03 02:37:25 +01:00
|
|
|
return self::supports_colour() ? "\033[0m" : "";
|
2008-10-04 07:41:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|