]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/framework/3rdParty/PhpShell/PHP/Shell/Extensions/Colour.php
Update ReleaseNotes + ChangeLog
[bacula/bacula] / gui / baculum / framework / 3rdParty / PhpShell / PHP / Shell / Extensions / Colour.php
1 <?php
2
3 class PHP_Shell_Extensions_Colour implements PHP_Shell_Extension {
4     static protected $instance;
5     # shell colours
6     const C_RESET = "\033[0m";
7
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";
16
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";
25
26     /**
27     * shell colours
28     *
29     * @var array
30     * @see applyColourScheme
31     */
32     protected $colours;
33
34     /**
35     * shell colour schemes
36     *
37     * @var array
38     * @see registerColourScheme
39     */
40     protected $colour_scheme;
41
42     public function register() {
43         $opt = PHP_Shell_Options::getInstance();
44
45         $opt->registerOption("background", $this, "optSetBackground");
46         $opt->registerOptionAlias("bg", "background");
47
48         $this->registerColourScheme(
49             "plain", array( 
50                 "default"   => "", "value"     => "",
51                 "exception" => "", "reset"     => ""));
52
53         $this->registerColourScheme(
54             "dark", array( 
55                 "default"   => self::C_YELLOW, 
56                 "value"     => self::C_WHITE,
57                 "exception" => self::C_PURPLE));
58
59         $this->registerColourScheme(
60             "light", array( 
61                 "default"   => self::C_BLACK, 
62                 "value"     => self::C_BLUE,
63                 "exception" => self::C_RED));
64
65     }
66
67     /**
68     * background colours
69     */
70     public function optSetBackground($key, $value) {
71         if (is_null($value)) {
72             print(':set '.$key.' needs a colour-scheme, e.g. :set '.$key.'=dark');
73             return;
74         }
75         if (false == $this->applyColourScheme($value)) {
76             print('setting colourscheme failed: colourscheme '.$value.' is unknown');
77             return;
78         }
79     }
80
81     /**
82     * get a colour for the shell
83     *
84     * @param string $type one of (value|exception|reset|default)
85     * @return string a colour string or a empty string
86     */
87     public function getColour($type) {
88         return isset($this->colour[$type]) ? $this->colour[$type] : '';
89     }
90
91     /**
92     * apply a colour scheme to the current shell
93     *
94     * @param string $scheme name of the scheme
95     * @return false if colourscheme is not known, otherwise true
96     */
97     public function applyColourScheme($scheme) {
98         if (!isset($this->colour_scheme[$scheme])) return false;
99
100         $this->colour = $this->colour_scheme[$scheme];
101
102         return true;
103     }
104
105     /**
106     * registers a colour scheme
107     *
108     * @param string $scheme name of the colour scheme
109     * @param array a array of colours
110     */
111     public function registerColourScheme($scheme, $colours) {
112         if (!is_array($colours)) return;
113
114         /* set a reset colour if it is not supplied from the outside */
115         if (!isset($colours["reset"])) $colours["reset"] = self::C_RESET;
116
117         $this->colour_scheme[$scheme] = $colours;
118     }
119 }
120