Skip to content

Commit 74f460c

Browse files
committed
update some screenshots. begin prepare nofity components
1 parent 6e24311 commit 74f460c

File tree

20 files changed

+329
-51
lines changed

20 files changed

+329
-51
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,12 @@ $app->run();
9292

9393
然后在命令行里执行 `php examples/app`, 立即就可以看到如下输出了:
9494

95-
!['output-commands-info'](images/example-app.png)
95+
!['app-command-list'](docs/screenshots/app-command-list.png)
9696

9797
> `Independent Commands` 中的 demo 就是我们上面添加的命令
9898
99+
- `[alias: ...]` 命令最后的alias 表明了此命令拥有的别名。
100+
99101
## 添加命令
100102

101103
添加命令的方式有三种
@@ -206,7 +208,7 @@ class HomeController extends Controller
206208
207209
- 运行效果(by `php examples/app home`):
208210

209-
![command-group-example](./images/example-for-group.png)
211+
![group-command-list](docs/screenshots/group-command-list.png)
210212

211213
更多请查看 [examples](./examples) 中的示例代码和在目录下运行示例 `php examples/app` 来查看效果
212214

@@ -429,7 +431,7 @@ while ($i <= $total) {
429431
}
430432
```
431433

432-
![show-progress](images/show-progress.png)
434+
![show-progress](docs/screenshots/progress-demo.png)
433435

434436
### 列表数据展示输出
435437

README_en.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ $app->run();
7272

7373
now, you can see:
7474

75-
!['output-commands-info'](images/example-app.png)
75+
!['app-command-list'](docs/screenshots/app-command-list.png)
7676

7777
## input
7878

293 KB
Loading
326 KB
Loading

docs/screenshots/progress-demo.png

189 KB
Loading

examples/Controllers/HomeController.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919
class HomeController extends Controller
2020
{
21-
protected static $description = 'default command controller. there are some command usage examples(2)';
21+
protected static $description = 'This is a demo command controller. there are some command usage examples(2)';
2222

2323
/**
2424
* @return array
@@ -123,7 +123,7 @@ public function artFontCommand()
123123
}
124124

125125
/**
126-
* a counter example show. It is like progress txt, but no max value.
126+
* dynamic notice message show: counterTxt. It is like progress txt, but no max value.
127127
* @example
128128
* {script} {command}
129129
* @return int
@@ -146,6 +146,32 @@ public function counterCommand()
146146
return 0;
147147
}
148148

149+
/**
150+
* dynamic notice message show: spinner
151+
*/
152+
public function spinnerCommand()
153+
{
154+
$total = 5000;
155+
156+
while ($total--) {
157+
Show::spinner();
158+
usleep(100);
159+
}
160+
}
161+
162+
/**
163+
* dynamic notice message show: pending
164+
*/
165+
public function pendingCommand()
166+
{
167+
$total = 5000;
168+
169+
while ($total--) {
170+
Show::spinner();
171+
usleep(100);
172+
}
173+
}
174+
149175
/**
150176
* a progress bar example show
151177
* @options
@@ -504,7 +530,7 @@ public function limitedAskCommand()
504530
}
505531

506532
/**
507-
* This is a demo for input password on command line. use: <magenta>Interact::askPassword()</magenta>
533+
* This is a demo for input password. use: <magenta>Interact::askPassword()</magenta>
508534
* @usage {fullCommand}
509535
*/
510536
public function pwdCommand()

examples/app

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ $app = new \Inhere\Console\Application([
1111
'rootPath' => dirname(__DIR__),
1212
]);
1313

14-
$app->setLogoText('
15-
_ _ ___ _ _
16-
| || | / _ \| || |
17-
| || |_| | | | || |_
18-
|__ _| | | |__ _|
19-
| | | |_| | | |
20-
|_| \___/ |_|
21-
');
14+
$app->setLogo("
15+
________ ____ ___ ___ __ _
16+
/ ____/ / / _/ / | ____ ____ / (_)________ _/ /_(_)___ ____
17+
/ / / / / / / /| | / __ \/ __ \/ / / ___/ __ `/ __/ / __ \/ __ \
18+
/ /___/ /____/ / / ___ |/ /_/ / /_/ / / / /__/ /_/ / /_/ / /_/ / / / /
19+
\____/_____/___/ /_/ |_/ .___/ .___/_/_/\___/\__,_/\__/_/\____/_/ /_/
20+
/_/ /_/
21+
", 'success');
2222

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

