diff --git a/src/AbstractApplication.php b/src/AbstractApplication.php index 514eddb..da25865 100644 --- a/src/AbstractApplication.php +++ b/src/AbstractApplication.php @@ -10,7 +10,6 @@ use ErrorException; use Inhere\Console\Component\ErrorHandler; -use Inhere\Console\Component\Style\Style; use Inhere\Console\Contract\ApplicationInterface; use Inhere\Console\Contract\ErrorHandlerInterface; use Inhere\Console\Contract\InputInterface; @@ -22,6 +21,7 @@ use Inhere\Console\Traits\SimpleEventTrait; use InvalidArgumentException; use Throwable; +use Toolkit\Cli\Style; use Toolkit\Stdlib\Helper\PhpHelper; use function array_keys; use function array_merge; diff --git a/src/Component/ErrorHandler.php b/src/Component/ErrorHandler.php index 7c62c4c..282ffe2 100644 --- a/src/Component/ErrorHandler.php +++ b/src/Component/ErrorHandler.php @@ -12,7 +12,7 @@ use Inhere\Console\Contract\ErrorHandlerInterface; use Inhere\Console\Exception\PromptException; use Throwable; -use Toolkit\Cli\Highlighter; +use Toolkit\Cli\Util\Highlighter; use function file_get_contents; use function get_class; use function sprintf; diff --git a/src/Component/Style/Style.php b/src/Component/Style/Style.php index 9cc441a..318efc4 100644 --- a/src/Component/Style/Style.php +++ b/src/Component/Style/Style.php @@ -28,6 +28,7 @@ * * @package Inhere\Console\Component\Style * @link https://github.com/ventoviro/windwalker-IO + * @deprecated please use Toolkit\Cli\Style instead. * * @method string info(string $message) * @method string comment(string $message) diff --git a/src/Console.php b/src/Console.php index fb63ad2..4c07d27 100644 --- a/src/Console.php +++ b/src/Console.php @@ -2,19 +2,12 @@ namespace Inhere\Console; -use Inhere\Console\Component\Style\Style; use Inhere\Console\IO\Input; use Inhere\Console\IO\Output; +use Toolkit\Cli\Cli; use Toolkit\Cli\ColorTag; -use function array_merge; use function date; -use function fflush; -use function fgets; -use function fgetss; -use function file_get_contents; -use function fwrite; use function implode; -use function is_array; use function is_numeric; use function json_encode; use function sprintf; @@ -23,25 +16,22 @@ use const JSON_PRETTY_PRINT; use const JSON_UNESCAPED_SLASHES; use const PHP_EOL; -use const STDERR; -use const STDIN; -use const STDOUT; /** * Class Console * * @package Inhere\Console */ -class Console +class Console extends Cli { // constants for error level 0 - 4. you can setting by '--debug LEVEL' public const VERB_QUIET = 0; public const VERB_ERROR = 1; // default reporting on error - public const VERB_WARN = 2; + public const VERB_WARN = 2; - public const VERB_INFO = 3; + public const VERB_INFO = 3; public const VERB_DEBUG = 4; @@ -71,12 +61,6 @@ class Console */ private static $app; - /** @var string */ - private static $buffer = ''; - - /** @var bool */ - private static $buffering = false; - /** * @return Application */ @@ -108,188 +92,6 @@ public static function newApp( return new Application($config, $input, $output); } - /** - * @return Style - */ - public static function style(): Style - { - return Style::instance(); - } - - /*********************************************************************************** - * Output message - ***********************************************************************************/ - - /** - * Format and write message to terminal. like printf() - * - * @param string $format - * @param mixed ...$args - * - * @return int - */ - public static function writef(string $format, ...$args): int - { - return self::write(sprintf($format, ...$args)); - } - - /** - * Format and write message to terminal. like printf() - * - * @param string $format - * @param mixed ...$args - * - * @return int - */ - public static function printf(string $format, ...$args): int - { - return self::write(sprintf($format, ...$args)); - } - - /** - * Write raw data to stdout, will disable color render. - * - * @param string|array $message - * @param bool $nl - * @param bool|int $quit - * @param array $opts - * - * @return int - */ - public static function writeRaw($message, $nl = true, $quit = false, array $opts = []): int - { - $opts['color'] = false; - return self::write($message, $nl, $quit, $opts); - } - - /** - * Write data to stdout with newline. - * - * @param string|array $message - * @param array $opts - * @param bool|int $quit - * - * @return int - */ - public static function writeln($message, $quit = false, array $opts = []): int - { - return self::write($message, true, $quit, $opts); - } - - /** - * Write data to stdout with newline. - * - * @param string|array $message - * @param array $opts - * @param bool|int $quit - * - * @return int - */ - public static function println($message, $quit = false, array $opts = []): int - { - return self::write($message, true, $quit, $opts); - } - - /** - * Write message to stdout. - * - * @param string|array $message - * @param array $opts - * @param bool|int $quit - * - * @return int - */ - public static function print($message, $quit = false, array $opts = []): int - { - return self::write($message, false, $quit, $opts); - } - - /** - * Write a message to standard output stream. - * - * @param string|array $messages Output message - * @param boolean $nl True 会添加换行符, False 原样输出,不添加换行符 - * @param int|boolean $quit If is int, setting it is exit code. - * 'True' translate as code 0 and exit, 'False' will not exit. - * @param array $opts Some options for write - * [ - * 'color' => bool, // whether render color, default is: True. - * 'stream' => resource, // the stream resource, default is: STDOUT - * 'flush' => bool, // flush the stream data, default is: True - * ] - * - * @return int - */ - public static function write($messages, $nl = true, $quit = false, array $opts = []): int - { - if (is_array($messages)) { - $messages = implode($nl ? PHP_EOL : '', $messages); - } - - $messages = (string)$messages; - - if (!isset($opts['color']) || $opts['color']) { - $messages = Style::instance()->render($messages); - } else { - $messages = Style::stripColor($messages); - } - - // if open buffering - if (self::isBuffering()) { - self::$buffer .= $messages . ($nl ? PHP_EOL : ''); - - if (!$quit) { - return 0; - } - - $messages = self::$buffer; - // clear buffer - self::$buffer = ''; - } else { - $messages .= $nl ? PHP_EOL : ''; - } - - fwrite($stream = $opts['stream'] ?? STDOUT, $messages); - - if (!isset($opts['flush']) || $opts['flush']) { - fflush($stream); - } - - // if will quit. - if ($quit !== false) { - $code = true === $quit ? 0 : (int)$quit; - exit($code); - } - - return 0; - } - - /** - * Logs data to stdout - * - * @param string|array $text - * @param bool $nl - * @param bool|int $quit - */ - public static function stdout($text, $nl = true, $quit = false): void - { - self::write($text, $nl, $quit); - } - - /** - * Logs data to stderr - * - * @param string|array $text - * @param bool $nl - * @param bool|int $quit - */ - public static function stderr($text, $nl = true, $quit = -200): void - { - self::write($text, $nl, $quit, [ - 'stream' => STDERR, - ]); - } - /** * @param int $level * @param string $format @@ -335,220 +137,9 @@ public static function log(string $msg, array $data = [], int $level = self::VER } } - $optString = $userOpts ? ' ' . implode(' ', $userOpts) : ''; - - self::write(sprintf( - '%s [%s]%s %s %s', - date('Y/m/d H:i:s'), - $taggedName, - $optString, - trim($msg), - $data ? PHP_EOL . json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT) : '' - )); - } - - /*********************************************************************************** - * Read message - ***********************************************************************************/ - - /** - * Read message from STDIN - * - * @param mixed $message - * @param bool $nl - * @param array $opts - * - * @return string - */ - public static function read($message = null, bool $nl = false, array $opts = []): string - { - if ($message) { - self::write($message, $nl); - } - - $opts = array_merge([ - 'length' => 1024, - 'stream' => STDIN, - ], $opts); - - return file_get_contents($opts['stream'], $opts['length']); - } - - /** - * Gets line from file pointer - * - * @param mixed $message - * @param bool $nl - * @param array $opts - * [ - * 'stream' => \STDIN - * ] - * - * @return string - */ - public static function readln($message = null, $nl = false, array $opts = []): string - { - if ($message) { - self::write($message, $nl); - } - - $opts = array_merge([ - 'length' => 1024, - 'stream' => STDIN, - ], $opts); - - return trim(fgets($opts['stream'], $opts['length'])); - } + $optString = $userOpts ? ' ' . implode(' ', $userOpts) : ''; + $dataString = $data ? PHP_EOL . json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT) : ''; - /** - * Read input information - * - * @param mixed $message 若不为空,则先输出文本 - * @param bool $nl true 会添加换行符 false 原样输出,不添加换行符 - * - * @return string - */ - public static function readRow($message = null, $nl = false): string - { - return self::readln($message, $nl); - } - - /** - * Gets line from file pointer and strip HTML tags - * - * @param mixed $message - * @param bool $nl - * @param array $opts - * - * @return string - */ - public static function readSafe($message = null, bool $nl = false, array $opts = []): string - { - if ($message) { - self::write($message, $nl); - } - - $opts = array_merge([ - 'length' => 1024, - 'stream' => STDIN, - 'allowTags' => null, - ], $opts); - - return trim(fgetss($opts['stream'], $opts['length'], $opts['allowTags'])); - } - - /** - * Gets first character from file pointer - * - * @param string $message - * @param bool $nl - * - * @return string - */ - public static function readChar(string $message = '', bool $nl = false): string - { - $line = self::readln($message, $nl); - - return $line !== '' ? $line[0] : ''; - } - - /** - * Read input first char - * - * @param string $message - * @param bool $nl - * - * @return string - */ - public static function readFirst(string $message = '', bool $nl = false): string - { - return self::readChar($message, $nl); - } - - /*********************************************************************************** - * Output buffer - ***********************************************************************************/ - - /** - * @return bool - */ - public static function isBuffering(): bool - { - return self::$buffering; - } - - /** - * @return string - */ - public static function getBuffer(): string - { - return self::$buffer; - } - - /** - * @param string $buffer - */ - public static function setBuffer(string $buffer): void - { - self::$buffer = $buffer; - } - - /** - * Start buffering - */ - public static function startBuffer(): void - { - self::$buffering = true; - } - - /** - * Clear buffering - */ - public static function clearBuffer(): void - { - self::$buffer = ''; - } - - /** - * Stop buffering - * - * @param bool $flush Whether flush buffer to output stream - * @param bool $nl Default is False, because the last write() have been added "\n" - * @param bool $quit - * @param array $opts - * - * @return string If flush = False, will return all buffer text. - * @see write() - */ - public static function stopBuffer($flush = true, $nl = false, $quit = false, array $opts = []): string - { - self::$buffering = false; - - if ($flush && self::$buffer) { - // all text have been rendered by Style::render() in every write(); - $opts['color'] = false; - - // flush to stream - self::write(self::$buffer, $nl, $quit, $opts); - - // clear buffer - self::$buffer = ''; - } - - return self::$buffer; - } - - /** - * Stop buffering and flush buffer text - * - * @param bool $nl - * @param bool $quit - * @param array $opts - * - * @see write() - */ - public static function flushBuffer($nl = false, $quit = false, array $opts = []): void - { - self::stopBuffer(true, $nl, $quit, $opts); + self::writef('%s [%s]%s %s %s', date('Y/m/d H:i:s'), $taggedName, $optString, trim($msg), $dataString); } } diff --git a/src/IO/Output.php b/src/IO/Output.php index 5ae5e17..b7e7fc9 100644 --- a/src/IO/Output.php +++ b/src/IO/Output.php @@ -8,7 +8,7 @@ namespace Inhere\Console\IO; -use Inhere\Console\Component\Style\Style; +use Toolkit\Cli\Style; use Inhere\Console\Console; use Inhere\Console\Traits\FormatOutputAwareTrait; use Toolkit\Cli\Cli; diff --git a/src/Traits/ApplicationHelpTrait.php b/src/Traits/ApplicationHelpTrait.php index 9c113c3..0280b3f 100644 --- a/src/Traits/ApplicationHelpTrait.php +++ b/src/Traits/ApplicationHelpTrait.php @@ -9,7 +9,7 @@ namespace Inhere\Console\Traits; use Inhere\Console\AbstractHandler; -use Inhere\Console\Component\Style\Style; +use Toolkit\Cli\Style; use Inhere\Console\Console; use Inhere\Console\Contract\CommandInterface; use Inhere\Console\IO\Input; diff --git a/src/Traits/FormatOutputAwareTrait.php b/src/Traits/FormatOutputAwareTrait.php index f6ebf6e..b17518e 100644 --- a/src/Traits/FormatOutputAwareTrait.php +++ b/src/Traits/FormatOutputAwareTrait.php @@ -17,7 +17,7 @@ use Inhere\Console\Component\Formatter\SingleList; use Inhere\Console\Component\Formatter\Table; use Inhere\Console\Component\Formatter\Title; -use Inhere\Console\Component\Style\Style; +use Toolkit\Cli\Style; use Inhere\Console\Console; use Inhere\Console\Util\Interact; use Inhere\Console\Util\Show; diff --git a/src/Util/Show.php b/src/Util/Show.php index 7acc9f4..e49ce98 100644 --- a/src/Util/Show.php +++ b/src/Util/Show.php @@ -16,7 +16,7 @@ use Inhere\Console\Component\Progress\DynamicText; use Inhere\Console\Component\Progress\SimpleBar; use Inhere\Console\Component\Progress\SimpleTextBar; -use Inhere\Console\Component\Style\Style; +use Toolkit\Cli\Style; use Inhere\Console\Console; use LogicException; use Toolkit\Cli\Cli;