Skip to content

Commit a1ea50c

Browse files
committed
update add new show method: pointing, tree, splitLine
1 parent 03c3721 commit a1ea50c

File tree

5 files changed

+191
-20
lines changed

5 files changed

+191
-20
lines changed

examples/Controllers/HomeController.php

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,17 +163,17 @@ public function artFontCommand()
163163
public function counterCommand()
164164
{
165165
$total = 120;
166-
$ctr = Show::counterTxt('handling ...', 'handled.');
166+
$ctt = Show::counterTxt('handling ...', 'handled.');
167167
$this->write('Counter:');
168168

169169
while ($total - 1) {
170-
$ctr->send(1);
170+
$ctt->send(1);
171171
usleep(30000);
172172
$total--;
173173
}
174174

175175
// end of the counter.
176-
$ctr->send(-1);
176+
$ctt->send(-1);
177177

178178
// $this->output->aList($data, 'runtime profile');
179179

@@ -443,6 +443,34 @@ public function tableCommand()
443443
Show::table($data1, 'no head table show');
444444
}
445445

446+
/**
447+
* output format message: tree
448+
*/
449+
public function treeCommand()
450+
{
451+
Show::tree([
452+
123,
453+
true,
454+
false,
455+
null,
456+
'one-level',
457+
'one-level1',
458+
459+
[
460+
'two-level',
461+
'two-level1',
462+
'two-level2',
463+
[
464+
'three-level',
465+
'three-level1',
466+
'three-level2',
467+
],
468+
],
469+
470+
'one-level99',
471+
]);
472+
}
473+
446474
/**
447475
* output format message: padding
448476
*/

src/Components/Formatter/Tree.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2018-01-02
6+
* Time: 11:38
7+
*/
8+
9+
namespace Inhere\Console\Components\Formatter;
10+
11+
/**
12+
* Class Tree
13+
* @package Inhere\Console\Components\Formatter
14+
*/
15+
class Tree extends Formatter
16+
{
17+
18+
}

