Skip to content

Commit

Permalink
fix: cli-utils cannot render UpperCase color tag
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Feb 27, 2019
1 parent 1e31c80 commit 0c82710
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 99 deletions.
11 changes: 8 additions & 3 deletions libs/cli-utils/src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ class App
*/
private $messages = [];

/** @var int */
/**
* @var int
*/
private $keyWidth = 12;

/**
Expand All @@ -61,7 +63,6 @@ public function __construct(array $argv = null)
$this->script = \array_shift($argv);
// parse flags
[$this->args, $this->opts] = Flags::simpleParseArgv($argv);

}

/**
Expand Down Expand Up @@ -178,6 +179,10 @@ public function addCommand(string $command, callable $handler, string $descripti
throw new \InvalidArgumentException('Invalid arguments');
}

if (($len = \strlen($command)) > $this->keyWidth) {
$this->keyWidth = $len;
}

$this->commands[$command] = $handler;
$this->messages[$command] = \trim($description);
}
Expand Down Expand Up @@ -219,7 +224,7 @@ public function displayHelp(string $err = ''): void
$help = "Welcome to the Lite Console Application.\n\n<comment>Available Commands:</comment>\n";

foreach ($this->messages as $command => $desc) {
$command = str_pad($command, $commandWidth, ' ');
$command = \str_pad($command, $commandWidth, ' ');
$desc = $desc ?: 'No description for the command';
$help .= " $command $desc\n";
}
Expand Down
87 changes: 74 additions & 13 deletions libs/cli-utils/src/Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,33 @@

namespace Toolkit\Cli;

use function getenv;

/**
* Class Cli
* @package Toolkit\Cli
*/
class Cli
{
public const LOG_LEVEL2TAG = [
'info' => 'info',
'warn' => 'warning',
'warning' => 'warning',
'debug' => 'cyan',
'notice' => 'notice',
'error' => 'error',
];

/*******************************************************************************
* read/write message
******************************************************************************/

/**
* @param mixed $message
* @param string $message
* @param bool $nl
* @return string
*/
public static function read($message = null, $nl = false): string
public static function read(string $message = '', bool $nl = false): string
{
if ($message) {
self::write($message, $nl);
Expand All @@ -33,15 +44,24 @@ public static function read($message = null, $nl = false): string
}

/**
* write message to console
* @param $messages
* @param bool $nl
* @param bool $quit
* @param string $format
* @param mixed ...$args
*/
public static function write($messages, $nl = true, $quit = false): void
public static function writef(string $format, ...$args): void
{
self::write(\sprintf($format, ...$args));
}

/**
* Write message to console
* @param string|array $messages
* @param bool $nl
* @param bool|int $quit
*/
public static function write($messages, bool $nl = true, $quit = false): void
{
if (\is_array($messages)) {
$messages = implode($nl ? \PHP_EOL : '', $messages);
$messages = \implode($nl ? \PHP_EOL : '', $messages);
}

self::stdout(Color::parseTag($messages), $nl, $quit);
Expand All @@ -53,10 +73,10 @@ public static function write($messages, $nl = true, $quit = false): void
* @param bool $nl
* @param bool|int $quit
*/
public static function stdout(string $message, $nl = true, $quit = false): void
public static function stdout(string $message, bool $nl = true, $quit = false): void
{
fwrite(\STDOUT, $message . ($nl ? \PHP_EOL : ''));
fflush(\STDOUT);
\fwrite(\STDOUT, $message . ($nl ? \PHP_EOL : ''));
\fflush(\STDOUT);

if (($isTrue = true === $quit) || \is_int($quit)) {
$code = $isTrue ? 0 : $quit;
Expand All @@ -72,8 +92,8 @@ public static function stdout(string $message, $nl = true, $quit = false): void
*/
public static function stderr(string $message, $nl = true, $quit = -1): void
{
fwrite(\STDERR, self::color('[ERROR] ', 'red') . $message . ($nl ? PHP_EOL : ''));
fflush(\STDOUT);
\fwrite(\STDERR, self::color('[ERROR] ', 'red') . $message . ($nl ? PHP_EOL : ''));
\fflush(\STDOUT);

if (($isTrue = true === $quit) || \is_int($quit)) {
$code = $isTrue ? 0 : $quit;
Expand All @@ -95,6 +115,47 @@ public static function color(string $text, $style = null): string
return Color::render($text, $style);
}

/**
* print log to console
* @param string $msg
* @param array $data
* @param string $type
* @param array $opts
* [
* '_category' => 'application',
* 'process' => 'work',
* 'pid' => 234,
* 'coId' => 12,
* ]
*/
public static function log(string $msg, array $data = [], string $type = 'info', array $opts = []): void
{
if (isset(self::LOG_LEVEL2TAG[$type])) {
$type = ColorTag::add(\strtoupper($type), self::LOG_LEVEL2TAG[$type]);
}

$userOpts = [];

foreach ($opts as $n => $v) {
if (\is_numeric($n) || \strpos($n, '_') === 0) {
$userOpts[] = "[$v]";
} else {
$userOpts[] = "[$n:$v]";
}
}

$optString = $userOpts ? ' ' . \implode(' ', $userOpts) : '';

self::write(\sprintf(
'%s [%s]%s %s %s',
\date('Y/m/d H:i:s'),
$type,
$optString,
\trim($msg),
$data ? \PHP_EOL . \json_encode($data, \JSON_UNESCAPED_SLASHES | \JSON_PRETTY_PRINT) : ''
));
}

/*******************************************************************************
* some helpers
******************************************************************************/
Expand Down
29 changes: 20 additions & 9 deletions libs/cli-utils/src/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
*/
class Color
{
public const RESET = 0;
public const NORMAL = 0;

// Foreground color
Expand Down Expand Up @@ -164,7 +165,7 @@ class Color
* @param array $args
* @return string
*/
public static function __callStatic($method, array $args)
public static function __callStatic(string $method, array $args)
{
if (isset(self::STYLES[$method])) {
return self::render($args[0], $method);
Expand All @@ -173,12 +174,8 @@ public static function __callStatic($method, array $args)
return '';
}

/*******************************************************************************
* color render
******************************************************************************/

/**
* apply style for text
* Apply style for text
* @param string $style
* @param string $text
* @return string
Expand All @@ -189,7 +186,21 @@ public static function apply(string $style, string $text): string
}

/**
* render text
* Format and print to STDOUT
* @param string $format
* @param mixed ...$args
*/
public static function printf(string $format, ...$args): void
{
echo self::render(\sprintf($format, ...$args));
}

/*******************************************************************************
* color render
******************************************************************************/

/**
* Render text, apply color code
* @param string $text
* @param string|array $style
* - string: 'green', 'blue'
Expand All @@ -214,7 +225,7 @@ public static function render(string $text, $style = null): string
$color = \implode(';', $style);

// user color tag: <info>message</info>
} elseif (\strpos($text, '<') !== false) {
} elseif (\strpos($text, '</') > 0) {
return self::parseTag($text);
} else {
return $text;
Expand All @@ -240,7 +251,7 @@ public static function parseTag(string $text)
return static::clearColor($text);
}

if (!\preg_match_all(self::COLOR_TAG, $text, $matches)) {
if (!\preg_match_all(ColorTag::MATCH_TAG, $text, $matches)) {
return $text;
}

Expand Down
15 changes: 8 additions & 7 deletions libs/cli-utils/src/ColorTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
class ColorTag
{
// regex used for removing color tags
private const STRIP_TAG = '/<[\/]?[a-zA-Z=;]+>/';
public const STRIP_TAG = '/<[\/]?[a-zA-Z=;]+>/';

// Regex to match tags/
private const COLOR_TAG = '/<([a-zA-Z=;]+)>(.*?)<\/\\1>/s';
public const MATCH_TAG = '/<([a-zA-Z=;_]+)>(.*?)<\/\\1>/s';

/**
* alias of the wrap()
* Alias of the wrap()
* @param string $text
* @param string $tag
* @return string
*/
public static function add(string $text, string $tag): string
Expand Down Expand Up @@ -51,7 +52,7 @@ public static function wrap(string $text, string $tag): string
*/
public static function matchAll(string $text): array
{
if (!\preg_match_all(self::COLOR_TAG, $text, $matches)) {
if (!\preg_match_all(self::MATCH_TAG, $text, $matches)) {
return [];
}

Expand All @@ -64,17 +65,17 @@ public static function parse(string $text): string
}

/**
* exists color tags
* Exists color tags
* @param string $text
* @return bool
*/
public static function exists(string $text): bool
{
return false !== \strpos($text, '</');
return \strpos($text, '</') > 0;
}

/**
* alias of the strip()
* Alias of the strip()
* @param string $text
* @return string
*/
Expand Down
66 changes: 0 additions & 66 deletions libs/cli-utils/src/Console.php

This file was deleted.

15 changes: 14 additions & 1 deletion libs/cli-utils/test/ColorTagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,26 @@ class ColorTagTest extends TestCase
public function testMatchAll(): void
{
$ret = ColorTag::matchAll('<tag>text0</tag> or <info>text1</info>');

$this->assertCount(3, $ret);
// tag
$this->assertSame('tag', $ret[1][0]);
$this->assertSame('info', $ret[1][1]);
// content
$this->assertSame('text0', $ret[2][0]);

$ret = ColorTag::matchAll('<some_tag>text</some_tag>');
$this->assertCount(3, $ret);
// tag
$this->assertSame('some_tag', $ret[1][0]);
// content
$this->assertSame('text', $ret[2][0]);

$ret = ColorTag::matchAll('<someTag>text</someTag>');
$this->assertCount(3, $ret);
// tag
$this->assertSame('someTag', $ret[1][0]);
// content
$this->assertSame('text', $ret[2][0]);
}

public function testStrip(): void
Expand Down
Loading

0 comments on commit 0c82710

Please sign in to comment.