Skip to content

Commit 8d7f2fa

Browse files
committed
up
1 parent 1881132 commit 8d7f2fa

File tree

1 file changed

+44
-6
lines changed

1 file changed

+44
-6
lines changed

src/utils/Show.php

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -642,25 +642,63 @@ public static function getStyle()
642642
/**
643643
* Write a message to standard output stream.
644644
* @param string|array $messages Output message
645-
* @param boolean $nl true 会添加换行符 false 原样输出,不添加换行符
646-
* @param int|boolean $quit If is int, setting it is exit code.
645+
* @param boolean $nl True 会添加换行符, False 原样输出,不添加换行符
646+
* @param int|boolean $quit If is int, setting it is exit code. 'True' translate as code 0 and exit, 'False' will not exit.
647647
*/
648-
public static function write($messages, $nl = true, $quit = false)
648+
public static function write($messages, $nl = true, $quit = false, array $opts = [])
649649
{
650650
if (is_array($messages)) {
651651
$messages = implode($nl ? PHP_EOL : '', $messages);
652652
}
653653

654-
$messages = static::getStyle()->format($messages);
654+
$messages = static::getStyle()->render($messages);
655+
$stream = isset($opts['stream']) ? $opts['stream']: STDOUT;
655656

656-
fwrite(STDOUT, $messages . ($nl ? PHP_EOL : ''));
657+
fwrite($stream, $messages . ($nl ? PHP_EOL : ''));
657658

658659
if (is_int($quit) || true === $quit) {
659660
$code = true === $quit ? 0 : $quit;
660661
exit($code);
661662
}
662663

663-
fflush(STDOUT);
664+
if (isset($opts['flush']) && $opts['flush']) {
665+
fflush($stream);
666+
}
667+
}
668+
669+
/**
670+
* Logs data to stdout
671+
* @param string|array $text
672+
* @param bool $nl
673+
* @param bool|int $quit
674+
*/
675+
public static function writeln($text, $quit = false, array $opts = [])
676+
{
677+
self::write($text, true, $quit, $opts);
678+
}
679+
680+
/**
681+
* Logs data to stdout
682+
* @param string|array $text
683+
* @param bool $nl
684+
* @param bool|int $quit
685+
*/
686+
public static function stdout($text, $nl = true, $quit = false)
687+
{
688+
self::write($text, $nl, $quit);
689+
}
690+
691+
/**
692+
* Logs data to stderr
693+
* @param string|array $text
694+
* @param bool $nl
695+
* @param bool|int $quit
696+
*/
697+
public static function stderr($text, $nl = true, $quit = -200)
698+
{
699+
self::write($text, $nl, $quit, [
700+
'stream' => STDERR,
701+
]);
664702
}
665703

666704
}

0 commit comments

Comments
 (0)