Add console features
This commit is contained in:
parent
85583503f6
commit
f8a845527e
3 changed files with 78 additions and 1 deletions
|
@ -17,7 +17,7 @@ class Colors {
|
|||
/**
|
||||
* Colors constructor.
|
||||
*/
|
||||
public static function setColors()
|
||||
private static function setColors()
|
||||
{
|
||||
self::$foreground_colors['black'] = '0;30';
|
||||
self::$foreground_colors['dark_gray'] = '1;30';
|
||||
|
|
70
src/Cli/Assets/Console.php
Normal file
70
src/Cli/Assets/Console.php
Normal file
|
@ -0,0 +1,70 @@
|
|||
<?php
|
||||
|
||||
namespace Cli\Assets;
|
||||
|
||||
trait Console
|
||||
{
|
||||
|
||||
/**
|
||||
* @var false|resource
|
||||
*/
|
||||
private $handle = false;
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
*/
|
||||
public function print(string $message, $color = null, $background = '')
|
||||
{
|
||||
echo Colors::getColoredString($message, $color, $background);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
*/
|
||||
public function printLn(string $message, $color = null, $background = '')
|
||||
{
|
||||
$this->print($message, $color, $background);
|
||||
echo "\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
*/
|
||||
public function success(string $message)
|
||||
{
|
||||
$this->printLn($message, 'green');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $question
|
||||
* @return string
|
||||
*/
|
||||
public function ask($question)
|
||||
{
|
||||
$this->success($question);
|
||||
$this->print("\n > ", 'yellow');
|
||||
|
||||
return $this->getLine();
|
||||
}
|
||||
|
||||
/**
|
||||
* Terminate console
|
||||
*/
|
||||
public function terminate()
|
||||
{
|
||||
exit(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
private function getLine()
|
||||
{
|
||||
if( $this->handle === false) {
|
||||
$this->handle = fopen("php://stdin","r");
|
||||
}
|
||||
|
||||
return trim(fgets($this->handle));
|
||||
}
|
||||
|
||||
}
|
|
@ -2,8 +2,11 @@
|
|||
|
||||
namespace Cli\Commands;
|
||||
|
||||
use Cli\Assets\Console;
|
||||
|
||||
class Commands
|
||||
{
|
||||
use Console;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
|
@ -21,7 +24,11 @@ class Commands
|
|||
|
||||
public function handle()
|
||||
{
|
||||
$name = $this->ask('What is your name? ');
|
||||
|
||||
$this->success('Hello ' . $name);
|
||||
|
||||
$this->terminate();
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue