Skip to content

Commit 4fcde17

Browse files
committed
update, new add some show methods
1 parent 0cd48f3 commit 4fcde17

File tree

10 files changed

+455
-56
lines changed

10 files changed

+455
-56
lines changed

examples/HomeController.php

Lines changed: 109 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@
77
use Inhere\Console\Utils\AnsiCode;
88
use Inhere\Console\Utils\Download;
99
use Inhere\Console\Utils\Helper;
10-
use Inhere\Console\Utils\Show;
1110
use Inhere\Console\Utils\Interact;
11+
use Inhere\Console\Utils\Show;
1212

1313
/**
1414
* default command controller. there are some command usage examples(1)
15-
*
1615
* Class HomeController
1716
* @package Inhere\Console\examples
1817
*/
@@ -77,7 +76,6 @@ public function blockMsgCommand()
7776

7877
/**
7978
* a progress bar example show
80-
*
8179
* @options
8280
* --type the progress type, allow: bar,txt. <cyan>txt</cyan>
8381
* --done-char the done show char. <info>=</info>
@@ -189,6 +187,112 @@ public function fmtMsgCommand()
189187
], 'table show');
190188
}
191189

190+
/**
191+
* a example for display a table
192+
*/
193+
public function tableCommand()
194+
{
195+
$data = [
196+
[
197+
'id' => 1,
198+
'name' => 'john',
199+
'status' => 2,
200+
'email' => '[email protected]',
201+
],
202+
[
203+
'id' => 2,
204+
'name' => 'tom',
205+
'status' => 0,
206+
'email' => '[email protected]',
207+
],
208+
[
209+
'id' => 3,
210+
'name' => 'jack',
211+
'status' => 1,
212+
'email' => '[email protected]',
213+
],
214+
];
215+
Show::table($data, 'table show');
216+
217+
Show::table($data, 'No border table show', [
218+
'showBorder' => 0
219+
]);
220+
221+
Show::table($data, 'change style table show', [
222+
'bodyStyle' => 'info'
223+
]);
224+
225+
$data1 = [
226+
[
227+
'Walter White',
228+
'Father',
229+
'Teacher',
230+
],
231+
[
232+
'Skyler White',
233+
'Mother',
234+
'Accountant',
235+
],
236+
[
237+
'Walter White Jr.',
238+
'Son',
239+
'Student',
240+
],
241+
];
242+
243+
Show::table($data1, 'no head table show');
244+
}
245+
246+
/**
247+
* a example use padding() for show data
248+
*/
249+
public function paddingCommand()
250+
{
251+
$data = [
252+
'Eggs' => '$1.99',
253+
'Oatmeal' => '$4.99',
254+
'Bacon' => '$2.99',
255+
];
256+
257+
Show::padding($data, 'padding data show');
258+
}
259+
260+
/**
261+
* a example for dump, print, json data
262+
*/
263+
public function jsonCommand()
264+
{
265+
$data = [
266+
[
267+
'id' => 1,
268+
'name' => 'john',
269+
'status' => 2,
270+
'email' => '[email protected]',
271+
],
272+
[
273+
'id' => 2,
274+
'name' => 'tom',
275+
'status' => 0,
276+
'email' => '[email protected]',
277+
],
278+
[
279+
'id' => 3,
280+
'name' => 'jack',
281+
'status' => 1,
282+
'email' => '[email protected]',
283+
],
284+
];
285+
286+
$this->output->write('use dump:');
287+
$this->output->dump($data);
288+
289+
$this->output->write('use print:');
290+
$this->output->print($data);
291+
292+
$this->output->write('use json:');
293+
$this->output->json($data);
294+
}
295+
192296
/**
193297
* a example for use arguments on command
194298
* @usage home:useArg [arg1=val1 arg2=arg2] [options]
@@ -225,12 +329,11 @@ protected function defArgConfigure()
225329
*/
226330
public function defArgCommand()
227331
{
228-
$this->output->dumpVars($this->input->getArgs(), $this->input->getOpts(), $this->input->getBoolOpt('y'));
332+
$this->output->dump($this->input->getArgs(), $this->input->getOpts(), $this->input->getBoolOpt('y'));
229333
}
230334