src/Utils/FormatUtil.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,23 @@
1414
*/
1515
final class FormatUtil
1616
{
17+
/**
18+
* @param mixed $val
19+
* @return string
20+
*/
21+
public static function typeToString($val)
22+
{
23+
if (null === $val) {
24+
return '(Null)';
25+
}
26+
27+
if (\is_bool($val)) {
28+
return $val ? '(True)' : '(False)';
29+
}
30+
31+
return (string)$val;
32+
}
33+
1734
/**
1835
* to camel
1936
* @param string $name

src/Utils/Helper.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,17 @@ public static function strLen($string)
217217
return mb_strwidth($string, $encoding);
218218
}
219219

220+
/**
221+
* @param string $string
222+
* @param int $indent
223+
* @param string $padStr
224+
* @return string
225+
*/
226+
public static function strPad($string, $indent, $padStr)
227+
{
228+
return $indent > 0 ? str_pad($string, $indent, $padStr) : $string;
229+
}
230+
220231
/**
221232
* findValueByNodes
222233
* @param array $data

src/Utils/Show.php

Lines changed: 114 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,22 @@ public static function __callStatic($method, array $args = [])
184184
* Output Format Message(section/list/helpPanel/panel/table)
185185
**************************************************************************************************/
186186

187+
/**
188+
* @param string $title
189+
* @param string $char
190+
* @param int $width
191+
*/
192+
public static function splitLine(string $title, string $char = '-', int $width = 0)
193+
{
194+
if ($width <= 0) {
195+
list($width,) = CliUtil::getScreenSize();
196+
}
197+
198+
$length = $width - Helper::strLen($title) + 2;
199+
200+
self::write(str_pad('' . ucwords($title) . ' ', $length, $char, STR_PAD_BOTH));
201+
}
202+
187203
/**
188204
* @param string $title The title text
189205
* @param array $opts
@@ -201,22 +217,22 @@ public static function title($title, array $opts = [])
201217
// list($sW, $sH) = Helper::getScreenSize();
202218
$width = (int)$opts['width'];
203219
$char = trim($opts['char']);
204-
$indent = (int)$opts['indent'] > 0 ? $opts['indent'] : 2;
205-
$indentStr = str_pad(self::CHAR_SPACE, $indent, self::CHAR_SPACE);
220+
$indent = (int)$opts['indent'] >= 0 ? $opts['indent'] : 2;
221+
$indentStr = Helper::strPad(self::CHAR_SPACE, $indent, self::CHAR_SPACE);
206222

207223
$title = ucwords(trim($title));
208224
$tLength = Helper::strLen($title);
209225
$width = $width > 10 ? $width : 80;
210226

211227
// title position
212228
if ($tLength >= $width) {
213-
$titleIndent = str_pad(self::CHAR_SPACE, $indent, self::CHAR_SPACE);
229+
$titleIndent = Helper::strPad(self::CHAR_SPACE, $indent, self::CHAR_SPACE);
214230
} elseif ($opts['titlePos'] === self::POS_RIGHT) {
215-
$titleIndent = str_pad(self::CHAR_SPACE, ceil($width - $tLength) + $indent, self::CHAR_SPACE);
231+
$titleIndent = Helper::strPad(self::CHAR_SPACE, ceil($width - $tLength) + $indent, self::CHAR_SPACE);
216232
} elseif ($opts['titlePos'] === self::POS_MIDDLE) {
217-
$titleIndent = str_pad(self::CHAR_SPACE, ceil(($width - $tLength) / 2) + $indent, self::CHAR_SPACE);
233+
$titleIndent = Helper::strPad(self::CHAR_SPACE, ceil(($width - $tLength) / 2) + $indent, self::CHAR_SPACE);
218234
} else {
219-
$titleIndent = str_pad(self::CHAR_SPACE, $indent, self::CHAR_SPACE);
235+
$titleIndent = Helper::strPad(self::CHAR_SPACE, $indent, self::CHAR_SPACE);
220236
}
221237

222238
$titleLine = "$titleIndent<bold>$title</bold>\n";
@@ -244,22 +260,22 @@ public static function section($title, $body, array $opts = [])
244260
// list($sW, $sH) = Helper::getScreenSize();
245261
$width = (int)$opts['width'];
246262
$char = trim($opts['char']);
247-
$indent = (int)$opts['indent'] > 0 ? $opts['indent'] : 2;
248-
$indentStr = str_pad(self::CHAR_SPACE, $indent, self::CHAR_SPACE);
263+
$indent = (int)$opts['indent'] >= 0 ? $opts['indent'] : 2;
264+
$indentStr = Helper::strPad(self::CHAR_SPACE, $indent, self::CHAR_SPACE);
249265

250266
$title = ucwords(trim($title));
251267
$tLength = Helper::strLen($title);
252268
$width = $width > 10 ? $width : 80;
253269

254270
// title position
255271
if ($tLength >= $width) {
256-
$titleIndent = str_pad(self::CHAR_SPACE, $indent, self::CHAR_SPACE);
272+
$titleIndent = Helper::strPad(self::CHAR_SPACE, $indent, self::CHAR_SPACE);
257273
} elseif ($opts['titlePos'] === self::POS_RIGHT) {
258274
$titleIndent = str_pad(self::CHAR_SPACE, ceil($width - $tLength) + $indent, self::CHAR_SPACE);
259275
} elseif ($opts['titlePos'] === self::POS_MIDDLE) {
260276
$titleIndent = str_pad(self::CHAR_SPACE, ceil(($width - $tLength) / 2) + $indent, self::CHAR_SPACE);
261277
} else {
262-
$titleIndent = str_pad(self::CHAR_SPACE, $indent, self::CHAR_SPACE);
278+
$titleIndent = Helper::strPad(self::CHAR_SPACE, $indent, self::CHAR_SPACE);
263279
}
264280

265281
$tpl = "%s\n%s%s\n%s";// title topBorder body bottomBorder
@@ -635,12 +651,54 @@ public static function panel($data, $title = 'Information Panel', array $opts =
635651

636652
/**
637653
* @todo un-completed
654+
* ├ ─ ─
655+
* └ ─
638656
* @param array $data
639657
* @param array $opts
640658
*/
641659
public static function tree(array $data, array $opts = [])
642660
{
661+
static $counter = 0;
662+
static $started = 1;
643663

664+
if ($started) {
665+
$started = 0;
666+
$opts = array_merge([
667+
// 'char' => Helper::supportColor() ? '─' : '-', // ——
668+
'char' => '-',
669+
'prefix' => Helper::supportColor() ? '' : '|',
670+
'leftPadding' => '',
671+
], $opts);
672+
673+
$opts['_level'] = 1;
674+
$opts['_is_main'] = true;
675+
676+
self::startBuffer();
677+
}
678+
679+
foreach ($data as $key => $value) {
680+
if (is_scalar($value)) {
681+
$counter++;
682+
$leftString = $opts['leftPadding'] . str_pad($opts['prefix'], $opts['_level'] + 1, $opts['char']);
683+
684+
self::write($leftString . ' ' . FormatUtil::typeToString($value));
685+
} elseif (\is_array($value)) {
686+
$newOpts = $opts;
687+
$newOpts['_is_main'] = false;
688+
$newOpts['_level']++;
689+
690+
self::tree($value, $newOpts);
691+
}
692+
}
693+
694+
if ($opts['_is_main']) {
695+
self::write('node count: ' . $counter);
696+
// var_dump('f');
697+
self::flushBuffer();
698+
699+
// reset.
700+
$counter = $started = 0;
701+
}
644702
}
645703

646704
/**
@@ -875,12 +933,10 @@ public static function loading($msg = 'Loading ', $ended = false)
875933
* show a pending message
876934
* ```php
877935
* $total = 8000;
878-
*
879936
* while ($total--) {
880937
* Show::pending();
881938
* usleep(200);
882939
* }
883-
*
884940
* Show::pending('Done', true);
885941
* ```
886942
* @param string $msg
@@ -913,8 +969,53 @@ public static function pending($msg = 'Pending ', $ended = false)
913969
}
914970
}
915971

972+
/**
973+
* show a pending message
974+
* ```php
975+
* $total = 8000;
976+
* while ($total--) {
977+
* Show::pointing();
978+
* usleep(200);
979+
* }
980+
* Show::pointing('Total', true);
981+
* ```
982+
* @param string $msg
983+
* @param bool $ended
984+
* @return int
985+
*/
986+
public static function pointing($msg = 'handling ', $ended = false)
987+
{
988+
static $counter = 0;
989+
990+
if ($ended) {
991+
return printf(' %s %d', $msg ?: 'Total', $counter);
992+
}
993+
994+
if ($counter === 0 && $msg) {
995+
echo $msg;
996+
}
997+
998+
$counter++;
999+
1000+
return print '.';
1001+
}
1002+
9161003
/**
9171004
* 与文本进度条相比,没有 total
1005+
* ```php
1006+
* $total = 120;
1007+
* $ctt = Show::counterTxt('handling ...', 'handled.');
1008+
* $this->write('Counter:');
1009+
*
1010+
* while ($total - 1) {
1011+
* $ctt->send(1);
1012+
* usleep(30000);
1013+
* $total--;
1014+
* }
1015+
*
1016+
* // end of the counter.
1017+
* $ctt->send(-1);
1018+
* ```
9181019
* @param string $msg
9191020
* @param string|null $doneMsg
9201021
* @return \Generator
@@ -934,15 +1035,11 @@ public static function counterTxt($msg, $doneMsg = null)
9341035

9351036
$step = yield;
9361037

937-
if ((int)$step === self::FINISHED) {
1038+
if ((int)$step <= 0) {
9381039
$counter++;
9391040
$finished = true;
9401041
$msg = $doneMsg ?: $msg;
9411042
} else {
942-
if ((int)$step <= 0) {
943-
$step = 1;
944-
}
945-
9461043
$counter += $step;
9471044
}
9481045

0 commit comments

Comments
 (0)