Version: current

     1 <?php
     2 /**
     3  * Class to facilitate command-line output.
     4  * Support less-trivial output stuff such as colours (on xterm-color)
     5  *
     6  * @package sapphire
     7  * @subpackage dev
     8  */
     9 class SS_Cli extends Object {
    10     /**
    11      * Returns true if the current STDOUT supports the use of colour control codes.
    12      */
    13     static function supports_colour() {
    14         // Special case for buildbot
    15         if(isset($_ENV['_']) && strpos($_ENV['_'],'buildbot') !== false) return false;
    16
    17         if(!defined('STDOUT')) define('STDOUT', fopen("php://stdout","w"));
    18         return function_exists('posix_isatty') ? @posix_isatty(STDOUT) : false;
    19     }
    20
    21     /**
    22      * Return text encoded for CLI output, optionally coloured
    23      * @param string $fgColour The foreground colour - black, red, green, yellow, blue, magenta, cyan, white.  Null is default.
    24      * @param string $bgColour The foreground colour - black, red, green, yellow, blue, magenta, cyan, white.  Null is default.
    25      * @param string $bold A boolean variable - bold or not.
    26      */
    27     static function text($text, $fgColour = null, $bgColour = null, $bold = false) {
    28         if(!self::supports_colour()) return $text;
    29
    30         if($fgColour || $bgColour || $bold) {
    31             $prefix = self::start_colour($fgColour, $bgColour, $bold);
    32             $suffix = self::end_colour();
    33         } else {
    34             $prefix = $suffix = "";
    35         }
    36
    37         return $prefix . $text . $suffix;
    38
    39
    40     }
    41
    42     /**
    43      * Send control codes for changing text to the given colour
    44      * @param string $fgColour The foreground colour - black, red, green, yellow, blue, magenta, cyan, white.  Null is default.
    45      * @param string $bgColour The foreground colour - black, red, green, yellow, blue, magenta, cyan, white.  Null is default.
    46      * @param string $bold A boolean variable - bold or not.
    47      */
    48     static function start_colour($fgColour = null, $bgColour = null, $bold = false) {
    49         if(!self::supports_colour()) return "";
    50         $colours = array(
    51             'black' => 0,
    52             'red' => 1,
    53             'green' => 2,
    54             'yellow' => 3,
    55             'blue' => 4,
    56             'magenta' => 5,
    57             'cyan' => 6,
    58             'white' => 7,
    59         );
    60
    61         $prefix = "";
    62
    63         if($fgColour || $bold) {
    64             if(!$fgColour) $fgColour = "white";
    65             $prefix .= "\033[" . ($bold ? "1;" :"") . "3" . $colours[$fgColour] . "m";
    66         }
    67
    68
    69         if($bgColour) {
    70             $prefix .= "\033[4" . $colours[$bgColour] . "m";
    71         }
    72
    73         return $prefix;
    74     }
    75
    76     /**
    77      * Send control codes for returning to normal colour
    78      */
    79     static function end_colour() {
    80         return self::supports_colour() ? "\033[0m" : "";
    81     }
    82 }
    83
    84 ?>

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -