-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCli.php
More file actions
191 lines (170 loc) · 5.54 KB
/
Cli.php
File metadata and controls
191 lines (170 loc) · 5.54 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
namespace Virakal\Cli;
/**
* Functions designed to make writing PHP command line scripts simpler.
*
* Currently these are all targeted towards UNIX-y systems.
*
* Interface likely to change drastically.
*
* @author Jonathan Goodger <jonno.is@gmail.com>
* @license GPL3
*/
class Cli
{
/** Escape pattern to begin a colour control code */
const ESCAPE_PATTERN = "\033[%sm";
/** Escape code to reset to original colours */
const COLOUR_RESET = "\033[0m";
/**
* @staticvar array a list of codes for changing the foreground colour
*/
public static $foregroundColours = array(
'black' => '0;30',
'blue' => '0;34',
'brown' => '0;33',
'cyan' => '0;36',
'darkGray' => '1;30',
'darkGrey' => '1;30',
'green' => '0;32',
'lightBlue' => '1;34',
'lightCyan' => '1;36',
'lightGray' => '0;37',
'lightGreen' => '1;32',
'lightGrey' => '0;37',
'lightPurple' => '1;35',
'lightRed' => '1;31',
'purple' => '0;35',
'red' => '0;31',
'white' => '1;37',
'yellow' => '1;33',
);
/**
* @staticvar array a list of codes for changing the background colour
*/
public static $backgroundColours = array(
'black' => '40',
'blue' => '44',
'cyan' => '46',
'green' => '42',
'grey' => '47',
'lightGray' => '47',
'lightGrey' => '47',
'magenta' => '45',
'red' => '41',
'yellow' => '43',
);
/**
* @staticvar array a list of codes for changing the text formatting
*/
public static $formatCodes = array(
'blink' => '5',
'bold' => '1',
'dim' => '2',
'hidden' => '8',
'reverse' => '7',
'underline' => '4',
);
/**
* Format the given text.
*
* Notes:
* - Some format options don't work together
* - Output is largely dependent on the terminal and its settings
* - Aliased as `color`
*
* @access public
* @static
* @author Jonathan Goodger <jonno.is@gmail.com>
* @param string $text the text to format
* @param string $foreground the foreground colour or null for none (default: null)
* @param string $background the background colour or null for none (default: null)
* @param string|array $format an additional formatting options, or an array of them
* @return string
*/
public static function colour($text, $foreground=null, $background=null, $format=array())
{
return self::getForegroundCode($foreground)
. self::getBackgroundCode($background)
. implode(array_map(function ($x) {return self::getFormatCode($x);}, $format))
. $text . self::getColourReset();
}
/**
* Format the given text and echo it.
*
* @see colour for argument docs
* @access public
* @static
* @author Jonathan Goodger <jonno.is@gmail.com>
*/
public static function say($text, $foreground=null, $background=null, $format=array())
{
$ret = self::colour($text, $foreground, $background, $format) . PHP_EOL;
echo $ret;
return $ret;
}
/**
* @see colour an alias
* @access public
* @static
* @author Jonathan Goodger <jonno.is@gmail.com>
*/
public static function color($text, $foreground=null, $background=null, $format=array())
{
return self::colour($text, $foreground, $background, $format);
}
public static function getForegroundCode($colour=null)
{
if (isset(self::$foregroundColours[$colour])) {
return sprintf(self::ESCAPE_PATTERN, self::$foregroundColours[$colour]);
}
return '';
}
public static function getBackgroundCode($colour=null)
{
if (isset(self::$backgroundColours[$colour])) {
return sprintf(self::ESCAPE_PATTERN, self::$backgroundColours[$colour]);
}
return '';
}
public static function getFormatCode($format=null)
{
if (isset(self::$formatCodes[$format])) {
return sprintf(self::ESCAPE_PATTERN, self::$formatCodes[$format]);
}
return '';
}
public static function getColourReset()
{
return self::COLOUR_RESET;
}
/**
* Sound the system bell.
*
* @access public
* @static
* @author Jonathan Goodger <jonno.is@gmail.com>
* @param integer $repeat number of times to repeat the bell (default: 1)
* @return void
*/
public static function bell($repeat=1) {
echo str_repeat("\007", $repeat);
}
/**
* Execute a shell command, displaying the output and allowing the user to interact.
*
* @access public
* @static
* @author Jonathan Goodger <jonno.is@gmail.com>
* @param string $cmd the command to run
* @param string $cwd the working directory for the command (default: the current working directory)
* @param array $env an array of environment variables (default: the current system environment)
* @return integer
* @see proc_open for more details on arguments
*/
public static function execInteractive($cmd, $cwd=null, $env=null)
{
$proc = proc_open($cmd, array(STDIN, STDOUT, STDERR), $pipes, $cwd, $env);
return proc_close($proc);
}
}