Skip to content

Commit

Permalink
add some tool method. some modify
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Mar 28, 2018
1 parent 8fa3450 commit 3bceb4c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 32 deletions.
34 changes: 33 additions & 1 deletion src/Helper/ConsoleUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,46 @@

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
*/
public static function log(string $msg, array $data = [], string $type = 'info')
{
if (isset(self::LOG_LEVEL2TAG[$type])) {
$type = Style::wrap(\strtoupper($type), self::LOG_LEVEL2TAG[$type]);
}

\output()->writeln(\sprintf(
'%s [%s] %s %s',
\date('Y/m/d H:i:s'),
$type,
\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 3bceb4c

Please sign in to comment.