Update console features
This commit is contained in:
parent
f8a845527e
commit
f56bc7952a
3 changed files with 64 additions and 58 deletions
|
@ -2,48 +2,48 @@
|
|||
|
||||
namespace Cli\Assets;
|
||||
|
||||
class Colors {
|
||||
trait Colors {
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private static $foreground_colors = [];
|
||||
private $foreground_colors = [];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private static $background_colors = [];
|
||||
private $background_colors = [];
|
||||
|
||||
/**
|
||||
* Colors constructor.
|
||||
*/
|
||||
private static function setColors()
|
||||
private function setColors()
|
||||
{
|
||||
self::$foreground_colors['black'] = '0;30';
|
||||
self::$foreground_colors['dark_gray'] = '1;30';
|
||||
self::$foreground_colors['blue'] = '0;34';
|
||||
self::$foreground_colors['light_blue'] = '1;34';
|
||||
self::$foreground_colors['green'] = '0;32';
|
||||
self::$foreground_colors['light_green'] = '1;32';
|
||||
self::$foreground_colors['cyan'] = '0;36';
|
||||
self::$foreground_colors['light_cyan'] = '1;36';
|
||||
self::$foreground_colors['red'] = '0;31';
|
||||
self::$foreground_colors['light_red'] = '1;31';
|
||||
self::$foreground_colors['purple'] = '0;35';
|
||||
self::$foreground_colors['light_purple'] = '1;35';
|
||||
self::$foreground_colors['brown'] = '0;33';
|
||||
self::$foreground_colors['yellow'] = '1;33';
|
||||
self::$foreground_colors['light_gray'] = '0;37';
|
||||
self::$foreground_colors['white'] = '1;37';
|
||||
$this->foreground_colors['black'] = '0;30';
|
||||
$this->foreground_colors['dark_gray'] = '1;30';
|
||||
$this->foreground_colors['blue'] = '0;34';
|
||||
$this->foreground_colors['light_blue'] = '1;34';
|
||||
$this->foreground_colors['green'] = '0;32';
|
||||
$this->foreground_colors['light_green'] = '1;32';
|
||||
$this->foreground_colors['cyan'] = '0;36';
|
||||
$this->foreground_colors['light_cyan'] = '1;36';
|
||||
$this->foreground_colors['red'] = '0;31';
|
||||
$this->foreground_colors['light_red'] = '1;31';
|
||||
$this->foreground_colors['purple'] = '0;35';
|
||||
$this->foreground_colors['light_purple'] = '1;35';
|
||||
$this->foreground_colors['brown'] = '0;33';
|
||||
$this->foreground_colors['yellow'] = '1;33';
|
||||
$this->foreground_colors['light_gray'] = '0;37';
|
||||
$this->foreground_colors['white'] = '1;37';
|
||||
|
||||
self::$background_colors['black'] = '40';
|
||||
self::$background_colors['red'] = '41';
|
||||
self::$background_colors['green'] = '42';
|
||||
self::$background_colors['yellow'] = '43';
|
||||
self::$background_colors['blue'] = '44';
|
||||
self::$background_colors['magenta'] = '45';
|
||||
self::$background_colors['cyan'] = '46';
|
||||
self::$background_colors['light_gray'] = '47';
|
||||
$this->background_colors['black'] = '40';
|
||||
$this->background_colors['red'] = '41';
|
||||
$this->background_colors['green'] = '42';
|
||||
$this->background_colors['yellow'] = '43';
|
||||
$this->background_colors['blue'] = '44';
|
||||
$this->background_colors['magenta'] = '45';
|
||||
$this->background_colors['cyan'] = '46';
|
||||
$this->background_colors['light_gray'] = '47';
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -52,18 +52,18 @@ class Colors {
|
|||
* @param null $background_color
|
||||
* @return string
|
||||
*/
|
||||
public static function getColoredString($string, $foreground_color = null, $background_color = null)
|
||||
public function printColor($string, $foreground_color = null, $background_color = null)
|
||||
{
|
||||
self::setColors();
|
||||
$this->setColors();
|
||||
|
||||
$colored_string = "";
|
||||
|
||||
if (isset(self::$foreground_colors[$foreground_color])) {
|
||||
$colored_string .= "\033[" . self::$foreground_colors[$foreground_color] . "m";
|
||||
if (isset($this->foreground_colors[$foreground_color])) {
|
||||
$colored_string .= "\033[" . $this->foreground_colors[$foreground_color] . "m";
|
||||
}
|
||||
|
||||
if (isset(self::$background_colors[$background_color])) {
|
||||
$colored_string .= "\033[" . self::$background_colors[$background_color] . "m";
|
||||
if (isset($this->background_colors[$background_color])) {
|
||||
$colored_string .= "\033[" . $this->background_colors[$background_color] . "m";
|
||||
}
|
||||
|
||||
|
||||
|
@ -71,22 +71,4 @@ class Colors {
|
|||
|
||||
return $colored_string;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getForegroundColors()
|
||||
{
|
||||
self::setColors();
|
||||
return array_keys(self::$foreground_colors);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getBackgroundColors()
|
||||
{
|
||||
self::setColors();
|
||||
return array_keys(self::$background_colors);
|
||||
}
|
||||
}
|
|
@ -4,6 +4,7 @@ namespace Cli\Assets;
|
|||
|
||||
trait Console
|
||||
{
|
||||
use Colors;
|
||||
|
||||
/**
|
||||
* @var false|resource
|
||||
|
@ -12,14 +13,18 @@ trait Console
|
|||
|
||||
/**
|
||||
* @param string $message
|
||||
* @param null $color
|
||||
* @param string $background
|
||||
*/
|
||||
public function print(string $message, $color = null, $background = '')
|
||||
{
|
||||
echo Colors::getColoredString($message, $color, $background);
|
||||
echo $this->printColor($message, $color, $background);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
* @param null $color
|
||||
* @param string $background
|
||||
*/
|
||||
public function printLn(string $message, $color = null, $background = '')
|
||||
{
|
||||
|
@ -35,14 +40,30 @@ trait Console
|
|||
$this->printLn($message, 'green');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
*/
|
||||
public function warning(string $message)
|
||||
{
|
||||
$this->printLn($message, 'brown');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
*/
|
||||
public function error(string $message)
|
||||
{
|
||||
$this->printLn($message, 'white', 'red');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $question
|
||||
* @return string
|
||||
*/
|
||||
public function ask($question)
|
||||
{
|
||||
$this->success($question);
|
||||
$this->print("\n > ", 'yellow');
|
||||
$this->success($question );
|
||||
$this->print(" > ", 'yellow');
|
||||
|
||||
return $this->getLine();
|
||||
}
|
||||
|
|
|
@ -22,13 +22,16 @@ class Commands
|
|||
$this->args = $args;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle console command
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$name = $this->ask('What is your name? ');
|
||||
|
||||
$this->success('Hello ' . $name);
|
||||
|
||||
$this->terminate();
|
||||
$this->success('Succcess: Hello ' . $name);
|
||||
$this->warning('Warning: Hello ' . $name);
|
||||
$this->error('Error: Hello ' . $name);
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue