3 class PHP_Shell_Extensions_Colour implements PHP_Shell_Extension {
4 static protected $instance;
6 const C_RESET = "\033[0m";
8 const C_BLACK = "\033[0;30m";
9 const C_RED = "\033[0;31m";
10 const C_GREEN = "\033[0;32m";
11 const C_BROWN = "\033[0;33m";
12 const C_BLUE = "\033[0;34m";
13 const C_PURPLE = "\033[0;35m";
14 const C_CYAN = "\033[0;36m";
15 const C_LIGHT_GRAY = "\033[0;37m";
17 const C_GRAY = "\033[1;30m";
18 const C_LIGHT_RED = "\033[1;31m";
19 const C_LIGHT_GREEN = "\033[1;32m";
20 const C_YELLOW = "\033[1;33m";
21 const C_LIGHT_BLUE = "\033[1;34m";
22 const C_LIGHT_PURPLE = "\033[1;35m";
23 const C_LIGHT_CYAN = "\033[1;36m";
24 const C_WHITE = "\033[1;37m";
30 * @see applyColourScheme
35 * shell colour schemes
38 * @see registerColourScheme
40 protected $colour_scheme;
42 public function register() {
43 $opt = PHP_Shell_Options::getInstance();
45 $opt->registerOption("background", $this, "optSetBackground");
46 $opt->registerOptionAlias("bg", "background");
48 $this->registerColourScheme(
50 "default" => "", "value" => "",
51 "exception" => "", "reset" => ""));
53 $this->registerColourScheme(
55 "default" => self::C_YELLOW,
56 "value" => self::C_WHITE,
57 "exception" => self::C_PURPLE));
59 $this->registerColourScheme(
61 "default" => self::C_BLACK,
62 "value" => self::C_BLUE,
63 "exception" => self::C_RED));
70 public function optSetBackground($key, $value) {
71 if (is_null($value)) {
72 print(':set '.$key.' needs a colour-scheme, e.g. :set '.$key.'=dark');
75 if (false == $this->applyColourScheme($value)) {
76 print('setting colourscheme failed: colourscheme '.$value.' is unknown');
82 * get a colour for the shell
84 * @param string $type one of (value|exception|reset|default)
85 * @return string a colour string or a empty string
87 public function getColour($type) {
88 return isset($this->colour[$type]) ? $this->colour[$type] : '';
92 * apply a colour scheme to the current shell
94 * @param string $scheme name of the scheme
95 * @return false if colourscheme is not known, otherwise true
97 public function applyColourScheme($scheme) {
98 if (!isset($this->colour_scheme[$scheme])) return false;
100 $this->colour = $this->colour_scheme[$scheme];
106 * registers a colour scheme
108 * @param string $scheme name of the colour scheme
109 * @param array a array of colours
111 public function registerColourScheme($scheme, $colours) {
112 if (!is_array($colours)) return;
114 /* set a reset colour if it is not supplied from the outside */
115 if (!isset($colours["reset"])) $colours["reset"] = self::C_RESET;
117 $this->colour_scheme[$scheme] = $colours;