Skip to content
This repository was archived by the owner on Jan 23, 2019. It is now read-only.

Commit 90ec6fb

Browse files
committed
some modify for cli output
1 parent dbcb029 commit 90ec6fb

File tree

3 files changed

+44
-26
lines changed

3 files changed

+44
-26
lines changed

src/console/ConsoleHelper.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ class ConsoleHelper
1818
public static function isSupportColor()
1919
{
2020
if (DIRECTORY_SEPARATOR === '\\') {
21-
return false !== getenv('ANSICON') || 'ON' === getenv('ConEmuANSI');
21+
return
22+
'10.0.10586' === PHP_WINDOWS_VERSION_MAJOR.'.'.PHP_WINDOWS_VERSION_MINOR.'.'.PHP_WINDOWS_VERSION_BUILD
23+
|| false !== getenv('ANSICON')
24+
|| 'ON' === getenv('ConEmuANSI')
25+
|| 'xterm' === getenv('TERM');
2226
}
2327

2428
if (!defined('STDOUT')) {

src/console/Interact.php

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@
1717
*/
1818
class Interact
1919
{
20-
const STAR_LINE = "*************************************%s*************************************\n";
21-
22-
const NL = "\n";// new line
23-
const TAB = ' ';
24-
const NL_TAB = "\n ";// new line + tab
2520

2621
/////////////////////////////////////////////////////////////////
2722
/// Interactive method (select/confirm/question/loopAsk)
@@ -631,8 +626,8 @@ public static function getColor()
631626

632627
/**
633628
* 读取输入信息
634-
* @param string $text 若不为空,则先输出文本
635-
* @param bool $nl true 会添加换行符 false 原样输出,不添加换行符
629+
* @param string $message 若不为空,则先输出文本
630+
* @param bool $nl true 会添加换行符 false 原样输出,不添加换行符
636631
* @return string
637632
*/
638633
public static function readRow($message = null, $nl = false)
@@ -641,27 +636,35 @@ public static function readRow($message = null, $nl = false)
641636
}
642637
public static function read($message = null, $nl = false)
643638
{
644-
self::write($message, $nl);
639+
if ( $message ) {
640+
self::write($message, $nl);
641+
}
645642

646643
return trim(fgets(STDIN));
647644
}
648645

649646
/**
650-
* 输出
651-
* @param string $text
652-
* @param bool $nl true 会添加换行符 false 原样输出,不添加换行符
653-
* @param int|boolean $quit If is int, settin it is exit code.
647+
* Write a message to standard output stream.
648+
* @param string|array $messages Output message
649+
* @param boolean $nl true 会添加换行符 false 原样输出,不添加换行符
650+
* @param int|boolean $quit If is int, settin it is exit code.
654651
*/
655-
public static function write($text, $nl = true, $quit = false)
652+
public static function write($messages, $nl = true, $quit = false)
656653
{
657-
$text = static::getColor()->format($text);
654+
// if ( is_array($messages) ) {
655+
// $messages = implode( $nl ? PHP_EOL : '', $messages );
656+
// }
658657

659-
fwrite(STDOUT, $text . ($nl ? "\n" : null));
658+
$messages = static::getColor()->format($messages);
659+
660+
fwrite(STDOUT, $messages . ($nl ? PHP_EOL : ''));
660661

661662
if ( is_int($quit) || true === $quit) {
662663
$code = true === $quit ? 0 : $quit;
663664
exit($code);
664665
}
666+
667+
fflush(STDOUT);
665668
}
666669

667670

src/console/Output.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -132,35 +132,46 @@ public function comment($messages, $quit = false)
132132

133133
/**
134134
* 读取输入信息
135-
* @param string $text 若不为空,则先输出文本
136-
* @param bool $nl true 会添加换行符 false 原样输出,不添加换行符
135+
* @param string $message 若不为空,则先输出文本
136+
* @param bool $nl true 会添加换行符 false 原样输出,不添加换行符
137137
* @return string
138138
*/
139139
public function read($message = null, $nl = false)
140140
{
141-
$this->write($message, $nl);
141+
if ( $message ) {
142+
$this->write($message, $nl);
143+
}
142144

143145
return trim(fgets(STDIN));
144146
}
145147

146148
/**
147-
* Write a message to standard output.
148-
* @param string $text output message
149-
* @param bool $nl true 会添加换行符 false 原样输出,不添加换行符
150-
* @param int|boolean $quit If is int, settin it is exit code.
149+
* Write a message to standard output stream.
150+
* @param mixed $messages Output message
151+
* @param bool $nl true 会添加换行符 false 原样输出,不添加换行符
152+
* @param int|boolean $quit If is int, settin it is exit code.
151153
* @return static
152154
*/
153-
public function write($text = '', $nl = true, $quit = false)
155+
public function write($messages = '', $nl = true, $quit = false)
154156
{
155-
$text = $this->getColor()->format($text);
157+
if ( is_array($messages) ) {
158+
$messages = implode( $nl ? PHP_EOL : '', $messages );
159+
}
156160

157-
fwrite($this->outputStream, $text . ($nl ? "\n" : null));
161+
$messages = $this->getColor()->format($messages);
162+
163+
if (false === @fwrite($this->outputStream, $messages . ($nl ? PHP_EOL : ''))) {
164+
// should never happen
165+
throw new \RuntimeException('Unable to write output.');
166+
}
158167

159168
if ( is_int($quit) || true === $quit) {
160169
$code = true === $quit ? 0 : $quit;
161170
exit($code);
162171
}
163172

173+
fflush($this->outputStream);
174+
164175
return $this;
165176
}
166177

0 commit comments

Comments
 (0)