Skip to content
This repository has been archived by the owner on Apr 21, 2021. It is now read-only.

Commit

Permalink
add color for command env. :)
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 1, 2016
1 parent d66caa1 commit c4adefe
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 13 deletions.
17 changes: 12 additions & 5 deletions Po.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* 若使用了命名空间 类方法调用 需在最前加上'\'。 @example \Po::d($arg1,$arg2,$arg3,...);
**/

include __DIR__.'/helpers/PrintHelper.php';
include_once __DIR__.'/helpers/PrintHelper.php';
include __DIR__.'/helpers/StaticInvokeHelper.php';

class Po extends StaticInvokeHelper
Expand Down Expand Up @@ -75,7 +75,6 @@ class Po extends StaticInvokeHelper
*/
static private $stripTags = false;


// 标记打印调用后是否退出程序
private $exit = false;

Expand Down Expand Up @@ -224,7 +223,9 @@ public function invoking($methodName,array $data)
$outputData = $positionData.$outputData;
}

if ( !PrintHelper::isWebRequest() ) {
if ( PrintHelper::isCli() && PrintHelper::hasColorSupport() ) {
$outputData .= "\n\033[46;37m <<<<<< $methodName() print out end ...... \033[0m\n";
} else if ( !PrintHelper::isWebRequest() ) {
$outputData .= "\n<<<<<< $methodName() print out end ......\n";
}

Expand Down Expand Up @@ -264,6 +265,7 @@ static public function stripTags($value=true)
/**
* 开启侦测Ajax请求, 需在加载页面时开启(而不是在Ajax请求时调用开启)
* @todo 未完善
* @param bool $value
*/
static public function detectAjax($value=true)
{
Expand Down Expand Up @@ -756,7 +758,12 @@ public function calledPosition($backNum=6,$separator='#5')
# ajax cli flash
if (!PrintHelper::isWebRequest() || self::$stripTags ) {
$positionInfo = str_replace($this->rootPath, '<ROOT>', $positionInfo);
$this->positionData = "\n>>>>>> The method $positionInfo\n\n";

if ( PrintHelper::isCli() && PrintHelper::hasColorSupport() ) {
$this->positionData = "\n\033[46;37m >>>>>> The method $positionInfo\033[0m\n";
} else {
$this->positionData = "\n>>>>>> The method $positionInfo\n";
}

return $this;
}
Expand All @@ -767,7 +774,7 @@ public function calledPosition($backNum=6,$separator='#5')
# 加载样式和jQuery。 TODO: 同一个页面只加载一次样式和jQuery
if (!self::$hasStyle) {
$positionData .= self::_styleTag().PHP_EOL.self::_scriptTag();
self::$hasStyle =true;
self::$hasStyle = true;
}

#
Expand Down
74 changes: 66 additions & 8 deletions helpers/PrintHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ public static function clearTagAndFormat($data)
$data
);
$data = preg_replace(
array( "/[\n\r]+/i", "/Array[\s]*\(/","/=>[\s]+/i" ),
array(
"/[\n\r]+/i",
"/Array[\s]*\(/",
"/=>[\s]+/i"
),
array( "\n", 'Array (',"=> " ),
$data
);
Expand Down Expand Up @@ -121,8 +125,7 @@ public static function getLines($fileName, $startLine = 1, $endLine = 50, $metho
$content[] = $obj_file->current(); // current()获取当前行内容
$obj_file->next(); // 下一行
}
}catch(Exception $e)
{
}catch(Exception $e) {
throw new Exception("读取文件--{$fileName} 发生错误!");
}

Expand All @@ -149,11 +152,38 @@ public static function getLines($fileName, $startLine = 1, $endLine = 50, $metho
return array_filter($content); // array_filter过滤:false,null,''
}

// 命令模式
public static function isCliMode()
/**
* isWeb
* @return boolean
*/
public static function isWeb()
{
return in_array(
PHP_SAPI,
array(
'apache',
'cgi',
'fast-cgi',
'cgi-fcgi',
'fpm-fcgi',
'srv'
)
);
}

/**
* isCli
* @return boolean
*/
public static function isCli()
{
// return PHP_SAPI === 'cli' ? true : false;
return php_sapi_name() === 'cli';
return in_array(
PHP_SAPI,
array(
'cli',
'cli-server'
)
);
}

// ajax 请求
Expand All @@ -178,9 +208,37 @@ public static function getIsFlash()
// 是正常的网络请求 get post
public static function isWebRequest()
{
return !self::isCliMode() && !self::isAjax() && !self::isFlash();
return !self::isCli() && !self::isAjax() && !self::isFlash();
}

/**
* Returns true if STDOUT supports colorization.
* This code has been copied and adapted from
* \Symfony\Component\Console\Output\OutputStream.
* @return boolean
*/
public static function hasColorSupport()
{
if (DIRECTORY_SEPARATOR === '\\') {
return false !== getenv('ANSICON') || 'ON' === getenv('ConEmuANSI');
}

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

return self::isInteractive(STDOUT);
}

/**
* Returns if the file descriptor is an interactive terminal or not.
* @param int|resource $fileDescriptor
* @return boolean
*/
public static function isInteractive($fileDescriptor)
{
return function_exists('posix_isatty') && @posix_isatty($fileDescriptor);
}

public static function varExport($var, $return=false, $length=200)
{
Expand Down

0 comments on commit c4adefe

Please sign in to comment.