Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
stelin committed Apr 10, 2018
2 parents bd1cd31 + 2cd14ba commit d87c52f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 35 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

Swoft Console Component

# Install
## Install

- composer command

```bash
composer require swoft/console
```

# Document
## Document

Please see [document site](https://doc.swoft.org)

# LICENSE
## LICENSE

The Component is open-sourced software licensed under the [Apache license](LICENSE).
54 changes: 53 additions & 1 deletion src/Helper/ConsoleUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,66 @@

namespace Swoft\Console\Helper;

use Swoft\Console\Style\Style;

/**
* Class ConsoleUtil
* @package Swoft\Console\Helper
*/
class ConsoleUtil
{
const LOG_LEVEL2TAG = [
'info' => 'info',
'warn' => 'warning',
'warning' => 'warning',
'debug' => 'cyan',
'notice' => 'notice',
'error' => 'error',
];

/**
* 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 = [])
{
if (isset(self::LOG_LEVEL2TAG[$type])) {
$type = Style::wrap(\strtoupper($type), self::LOG_LEVEL2TAG[$type]);
}

$userOpts = [];

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

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

\output()->writeln(\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) : ''
));
}

/**
* 与文本进度条相比,没有 total
* 与文本进度条相比,没有 total - 不会显示进度百分比
*
* ```php
* $total = 120;
Expand Down
39 changes: 8 additions & 31 deletions src/Style/Style.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Swoft\Console\Style;

use Swoft\Bean\Annotation\Bean;
use Swoft\Console\Helper\CommandHelper;

/**
* The style of command
Expand Down Expand Up @@ -85,7 +86,7 @@ public function init()
public function t(string $message)
{
// 不支持颜色,移除颜色标签
if (! $this->isSupportColor()) {
if (!CommandHelper::supportColor()) {
return $this->stripColor($message);
}

Expand Down Expand Up @@ -150,42 +151,18 @@ private function replaceColor(string $text, string $tag, string $match, string $
* @param string $message
* @return mixed
*/
private function stripColor(string $message)
public function stripColor(string $message)
{
return preg_replace(self::STRIP_REG, '', $message);
}

/**
* 命令行是否支持颜色
*
* @return bool
*/
private function isSupportColor(): bool
{
if (DIRECTORY_SEPARATOR === '\\') {
$term = 'xterm' === getenv('TERM');
$ansicon = false !== getenv('ANSICON');
$conEmuAnsi = 'ON' === getenv('ConEmuANSI');
$windowsVersion = '10.0.10586' === PHP_WINDOWS_VERSION_MAJOR . '.' . PHP_WINDOWS_VERSION_MINOR . '.' . PHP_WINDOWS_VERSION_BUILD;
$isSupport = $windowsVersion || $ansicon || $conEmuAnsi || $term;
return $isSupport;
}

if (! \defined('STDOUT')) {
return false;
}

return $this->isInteractive(STDOUT);
}

/**
* 是否是交互是终端
*
* @param mixed $fileDescriptor
* @return bool
* @param string $text
* @param string $tag
* @return string
*/
private function isInteractive($fileDescriptor): bool
public static function wrap(string $text, string $tag = 'info'): string
{
return \function_exists('posix_isatty') && @posix_isatty($fileDescriptor);
return \sprintf('<%s>%s</%s>', $tag, $text, $tag);
}
}

0 comments on commit d87c52f

Please sign in to comment.