-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolourfulcli.php
executable file
·136 lines (114 loc) · 2.78 KB
/
colourfulcli.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
/**
* Output colourful lines to the cli
*
* @author P. Prins
*
*/
class Colourfulcli {
// Foreground colours we can use
private static $foreground = array(
'default' => 37,
'black' => 30,
'red' => 31,
'green' => 32,
'yellow' => 33,
'blue' => 34,
'magenta' => 35,
'cyan' => 36,
'white' => 37
);
// Background colours we can use
private static $background = array(
'default' => 40,
'black' => 40,
'red' => 41,
'green' => 42,
'yellow' => 43,
'blue' => 44,
'magenta' => 45,
'cyan' => 46,
'white' => 47
);
// The current foreground
private static $currentForeground = "default";
// The current background
private static $currentBackground = "default";
/**
* Write a line with formatted colours to the stdout
* @param string $text
* @return string
*/
public static function writeLn($text) {
$stdout = fopen("php://stdout", 'w');
fwrite($stdout, self::getString($text));
fclose($stdout);
}
/**
* Format a string with command line colours
* @param string $text
* @return string
*/
public static function getString($text) {
return sprintf("\033[0;%dm\033[%dm%s\033[0m\n", self::$foreground[self::$currentForeground], self::$background[self::$currentBackground], $text);
}
/**
* a simple line mode
* @param string $text
*/
public static function line($text) {
self::setBackground('default');
self::setForeground('default');
self::writeLn($text);
}
/**
* Debug mode
* @param string $text
*/
public static function debug($text) {
self::setBackground('default');
self::setForeground('yellow');
self::writeLn($text);
}
/**
* Comment mode
* @param string $text
*/
public static function comment($text) {
self::setBackground('default');
self::setForeground('green');
self::writeLn($text);
}
/**
* Info mode
* @param string $text
*/
public static function info($text) {
self::setBackground('default');
self::setForeground('green');
self::writeLn($text);
}
/**
* Error mode
* @param string $text
*/
public static function error($text) {
self::setBackground('red');
self::setForeground('white');
self::writeLn($text);
}
/**
* Set background colour
* @param string
*/
public static function setBackground($colour) {
self::$currentBackground = $colour;
}
/**
* Set foreground color
* @param string
*/
public static function setForeground($colour) {
self::$currentForeground = $colour;
}
}