Skip to content

Commit 6934fc4

Browse files
committed
add new method for print JSON
1 parent 53485de commit 6934fc4

File tree

4 files changed

+44
-33
lines changed

4 files changed

+44
-33
lines changed

src/Component/Progress/CounterText.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class CounterText extends NotifyMessage
3535
*
3636
* @return Generator
3737
*/
38-
public static function gen(string $msg, $doneMsg = ''): Generator
38+
public static function gen(string $msg, string $doneMsg = ''): Generator
3939
{
4040
$counter = 0;
4141
$finished = false;

src/Concern/FormatOutputAwareTrait.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace Inhere\Console\Concern;
1010

1111
use Inhere\Console\Console;
12+
use Toolkit\Stdlib\Helper\JsonHelper;
1213
use Toolkit\Stdlib\Php;
1314
use function array_merge;
1415
use function json_encode;
@@ -110,6 +111,19 @@ public function json(
110111
return $string;
111112
}
112113

114+
/**
115+
* @param mixed $data
116+
* @param string $title
117+
*/
118+
public function prettyJSON($data, string $title = 'JSON:'): void
119+
{
120+
if ($title) {
121+
Console::colored($title, 'ylw0');
122+
}
123+
124+
Console::write(JsonHelper::prettyJSON($data));
125+
}
126+
113127
/**
114128
* @param mixed ...$vars
115129
*/

src/Concern/StyledOutputAwareTrait.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
* @method checkbox(string $description, $options, $default = null, bool $allowExit = true): array
6060
* @method ask(string $question, string $default = '', Closure $validator = null): string
6161
* @method askPassword(string $prompt = 'Enter Password:'): string
62+
* @see Interact
6263
*/
6364
trait StyledOutputAwareTrait
6465
{

src/Util/Show.php

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Toolkit\Cli\Cli;
2222
use Toolkit\Cli\ColorTag;
2323
use Toolkit\Cli\Style;
24+
use Toolkit\Stdlib\Helper\JsonHelper;
2425
use Toolkit\Stdlib\Math;
2526
use Toolkit\Stdlib\Str;
2627
use Toolkit\Sys\Sys;
@@ -30,17 +31,13 @@
3031
use function implode;
3132
use function is_array;
3233
use function is_string;
33-
use function json_encode;
3434
use function microtime;
3535
use function sprintf;
3636
use function strlen;
3737
use function strpos;
3838
use function strtoupper;
3939
use function substr;
4040
use function ucwords;
41-
use const JSON_PRETTY_PRINT;
42-
use const JSON_UNESCAPED_SLASHES;
43-
use const JSON_UNESCAPED_UNICODE;
4441
use const PHP_EOL;
4542

4643
/**
@@ -208,17 +205,15 @@ public static function __callStatic(string $method, array $args = [])
208205
* Print JSON
209206
*
210207
* @param mixed $data
211-
* @param int $flags
212-
*
213-
* @return int
208+
* @param string $title
214209
*/
215-
public static function prettyJSON(
216-
$data,
217-
int $flags = JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES
218-
): int {
219-
$string = (string)json_encode($data, $flags);
210+
public static function prettyJSON($data, string $title = 'JSON:'): void
211+
{
212+
if ($title) {
213+
Console::colored($title, 'ylw0');
214+
}
220215

221-
return Console::write($string);
216+
Console::write(JsonHelper::prettyJSON($data));
222217
}
223218

224219
/**
@@ -424,7 +419,7 @@ public static function table(array $data, string $title = 'Data Table', array $o
424419
* @param string $msg
425420
* @param bool $ended
426421
*/
427-
public static function spinner(string $msg = '', $ended = false): void
422+
public static function spinner(string $msg = '', bool $ended = false): void
428423
{
429424
static $chars = '-\|/';
430425
static $counter = 0;
@@ -457,7 +452,7 @@ public static function spinner(string $msg = '', $ended = false): void
457452
* @param string $msg
458453
* @param bool $ended
459454
*/
460-
public static function loading(string $msg = 'Loading ', $ended = false): void
455+
public static function loading(string $msg = 'Loading ', bool $ended = false): void
461456
{
462457
self::pending($msg, $ended);
463458
}
@@ -476,7 +471,7 @@ public static function loading(string $msg = 'Loading ', $ended = false): void
476471
* @param string $msg
477472
* @param bool $ended
478473
*/
479-
public static function pending(string $msg = 'Pending ', $ended = false): void
474+
public static function pending(string $msg = 'Pending ', bool $ended = false): void
480475
{
481476
static $counter = 0;
482477
static $lastTime = null;
@@ -516,9 +511,9 @@ public static function pending(string $msg = 'Pending ', $ended = false): void
516511
* @param string $msg
517512
* @param bool $ended
518513
*
519-
* @return int|mixed
514+
* @return int
520515
*/
521-
public static function pointing(string $msg = 'handling ', $ended = false)
516+
public static function pointing(string $msg = 'handling ', bool $ended = false): int
522517
{
523518
static $counter = 0;
524519

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

533528
$counter++;
534529

535-
return print '.';
530+
print '.';
531+
return 0;
536532
}
537533

538534
/**
539535
* 与文本进度条相比,没有 total
540536
*
541537
* @param string $msg
542-
* @param string|null $doneMsg
538+
* @param string $doneMsg
543539
*
544540
* @return Generator
545541
*/
546-
public static function counterTxt(string $msg, $doneMsg = ''): Generator
542+
public static function counterTxt(string $msg, string $doneMsg = ''): Generator
547543
{
548544
return CounterText::gen($msg, $doneMsg);
549545
}
550546

551547
/**
552548
* @param string $doneMsg
553-
* @param string|null $fixMsg
549+
* @param string $fixMsg
554550
*
555551
* @return Generator
556552
*/
557-
public static function dynamicTxt(string $doneMsg, string $fixMsg = null): Generator
553+
public static function dynamicTxt(string $doneMsg, string $fixMsg = ''): Generator
558554
{
559555
return self::dynamicText($doneMsg, $fixMsg);
560556
}
561557

562558
/**
563559
* @param string $doneMsg
564-
* @param string|null $fixedMsg
560+
* @param string $fixedMsg
565561
*
566562
* @return Generator
567563
*/
568-
public static function dynamicText(string $doneMsg, string $fixedMsg = null): Generator
564+
public static function dynamicText(string $doneMsg, string $fixedMsg = ''): Generator
569565
{
570566
return DynamicText::gen($doneMsg, $fixedMsg);
571567
}
@@ -685,14 +681,14 @@ public static function clearBuffer(): void
685681
*
686682
* @param bool $flush Whether flush buffer to output stream
687683
* @param bool $nl Default is False, because the last write() have been added "\n"
688-
* @param bool $quit
684+
* @param bool|int $quit
689685
* @param array $opts
690686
*
691687
* @return null|string If flush = False, will return all buffer text.
692688
* @see Show::write()
693689
* @deprecated Please use \Inhere\Console\Console method instead it.
694690
*/
695-
public static function stopBuffer($flush = true, $nl = false, $quit = false, array $opts = []): ?string
691+
public static function stopBuffer(bool $flush = true, bool $nl = false, $quit = false, array $opts = []): ?string
696692
{
697693
self::$buffering = false;
698694

@@ -720,7 +716,7 @@ public static function stopBuffer($flush = true, $nl = false, $quit = false, arr
720716
* @see Show::write()
721717
* @deprecated Please use \Inhere\Console\Console method instead it.
722718
*/
723-
public static function flushBuffer($nl = false, $quit = false, array $opts = []): void
719+
public static function flushBuffer(bool $nl = false, $quit = false, array $opts = []): void
724720
{
725721
self::stopBuffer(true, $nl, $quit, $opts);
726722
}
@@ -747,7 +743,7 @@ public static function writef(string $format, ...$args): int
747743
*
748744
* @param string|array $messages Output message
749745
* @param boolean $nl True 会添加换行符, False 原样输出,不添加换行符
750-
* @param int|boolean $quit If is int, setting it is exit code. 'True' translate as code 0 and exit, 'False' will not exit.
746+
* @param int|bool $quit If is int, setting it is exit code. 'True' translate as code 0 and exit, 'False' will not exit.
751747
* @param array $opts Some options for write
752748
* refer:
753749
* [
@@ -758,7 +754,7 @@ public static function writef(string $format, ...$args): int
758754
*
759755
* @return int
760756
*/
761-
public static function write($messages, $nl = true, $quit = false, array $opts = []): int
757+
public static function write($messages, bool $nl = true, $quit = false, array $opts = []): int
762758
{
763759
return Console::write($messages, $nl, $quit, $opts);
764760
}
@@ -801,7 +797,7 @@ public static function writeln($message, $quit = false, array $opts = []): int
801797
*
802798
* @return int
803799
*/
804-
public static function colored($message, string $style = 'info', $nl = true, array $opts = []): int
800+
public static function colored($message, string $style = 'info', bool $nl = true, array $opts = []): int
805801
{
806802
$quit = isset($opts['quit']) ? (bool)$opts['quit'] : false;
807803

@@ -817,7 +813,7 @@ public static function colored($message, string $style = 'info', $nl = true, arr
817813
*
818814
* @return array
819815
*/
820-
public static function getBlockMethods($onlyKey = true): array
816+
public static function getBlockMethods(bool $onlyKey = true): array
821817
{
822818
return $onlyKey ? array_keys(self::$blockMethods) : self::$blockMethods;
823819
}

0 commit comments

Comments
 (0)