silverstripe-framework/src/Dev/CLI.php

94 lines
2.5 KiB
PHP
Raw Normal View History

<?php
namespace SilverStripe\Dev;
use SilverStripe\Core\Object;
/**
* Class to facilitate command-line output.
* Support less-trivial output stuff such as colours (on xterm-color)
*/
2016-09-09 08:43:05 +02:00
class CLI extends Object {
/**
* Returns true if the current STDOUT supports the use of colour control codes.
*/
public static function supports_colour() {
// Special case for buildbot
if(isset($_ENV['_']) && strpos($_ENV['_'],'buildbot') !== false) return false;
if(!defined('STDOUT')) define('STDOUT', fopen("php://stdout","w"));
return function_exists('posix_isatty') ? @posix_isatty(STDOUT) : false;
}
2014-08-15 08:53:05 +02:00
/**
* Return text encoded for CLI output, optionally coloured
*
* @param string $text
* @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 bool $bold A boolean variable - bold or not.
* @return string
*/
public static function text($text, $fgColour = null, $bgColour = null, $bold = false) {
if(!self::supports_colour()) return $text;
2014-08-15 08:53:05 +02:00
if($fgColour || $bgColour || $bold) {
$prefix = self::start_colour($fgColour, $bgColour, $bold);
$suffix = self::end_colour();
} else {
$prefix = $suffix = "";
}
2014-08-15 08:53:05 +02:00
return $prefix . $text . $suffix;
2014-08-15 08:53:05 +02: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 bool $bold A boolean variable - bold or not.
* @return string
*/
public 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,
);
2014-08-15 08:53:05 +02:00
$prefix = "";
if($fgColour || $bold) {
if(!$fgColour) $fgColour = "white";
$prefix .= "\033[" . ($bold ? "1;" :"") . "3" . $colours[$fgColour] . "m";
}
2014-08-15 08:53:05 +02:00
if($bgColour) {
$prefix .= "\033[4" . $colours[$bgColour] . "m";
}
2014-08-15 08:53:05 +02:00
return $prefix;
}
2014-08-15 08:53:05 +02:00
/**
* Send control codes for returning to normal colour
*/
public static function end_colour() {
return self::supports_colour() ? "\033[0m" : "";
}
}