mako\CLI


Description


Helper class for working with CLI.


Class methods


Toggle source

protected __construct()


Protected constructor since this is a static class.

Return value

NULL

protected function __construct()
{
	// Nothing here
}

Toggle source

public screenSize()


Returns the screen size.

Return value

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;
}

Toggle source

public static color($str, $textColor = NULL, $backgroundColor = NULL, $textOptions = array ( ))


Add text color and background color to a string.


Parameters

Type Description
string String to colorize
string (optional) Text color name
string (optional) Background color name
array (optional) Text options
Return value

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);
}

Toggle source

public static param($name, $default = NULL)


Return value of named parameters (--<name>=<value>).


Parameters

Type Description
string Parameter name
string (optional) Default value
Return 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;
}

Toggle source

public static input($question)


Prompt user for input.


Parameters

Type Description
string Question for the user
Return value

string

public static function input($question)
{
	fwrite(STDOUT, $question . ': ');

	return trim(fgets(STDIN));
}

Toggle source

public static confirm($question)


Prompt user a confirmation.


Parameters

Type Description
string Question for the user
Return value

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);
	}
}

Toggle source

public static stdout($message = '', $textColor = NULL, $backgroundColor = NULL, $textOptions = array ( ))


Print message to STDOUT.


Parameters

Type Description
string (optional) Message to print
string (optional) Text color
string (optional) Background color
array (optional) Text options
Return value

NULL

public static function stdout($message = '', $textColor = null, $backgroundColor = null, array $textOptions = array())
{
	fwrite(STDOUT, static::color($message, $textColor, $backgroundColor, $textOptions) . PHP_EOL);
}

Toggle source

public static stderr($message, $textColor = 'red', $backgroundColor = NULL, $textOptions = array ( ))


Print message to STDERR.


Parameters

Type Description
string Message to print
string (optional) Text color
string (optional) Background color
array (optional) Text options
Return value

NULL

public static function stderr($message, $textColor = 'red', $backgroundColor = null, array $textOptions = array())
{
	fwrite(STDERR, static::color($message, $textColor, $backgroundColor, $textOptions) . PHP_EOL);	
}

Toggle source

public static beep($beeps = 1)


Sytem Beep.


Parameters

Type Description
int (optional) Number of system beeps
Return value

NULL

public static function beep($beeps = 1)
{
	fwrite(STDOUT, str_repeat("\x07", $beeps));
}

Toggle source

public static wait($seconds = 5, $withBeep = false)


Display countdown for n seconds.


Parameters

Type Description
int Number of seconds to wait
boolean (optional) Enable beep?
Return value

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");
}