Skip to content

Commit

Permalink
add new method for print JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Aug 18, 2021
1 parent 53485de commit 6934fc4
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/Component/Progress/CounterText.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class CounterText extends NotifyMessage
*
* @return Generator
*/
public static function gen(string $msg, $doneMsg = ''): Generator
public static function gen(string $msg, string $doneMsg = ''): Generator
{
$counter = 0;
$finished = false;
Expand Down
14 changes: 14 additions & 0 deletions src/Concern/FormatOutputAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Inhere\Console\Concern;

use Inhere\Console\Console;
use Toolkit\Stdlib\Helper\JsonHelper;
use Toolkit\Stdlib\Php;
use function array_merge;
use function json_encode;
Expand Down Expand Up @@ -110,6 +111,19 @@ public function json(
return $string;
}

/**
* @param mixed $data
* @param string $title
*/
public function prettyJSON($data, string $title = 'JSON:'): void
{
if ($title) {
Console::colored($title, 'ylw0');
}

Console::write(JsonHelper::prettyJSON($data));
}

/**
* @param mixed ...$vars
*/
Expand Down
1 change: 1 addition & 0 deletions src/Concern/StyledOutputAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
* @method checkbox(string $description, $options, $default = null, bool $allowExit = true): array
* @method ask(string $question, string $default = '', Closure $validator = null): string
* @method askPassword(string $prompt = 'Enter Password:'): string
* @see Interact
*/
trait StyledOutputAwareTrait
{
Expand Down
60 changes: 28 additions & 32 deletions src/Util/Show.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Toolkit\Cli\Cli;
use Toolkit\Cli\ColorTag;
use Toolkit\Cli\Style;
use Toolkit\Stdlib\Helper\JsonHelper;
use Toolkit\Stdlib\Math;
use Toolkit\Stdlib\Str;
use Toolkit\Sys\Sys;
Expand All @@ -30,17 +31,13 @@
use function implode;
use function is_array;
use function is_string;
use function json_encode;
use function microtime;
use function sprintf;
use function strlen;
use function strpos;
use function strtoupper;
use function substr;
use function ucwords;
use const JSON_PRETTY_PRINT;
use const JSON_UNESCAPED_SLASHES;
use const JSON_UNESCAPED_UNICODE;
use const PHP_EOL;

/**
Expand Down Expand Up @@ -208,17 +205,15 @@ public static function __callStatic(string $method, array $args = [])
* Print JSON
*
* @param mixed $data
* @param int $flags
*
* @return int
* @param string $title
*/
public static function prettyJSON(
$data,
int $flags = JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES
): int {
$string = (string)json_encode($data, $flags);
public static function prettyJSON($data, string $title = 'JSON:'): void
{
if ($title) {
Console::colored($title, 'ylw0');
}

return Console::write($string);
Console::write(JsonHelper::prettyJSON($data));
}

/**
Expand Down Expand Up @@ -424,7 +419,7 @@ public static function table(array $data, string $title = 'Data Table', array $o
* @param string $msg
* @param bool $ended
*/
public static function spinner(string $msg = '', $ended = false): void
public static function spinner(string $msg = '', bool $ended = false): void
{
static $chars = '-\|/';
static $counter = 0;
Expand Down Expand Up @@ -457,7 +452,7 @@ public static function spinner(string $msg = '', $ended = false): void
* @param string $msg
* @param bool $ended
*/
public static function loading(string $msg = 'Loading ', $ended = false): void
public static function loading(string $msg = 'Loading ', bool $ended = false): void
{
self::pending($msg, $ended);
}
Expand All @@ -476,7 +471,7 @@ public static function loading(string $msg = 'Loading ', $ended = false): void
* @param string $msg
* @param bool $ended
*/
public static function pending(string $msg = 'Pending ', $ended = false): void
public static function pending(string $msg = 'Pending ', bool $ended = false): void
{
static $counter = 0;
static $lastTime = null;
Expand Down Expand Up @@ -516,9 +511,9 @@ public static function pending(string $msg = 'Pending ', $ended = false): void
* @param string $msg
* @param bool $ended
*
* @return int|mixed
* @return int
*/
public static function pointing(string $msg = 'handling ', $ended = false)
public static function pointing(string $msg = 'handling ', bool $ended = false): int
{
static $counter = 0;

Expand All @@ -532,40 +527,41 @@ public static function pointing(string $msg = 'handling ', $ended = false)

$counter++;

return print '.';
print '.';
return 0;
}

/**
* 与文本进度条相比,没有 total
*
* @param string $msg
* @param string|null $doneMsg
* @param string $doneMsg
*
* @return Generator
*/
public static function counterTxt(string $msg, $doneMsg = ''): Generator
public static function counterTxt(string $msg, string $doneMsg = ''): Generator
{
return CounterText::gen($msg, $doneMsg);
}

/**
* @param string $doneMsg
* @param string|null $fixMsg
* @param string $fixMsg
*
* @return Generator
*/
public static function dynamicTxt(string $doneMsg, string $fixMsg = null): Generator
public static function dynamicTxt(string $doneMsg, string $fixMsg = ''): Generator
{
return self::dynamicText($doneMsg, $fixMsg);
}

/**
* @param string $doneMsg
* @param string|null $fixedMsg
* @param string $fixedMsg
*
* @return Generator
*/
public static function dynamicText(string $doneMsg, string $fixedMsg = null): Generator
public static function dynamicText(string $doneMsg, string $fixedMsg = ''): Generator
{
return DynamicText::gen($doneMsg, $fixedMsg);
}
Expand Down Expand Up @@ -685,14 +681,14 @@ public static function clearBuffer(): void
*
* @param bool $flush Whether flush buffer to output stream
* @param bool $nl Default is False, because the last write() have been added "\n"
* @param bool $quit
* @param bool|int $quit
* @param array $opts
*
* @return null|string If flush = False, will return all buffer text.
* @see Show::write()
* @deprecated Please use \Inhere\Console\Console method instead it.
*/
public static function stopBuffer($flush = true, $nl = false, $quit = false, array $opts = []): ?string
public static function stopBuffer(bool $flush = true, bool $nl = false, $quit = false, array $opts = []): ?string
{
self::$buffering = false;

Expand Down Expand Up @@ -720,7 +716,7 @@ public static function stopBuffer($flush = true, $nl = false, $quit = false, arr
* @see Show::write()
* @deprecated Please use \Inhere\Console\Console method instead it.
*/
public static function flushBuffer($nl = false, $quit = false, array $opts = []): void
public static function flushBuffer(bool $nl = false, $quit = false, array $opts = []): void
{
self::stopBuffer(true, $nl, $quit, $opts);
}
Expand All @@ -747,7 +743,7 @@ public static function writef(string $format, ...$args): int
*
* @param string|array $messages Output message
* @param boolean $nl True 会添加换行符, False 原样输出,不添加换行符
* @param int|boolean $quit If is int, setting it is exit code. 'True' translate as code 0 and exit, 'False' will not exit.
* @param int|bool $quit If is int, setting it is exit code. 'True' translate as code 0 and exit, 'False' will not exit.
* @param array $opts Some options for write
* refer:
* [
Expand All @@ -758,7 +754,7 @@ public static function writef(string $format, ...$args): int
*
* @return int
*/
public static function write($messages, $nl = true, $quit = false, array $opts = []): int
public static function write($messages, bool $nl = true, $quit = false, array $opts = []): int
{
return Console::write($messages, $nl, $quit, $opts);
}
Expand Down Expand Up @@ -801,7 +797,7 @@ public static function writeln($message, $quit = false, array $opts = []): int
*
* @return int
*/
public static function colored($message, string $style = 'info', $nl = true, array $opts = []): int
public static function colored($message, string $style = 'info', bool $nl = true, array $opts = []): int
{
$quit = isset($opts['quit']) ? (bool)$opts['quit'] : false;

Expand All @@ -817,7 +813,7 @@ public static function colored($message, string $style = 'info', $nl = true, arr
*
* @return array
*/
public static function getBlockMethods($onlyKey = true): array
public static function getBlockMethods(bool $onlyKey = true): array
{
return $onlyKey ? array_keys(self::$blockMethods) : self::$blockMethods;
}
Expand Down

0 comments on commit 6934fc4

Please sign in to comment.