1 <?php
2 3 4 5 6 7 8
9 class SS_Cli extends Object {
10 11 12
13 static function supports_colour() {
14
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 23 24 25 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 44 45 46 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 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.
-