examples/demo/cli-spinner.php

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,102 @@
44
* User: inhere
55
* Date: 2017-12-21
66
* Time: 10:02
7-
*/
7+
* @link https://github.com/dealnews/Console
8+
*/
9+
10+
class Status
11+
{
12+
public static function clearLine()
13+
{
14+
echo "\033[2K"; // delete the current line
15+
echo "\r"; // return the cursor to the beginning of the line
16+
}
17+
18+
/**
19+
* Spinner that updates every call up to 4 per second
20+
*
21+
* .*.
22+
*/
23+
public static function spinner()
24+
{
25+
static $spinner = 0;
26+
static $mtime = null;
27+
28+
// static $chars = '-\|/';
29+
static $chars = '-.*.-';
30+
31+
$now = microtime(true);
32+
if (null === $mtime || ($mtime < $now - 0.1)) {
33+
$mtime = $now;
34+
self::clearLine();
35+
echo $chars[$spinner];
36+
$spinner++;
37+
38+
if ($spinner > strlen($chars) - 1) {
39+
$spinner = 0;
40+
}
41+
}
42+
}
43+
44+
/**
45+
* Uses `stty` to hide input/output completely.
46+
* @param boolean $hidden Will hide/show the next data. Defaults to true.
47+
*/
48+
public static function hide($hidden = true)
49+
{
50+
system( 'stty ' . ( $hidden? '-echo' : 'echo' ) );
51+
}
52+
53+
/**
54+
* Prompts the user for input. Optionally masking it.
55+
*
56+
* @param string $prompt The prompt to show the user
57+
* @param bool $masked If true, the users input will not be shown. e.g. password input
58+
* @param int $limit The maximum amount of input to accept
59+
* @return string
60+
*/
61+
public static function prompt($prompt, $masked=false, $limit=100)
62+
{
63+
echo "$prompt: ";
64+
if ($masked) {
65+
`stty -echo`; // disable shell echo
66+
}
67+
$buffer = "";
68+
$char = "";
69+
$f = fopen('php://stdin', 'r');
70+
while (strlen($buffer) < $limit) {
71+
$char = fread($f, 1);
72+
if ($char === "\n" || $char === "\r") {
73+
break;
74+
}
75+
$buffer.= $char;
76+
}
77+
if ($masked) {
78+
`stty echo`; // enable shell echo
79+
echo "\n";
80+
}
81+
return $buffer;
82+
}
83+
}
84+
85+
Status::hide();
86+
echo 'Password: ';
87+
$input = fgets(STDIN);
88+
Status::hide(false);
89+
echo $input;
90+
die;
91+
$total = random_int(5000, 10000);
92+
for ($x=1; $x<=$total; $x++) {
93+
Status::spinner();
94+
usleep(50);
95+
}
96+
97+
Status::clearLine();
98+
99+
//
100+
// $answer = Status::prompt("What is the secret word?", 0);
101+
// if ($answer == "secret") {
102+
// echo "Yay! You got it!";
103+
// } else {
104+
// echo "Boo! That is wrong!";
105+
// }

src/Base/AbstractApplication.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ abstract class AbstractApplication implements ApplicationInterface
4545
* @var array
4646
*/
4747
private $meta = [
48-
'name' => 'My Console',
48+
'name' => 'My Console Application',
4949
'debug' => false,
5050
'profile' => false,
5151
'version' => '0.5.1',
@@ -374,7 +374,7 @@ public function showVersionInfo($quit = true)
374374
}
375375

