Helper class for working with CLI.
Protected constructor since this is a static class.
NULL
protected function __construct()
{
// Nothing here
}
Returns the screen size.
array
public function screenSize()
{
$size = array('width' => 0, 'height' => 0);
if(function_exists('ncurses_getmaxyx'))
{
ncurses_getmaxyx(STDSCR, $size['height'], $size['width']);
}
else
{
if(!MAKO_IS_WINDOWS)
{
$size['width'] = exec('tput cols');
$size['height'] = exec('tput lines');
}
}
return $size;
}
Add text color and background color to a string.
| Type | Description |
|---|---|
| string | String to colorize |
| string | (optional) Text color name |
| string | (optional) Background color name |
| array | (optional) Text options |
string
public static function color($str, $textColor = null, $backgroundColor = null, array $textOptions = array())
{
if(MAKO_IS_WINDOWS)
{
return $str;
}
$style = array();
// Font color
if($textColor !== null)
{
if(!isset(static::$textColors[$textColor]))
{
throw new RuntimeException(vsprintf("%s(): Invalid text color. Only the following colors are valid: %s.", array
(
__METHOD__,
implode(', ', array_keys(static::$textColors))
)));
}
$style[] = static::$textColors[$textColor];
}
// Background color
if($backgroundColor !== null)
{
if(!isset(static::$backgroundColors[$backgroundColor]))
{
throw new RuntimeException(vsprintf("%s(): Invalid background color. Only the following colors are valid: %s.", array
(
__METHOD__,
implode(', ', array_keys(static::$backgroundColors))
)));
}
$style[] = static::$backgroundColors[$backgroundColor];
}
// Text options
if($textOptions !== null)
{
$options = array();
foreach($textOptions as $option)
{
if(!isset(static::$textOptions[$option]))
{
throw new RuntimeException(vsprintf("%s(): Invalid text option. Only the following options are valid: %s.", array
(
__METHOD__,
implode(', ', array_keys(static::$textOptions))
)));
}
$options[] = static::$textOptions[$option];
}
$style = array_merge($style, $options);
}
// Wrap text in style "tags"
return sprintf("\033[%sm%s\033[0m", implode(';', $style), $str);
}
Return value of named parameters (--<name>=<value>).
| Type | Description |
|---|---|
| string | Parameter name |
| string | (optional) Default value |
string
public static function param($name, $default = null)
{
static $parameters = false;
// Only parse parameters once
if($parameters === false)
{
$parameters = array();
foreach($_SERVER['argv'] as $arg)
{
if(substr($arg, 0, 2) === '--')
{
$arg = explode('=', substr($arg, 2), 2);
$parameters[$arg[0]] = isset($arg[1]) ? $arg[1] : true;
}
}
}
return isset($parameters[$name]) ? $parameters[$name] : $default;
}
Prompt user for input.
| Type | Description |
|---|---|
| string | Question for the user |
string
public static function input($question)
{
fwrite(STDOUT, $question . ': ');
return trim(fgets(STDIN));
}
Prompt user a confirmation.
| Type | Description |
|---|---|
| string | Question for the user |
boolean
public static function confirm($question)
{
fwrite(STDOUT, $question . ' [' . I18n::translate('Y') . '/' . I18n::translate('N') . ']: ');
$input = trim(fgets(STDIN));
switch(mb_strtoupper($input))
{
case I18n::translate('Y'):
return true;
break;
case I18n::translate('N'):
return false;
break;
default:
return static::confirm($question);
}
}
Print message to STDOUT.
| Type | Description |
|---|---|
| string | (optional) Message to print |
| string | (optional) Text color |
| string | (optional) Background color |
| array | (optional) Text options |
NULL
public static function stdout($message = '', $textColor = null, $backgroundColor = null, array $textOptions = array())
{
fwrite(STDOUT, static::color($message, $textColor, $backgroundColor, $textOptions) . PHP_EOL);
}
Print message to STDERR.
| Type | Description |
|---|---|
| string | Message to print |
| string | (optional) Text color |
| string | (optional) Background color |
| array | (optional) Text options |
NULL
public static function stderr($message, $textColor = 'red', $backgroundColor = null, array $textOptions = array())
{
fwrite(STDERR, static::color($message, $textColor, $backgroundColor, $textOptions) . PHP_EOL);
}
Sytem Beep.
| Type | Description |
|---|---|
| int | (optional) Number of system beeps |
NULL
public static function beep($beeps = 1)
{
fwrite(STDOUT, str_repeat("\x07", $beeps));
}
Display countdown for n seconds.
| Type | Description |
|---|---|
| int | Number of seconds to wait |
| boolean | (optional) Enable beep? |
NULL
public static function wait($seconds = 5, $withBeep = false)
{
$length = strlen($seconds);
while($seconds > 0)
{
fwrite(STDOUT, "\r" . I18n::translate('Please wait ...') . ' [ ' . str_pad($seconds--, $length, 0, STR_PAD_LEFT) . ' ]');
if($withBeep === true)
{
static::beep();
}
sleep(1);
}
fwrite(STDOUT, "\r\033[0K");
}