231335
/**
232336
* use <red>Interact::confirm</red> method
233-
*
234337
*/
235338
public function confirmCommand()
236339
{
@@ -241,7 +344,6 @@ public function confirmCommand()
241344

242345
/**
243346
* example for use <magenta>Interact::select</magenta> method
244-
*
245347
*/
246348
public function selectCommand()
247349
{
@@ -258,7 +360,7 @@ public function envCommand()
258360
{
259361
$info = [
260362
'phpVersion' => PHP_VERSION,
261-
'env' => 'test',
363+
'env' => 'test',
262364
'debug' => true,
263365
];
264366

@@ -269,7 +371,6 @@ public function envCommand()
269371

270372
/**
271373
* download a file to local
272-
*
273374
* @usage {command} url=url saveTo=[saveAs] type=[bar|text]
274375
* @example {command} url=https://github.com/inhere/php-librarys/archive/v2.0.1.zip type=bar
275376
*/

examples/app

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#!/usr/bin/env php
22
<?php
33

4-
define('PROJECT_PATH', dirname(__DIR__));
4+
define('BASE_PATH', dirname(__DIR__));
55

66
require __DIR__ . '/s-autoload.php';
77

88
// create app instance
99
$app = new \Inhere\Console\Application([
1010
'debug' => true,
11-
'rootPath' => PROJECT_PATH,
11+
'rootPath' => BASE_PATH,
1212
]);
1313

1414
// require dirname(__DIR__) . '/boot/cli-services.php';

images/table-show.png

161 KB
Loading

src/IO/Output.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -74,22 +74,6 @@ public function stderr($text = '', $nl = true)
7474
return $this;
7575
}
7676

77-
/**
78-
* @param array ...$args
79-
*/
80-
public function dumpVars(...$args)
81-
{
82-
echo Helper::dumpVars(...$args);
83-
}
84-
85-
/**
86-
* @param array ...$args
87-
*/
88-
public function printVars(...$args)
89-
{
90-
echo Helper::printVars(...$args);
91-
}
92-
9377
/////////////////////////////////////////////////////////////////
9478
/// Getter/Setter
9579
/////////////////////////////////////////////////////////////////

src/Traits/FormatOutputTrait.php

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

1111
use Inhere\Console\Style\Style;
12+
use Inhere\Console\Utils\Helper;
1213
use Inhere\Console\Utils\Show;
1314

1415
/**
@@ -193,4 +194,36 @@ public function __call($method, array $args = [])
193194

194195
throw new \LogicException("Call a not exists method: $method of the " . static::class);
195196
}
197+
198+
/**
199+
* @param mixed $data
200+
* @param bool $echo
201+
* @return int|string
202+
*/
203+
public function json($data, $echo = true)
204+
{
205+
$string = json_encode($data, JSON_PRETTY_PRINT);
206+
207+
if ($echo) {
208+
return Show::write($string);
209+
}
210+
211+
return $string;
212+
}
213+
214+
/**
215+
* @param array ...$vars
216+
*/
217+
public function dump(...$vars)
218+
{
219+
Show::write(Helper::dumpVars(...$vars));
220+
}
221+
222+
/**
223+
* @param array ...$vars
224+
*/
225+
public function print(...$vars)
226+
{
227+
Show::write(Helper::printVars(...$vars));
228+
}
196229
}

src/Utils/ArtFont.php

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017-10-24
6+
* Time: 9:23
7+
*/
8+
9+
namespace Inhere\Console\Utils;
10+
11+
/**
12+
* Class ArtFont
13+
* @package Inhere\Console\Utils
14+
*/
15+
class ArtFont
16+
{
17+
private static $instance;
18+
19+
/**
20+
* @var array
21+
*/
22+
private $artPaths = [];
23+
24+
/**
25+
* @var array
26+
*/
27+
private $fonts = [];
28+
29+
/**
30+
* @return self
31+
*/
32+
public static function create(): self
33+
{
34+
if (!self::$instance) {
35+
self::$instance = new self();
36+
}
37+
38+
return self::$instance;
39+
}
40+
41+
/**
42+
* @param string $name
43+
* @param array $opts
44+
*/
45+
public function draw(string $name, array $opts = [])
46+
{
47+
48+
}
49+
50+
public function addFont(string $name, string $content)
51+
{
52+
if ($name && $content) {
53+
$this->fonts[$name] = $content;
54+
}
55+
}
56+
57+
/**
58+
* @param string $path
59+
*/
60+
public function addPath(string $path)
61+
{
62+
if (file_exists($path)) {
63+
$this->artPaths[] = $path;
64+
}
65+
}
66+
67+
/**
68+
* @return array
69+
*/
70+
public function getArtPaths(): array
71+
{
72+
return $this->artPaths;
73+
}
74+
75+
/**
76+
* @param array $artPaths
77+
*/
78+
public function setArtPaths(array $artPaths)
79+
{
80+
foreach ($artPaths as $path) {
81+
$this->addPath($path);
82+
}
83+
}
84+
85+
/**
86+
* @return array
87+
*/
88+
public function getFonts(): array
89+
{
90+
return $this->fonts;
91+
}
92+
93+
/**
94+
* @param array $fonts
95+
*/
96+
public function setFonts(array $fonts)
97+
{
98+
foreach ($fonts as $name => $font) {
99+
$this->addFont($name, $font);
100+
}
101+
}
102+
}

0 commit comments

Comments
 (0)