376376
$this->output->aList([
377-
"\n <info>{$name}</info>, Version <comment>$version</comment>\n$logo",
377+
"$logo\n <info>{$name}</info>, Version <comment>$version</comment>\n",
378378
'System Info' => "PHP version <info>$phpVersion</info>, on <info>$os</info> system",
379379
'Application Info' => "Update at <info>$updateAt</info>, publish at <info>$publishAt</info>(current $date)",
380380
], null, [
@@ -612,10 +612,15 @@ public function getLogoText()
612612

613613
/**
614614
* @param string $logoTxt
615+
* @param string|null $style
615616
*/
616-
public function setLogoText(string $logoTxt)
617+
public function setLogo(string $logoTxt, string $style = null)
617618
{
618619
$this->meta['logoText'] = $logoTxt;
620+
621+
if ($style) {
622+
$this->meta['logoStyle'] = $style;
623+
}
619624
}
620625

621626
/**

src/Components/ArtFont.php

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
namespace Inhere\Console\Components;
1010

11-
use Inhere\Console\BuiltIn\ArtFonts\ArtFontsBlock;
1211
use Inhere\Console\Utils\Helper;
1312
use Inhere\Console\Utils\Show;
1413

@@ -94,6 +93,18 @@ public function showInternal(string $name, array $opts = [])
9493
return $this->show($name, self::INTERNAL_GROUP, $opts);
9594
}
9695

96+
/**
97+
* @param string $name
98+
* @param string $group
99+
* @return string
100+
*/
101+
public function showItalic(string $name, string $group = null, array $opts = [])
102+
{
103+
$opts['type'] = 'italic';
104+
105+
return $this->show($name . '_italic', $group, $opts);
106+
}
107+
97108
/**
98109
* display the art font
99110
* @param string $name
@@ -155,58 +166,74 @@ public function font(string $name, string $group = null)
155166
return '';
156167
}
157168

169+
158170
/**
159-
* @param string $name
160171
* @param string $group
161-
* @return string
172+
* @param string $path
173+
* @return $this
162174
*/
163-
public function italic(string $name, string $group = null)
175+
public function addGroup(string $group, string $path)
164176
{
165-
return $this->font($name . '_italic', $group);
166-
}
177+
$group = trim($group, '_');
167178

168-
public function addFontsFromPath(string $path, string $group)
169-
{
179+
if (!$group || !is_dir($path)) {
180+
return $this;
181+
}
182+
183+
if (!isset($this->groups[$group])) {
184+
$this->groups[$group] = $path;
185+
}
170186

187+
return $this;
171188
}
172189

173190
/**
174-
* @param string $name
175-
* @param string $file font file path
191+
* @param string $group
192+
* @param string $path
176193
* @return $this
177194
*/
178-
public function addFont(string $name, string $file)
195+
public function setGroup(string $group, string $path)
179196
{
180-
if (is_file($file) && ($txt = trim(file_get_contents($file)))) {
181-
$this->fonts[$name] = $txt;
197+
$group = trim($group, '_');
198+
199+
if (!$group || !is_dir($path)) {
200+
return $this;
182201
}
183202

203+
$this->groups[$group] = $path;
204+
184205
return $this;
185206
}
186207

187208
/**
188209
* @param string $name
189-
* @param string $content
210+
* @param string $file font file path
211+
* @param string|null $group
190212
* @return $this
191213
*/
192-
public function addFontContent(string $name, string $content)
214+
public function addFont(string $name, string $file, string $group = null)
193215
{
194-
if ($name && ($content = trim($content))) {
195-
$this->fontContents[$name] = $content;
216+
$group = $group ?: self::DEFAULT_GROUP;
217+
218+
if (is_file($file)) {
219+
$info = pathinfo($file);
220+
$ext = !empty($info['extension']) ? $info['extension'] : 'txt';
221+
222+
$this->fonts[$group][$name] = $info['dirname'] . '/' . $info['filename'] . '.' . $ext;
196223
}
197224

198225
return $this;
199226
}
200227

201228
/**
202-
* @param string $name Named fonts path
203-
* @param string $path fonts path
204-
* @return ArtFont
229+
* @param string $name
230+
* @param string $content
231+
* @return $this
205232
*/
206-
public function addPath(string $name, string $path)
233+
public function addFontContent(string $name, string $content)
207234
{
208-
if (file_exists($path)) {
209-
$this->groups[$name] = $path;
235+
if ($name && ($content = trim($content))) {
236+
$this->fontContents[$name] = $content;
210237
}
211238

212239
return $this;

0 commit comments

Comments
 (0)