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

Commit 45c5f0f

Browse files
committed
update for console tool class
1 parent 35cba39 commit 45c5f0f

File tree

3 files changed

+181
-54
lines changed

3 files changed

+181
-54
lines changed

src/console/Interact.php

Lines changed: 181 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -23,32 +23,9 @@ class Interact
2323
const TAB = ' ';
2424
const NL_TAB = "\n ";// new line + tab
2525

26-
//////////////////////////////////////// Interactive ////////////////////////////////////////
27-
28-
/**
29-
* 多行信息展示
30-
* @param mixed $data
31-
* @param string $title
32-
* @return void
33-
*/
34-
public static function panel($data, $title='Info panel')
35-
{
36-
$data = is_array($data) ? array_filter($data) : [trim($data)];
37-
self::write("\n " . sprintf(self::STAR_LINE,"<bold>$title</bold>"), false);
38-
39-
foreach ($data as $label => $value) {
40-
$line = ' * ';
41-
if (!is_numeric($label)) {
42-
$line .= "$label: ";
43-
}
44-
45-
self::write($line . $value);
46-
}
47-
48-
$star = $title ? substr(self::STAR_LINE, 0, strlen($title)): '';
49-
50-
self::write(' ' . sprintf(self::STAR_LINE, $star ));
51-
}
26+
/////////////////////////////////////////////////////////////////
27+
/// Interactive
28+
/////////////////////////////////////////////////////////////////
5229

5330
/**
5431
* 多选一
@@ -107,7 +84,7 @@ public static function confirm($question, $default = true)
10784
$defaultText = $default ? 'yes' : 'no';
10885

10986
$message = "<comment>$question</comment>\n Please confirm (yes|no) [default:<info>$defaultText</info>]: ";
110-
static::write($message, false);
87+
self::write($message, false);
11188

11289
$answer = self::readRow();
11390

@@ -137,7 +114,7 @@ public static function ask($question, $default = null, \Closure $validator = nul
137114
}
138115

139116
// $question = ucfirst(trim($question));
140-
static::write(ucfirst(trim($question)));
117+
self::write(ucfirst(trim($question)));
141118
$answer = self::readRow();
142119

143120
if ('' === $answer && null === $default ) {
@@ -206,6 +183,176 @@ public static function loopAsk($question, callable $callbackVerify = null, $allo
206183
return $answer;
207184
}
208185

186+
/////////////////////////////////////////////////////////////////
187+
/// Output Message
188+
/////////////////////////////////////////////////////////////////
189+
190+
/**
191+
* @param $msg
192+
*/
193+
public static function title($msg, $width = null)
194+
{
195+
$msg = ucwords(trim($msg));
196+
$msgLength = mb_strlen($msg, 'UTF-8');
197+
$width = (int)$width ? (int)$width : 50;
198+
199+
$indentSpace = str_pad(' ', ceil($width/2) - ceil($msgLength/2), ' ');
200+
$charStr = str_pad('=', $width, '=');
201+
202+
self::write(" {$indentSpace}{$msg} \n {$charStr}\n");
203+
}
204+
205+
/**
206+
* @param $msg
207+
*/
208+
public static function section($msg, $width = null)
209+
{
210+
$msg = ucwords(trim($msg));
211+
$msgLength = mb_strlen($msg, 'UTF-8');
212+
$width = (int)$width ? (int)$width : 50;
213+
214+
$indentSpace = str_pad(' ', ceil($width/2) - ceil($msgLength/2), ' ');
215+
$charStr = str_pad('-', $width, '-');
216+
217+
self::write(" {$indentSpace}{$msg} \n {$charStr}\n");
218+
}
219+
220+
/**
221+
* 多行信息展示
222+
* @param mixed $data
223+
* @param string $title
224+
* @return void
225+
*/
226+
public static function panel(array $data, $title='Info panel')
227+
{
228+
$data = is_array($data) ? array_filter($data) : [trim($data)];
229+
$title = ucwords(trim($title));
230+
231+
self::write("\n " . sprintf(self::STAR_LINE,"<bold>$title</bold>"), false);
232+
233+
foreach ($data as $label => $value) {
234+
$line = ' * ';
235+
if (!is_numeric($label)) {
236+
$line .= "$label: ";
237+
}
238+
239+
self::write("$line <info>$value</info>");
240+
}
241+
242+
$star = $title ? substr(self::STAR_LINE, 0, strlen($title)): '';
243+
244+
self::write(' ' . sprintf(self::STAR_LINE, $star ));
245+
}
246+
247+
/**
248+
* 表格数据信息展示
249+
* @param array $data
250+
* @param string $title
251+
* @return void
252+
*/
253+
public static function table(array $data, $title='Info List', $showBorder = true)
254+
{
255+
$rowIndex = 0;
256+
$head = $table = [];
257+
$info = [
258+
'rowCount' => count($data),
259+
'columnCount' => 0, // how many column in the table.
260+
'columnMaxWidth' => [], // table column max width
261+
'tableWidth' => 0, // table width. equals to all max column width's sum.
262+
];
263+
264+
// parse table data
265+
foreach ($data as $row) {
266+
// collection all field name
267+
if ($rowIndex === 0) {
268+
$head = array_keys($row);
269+
$info['columnCount'] = count($row);
270+
271+
foreach ($head as $index => $name) {
272+
$info['columnMaxWidth'][$index] = mb_strlen($name, 'UTF-8');
273+
}
274+
}
275+
276+
$colIndex = 0;
277+
278+
foreach ($row as $value) {
279+
// collection column max width
280+
if ( isset($info['columnMaxWidth'][$colIndex]) ) {
281+
$colWidth = mb_strlen($value, 'UTF-8');
282+
283+
// If current column width gt old column width. override old width.
284+
if ($colWidth > $info['columnMaxWidth'][$colIndex]) {
285+
$info['columnMaxWidth'][$colIndex] = $colWidth;
286+
}
287+
} else {
288+
$info['columnMaxWidth'][$colIndex] = mb_strlen($value, 'UTF-8');
289+
}
290+
291+
$colIndex++;
292+
}
293+
294+
$rowIndex++;
295+
}
296+
297+
$tableWidth = $info['tableWidth'] = array_sum($info['columnMaxWidth']);
298+
$columnCount = $info['columnCount'];
299+
300+
// output title
301+
if ($title) {
302+
$title = ucwords(trim($title));
303+
$titleLength = mb_strlen($title, 'UTF-8');
304+
$indentSpace = str_pad(' ', ceil($tableWidth/2) - ceil($titleLength/2) + ($columnCount*2), ' ');
305+
self::write(" {$indentSpace}<bold>{$title}</bold>");
306+
}
307+
308+
// output table top border
309+
if ($showBorder) {
310+
$border = str_pad('-', $tableWidth + ($columnCount*3) + 2, '-');
311+
self::write(' ' . $border);
312+
}
313+
314+
// output table head
315+
$headStr = ' | ';
316+
foreach ($head as $index => $name) {
317+
$colMaxWidth = $info['columnMaxWidth'][$index];
318+
$name = str_pad($name, $colMaxWidth, ' ');
319+
$headStr .= " {$name} |";
320+
}
321+
322+
self::write($headStr);
323+
324+
if ($showBorder) {
325+
self::write(' ' . $border);
326+
}
327+
328+
$rowIndex = 0;
329+
330+
// output table info
331+
foreach ($data as $row) {
332+
$colIndex = 0;
333+
$rowStr = ' | ';
334+
335+
foreach ($row as $value) {
336+
$colMaxWidth = $info['columnMaxWidth'][$colIndex];
337+
$value = str_pad($value, $colMaxWidth, ' ');
338+
$rowStr .= " <info>{$value}</info> |";
339+
$colIndex++;
340+
}
341+
342+
self::write("{$rowStr}");
343+
344+
$rowIndex++;
345+
}
346+
347+
// output table bottom border
348+
if ($showBorder) {
349+
self::write(' ' . $border);
350+
}
351+
352+
echo "\n";
353+
unset($data);
354+
}
355+
209356
public static function primary($messages, $type = 'IMPORTANT')
210357
{
211358
static::block($messages, $type, 'primary');
@@ -261,9 +408,14 @@ public static function block($messages, $type = null, $style='default')
261408
}
262409

263410
// $this->write($text);
264-
static::write($text);
411+
self::write($text);
265412
}
266413

414+
/////////////////////////////////////////////////////////////////
415+
/// Helper Method
416+
/////////////////////////////////////////////////////////////////
417+
418+
267419
/**
268420
* @var Color
269421
*/
@@ -301,30 +453,5 @@ public static function write($text, $newLine=true, $exit=false)
301453
$exit && exit();
302454
}
303455

304-
/**
305-
* @param $msg
306-
*/
307-
public static function title($msg)
308-
{
309-
$msg = ucfirst(trim($msg));
310-
$length = mb_strlen($msg, 'UTF-8');
311-
$str = str_pad('=',$length + 6, '=');
312-
313-
static::write(" $msg ");
314-
static::write($str."\n");
315-
}
316-
317-
/**
318-
* @param $msg
319-
*/
320-
public static function section($msg)
321-
{
322-
$msg = ucfirst(trim($msg));
323-
$length = mb_strlen($msg, 'UTF-8');
324-
$str = str_pad('-',$length + 6, '-');
325-
326-
static::write(" $msg ");
327-
static::write($str."\n");
328-
}
329456

330457
} // end class
-53.5 KB
Binary file not shown.
36.8 KB
Loading

0 commit comments

Comments
 (0)