Skip to content

Commit c6a8d0a

Browse files
committed
update, some bug fixed for dispaly progress bar
1 parent abfbaab commit c6a8d0a

File tree

5 files changed

+38
-21
lines changed

5 files changed

+38
-21
lines changed

README.md

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
# php 命令行应用库
22

3-
简洁功能全面的php命令行应用库。提供控制台参数解析, 颜色风格输出, 用户信息交互, 特殊格式信息显示
3+
简洁、功能全面的php命令行应用库。提供控制台参数解析, 颜色风格输出, 用户信息交互, 特殊格式信息显示
44

5+
- 功能全面的命令行的选项参数解析(命名参数,短选项,长选项 ...)
56
- 命令行应用, 命令行的 `controller`, `command` 解析运行
6-
- 功能全面的命令行的选项参数解析
77
- 命令行中功能强大的 `input`, `output` 管理、使用
88
- 消息文本的多种颜色风格输出支持(`info`, `comment`, `success`, `danger`, `error` ... ...)
9-
- 常用的特殊格式信息显示(`section`, `panel`, `padding`, `help-panel`, `table`, `title`, `list`, `progressBar`)
9+
- 丰富的特殊格式信息显示(`section`, `panel`, `padding`, `help-panel`, `table`, `title`, `list`, `progressBar`)
1010
- 常用的用户信息交互支持(`select`, `confirm`, `ask/question`)
1111
- 命令方法注释自动解析(提取为参数 `arguments` 和 选项 `options` 等信息)
1212
- 类似 `symfony/console` 的预定义参数定义支持(按位置赋予参数值)
13+
- 输出是 windows,linux 兼容的,不支持颜色的环境会自动去除相关CODE
14+
15+
> 下面所有的特性,效果都是运行 `examples/` 中的示例代码 `php examples/app` 展示出来的。下载后可以直接测试体验
1316
1417
## [EN README](./README_en.md)
1518

@@ -97,14 +100,17 @@ $app->command('demo', function (Input $in, Output $out) {
97100
- 通过继承 `Inhere\Console\Command` 添加独立命令
98101

99102
```php
100-
use Inhere\Console\Utils\AnsiCode;
103+
use Inhere\Console\Command;
101104

102105
/**
103106
* Class Test
104107
* @package app\console\commands
105108
*/
106109
class TestCommand extends Command
107110
{
111+
protected static $name = 'test';
112+
protected static $description = 'this is a test independent command';
113+
108114
/**
109115
* execute
110116
* @param Inhere\Console\IO\Input $input
@@ -153,11 +159,15 @@ class HomeController extends Controller
153159
命令组(eg `HomeController`) 中的命令(eg: `indexCommand`)上注释是可被解析的。
154160

155161
- 当你使用 `php examples/app home -h` 时,可以查看到 `HomeController` 的所有命令信息
156-
- 当使用 `php examples/app home/index -h` 时,可以查看到关于 `HomeController::indexCommand` 更详细的信息。包括描述注释文本、`@usage``@example`
162+
- 当使用 `php examples/app home:index -h` 时,可以查看到关于 `HomeController::indexCommand` 更详细的信息。包括描述注释文本、`@usage``@example`
157163

158164
> 小提示:注释里面同样支持带颜色的文本输出 `eg: this is a command's description <info>message</info>`
159165
160-
更多请查看 [examples](./examples) 中的示例代码
166+
- 运行效果(by `php examples/app home`):
167+
168+
![command-group-example](./images/command-group-example.jpg)
169+
170+
更多请查看 [examples](./examples) 中的示例代码和在目录下运行示例 `php examples/app` 来查看效果
161171

162172
## 输入
163173

@@ -166,7 +176,7 @@ class HomeController extends Controller
166176
在终端中执行如下命令,用于演示参数选项等信息的解析:
167177

168178
```bash
169-
$ php examples/app home/useArg status=2 name=john arg0 -s=test --page=23 --id=154 -e dev -v vvv -d -rf --debug --test=false
179+
$ php examples/app home:useArg status=2 name=john arg0 -s=test --page=23 --id=154 -e dev -v vvv -d -rf --debug --test=false
170180
```
171181

172182
**一点说明:**
@@ -187,7 +197,7 @@ $ php examples/app home/useArg status=2 name=john arg0 -s=test --page=23 --id=15
187197

188198
```php
189199
echo $input->getScript(); // 'examples/app' 执行的入口脚本文件
190-
echo $input->getCommand(); // 'home/useArg' 解析到的第一个参数将会被认为是命令名称,并且不会再存入到 参数列表中
200+
echo $input->getCommand(); // 'home:useArg' 解析到的第一个参数将会被认为是命令名称,并且不会再存入到 参数列表中
191201
echo $input->getFullScript(); // 命令行输入的原样字符串
192202
```
193203

@@ -345,7 +355,7 @@ public static function progressBar(int $total, array $opts = [])
345355
$total = 120;
346356
$bar = Show::progressBar($total, [
347357
'msg' => 'Msg Text',
348-
'doneChar' => '#'
358+
'doneChar' => '#'// ♥ ■ ☺ ☻ = # Windows 环境下不要使用特殊字符,否则会乱码
349359
]);
350360
echo "Progress:\n";
351361

@@ -472,7 +482,7 @@ $data = [
472482

473483
$opts = [
474484
'showBorder' => true,
475-
'tHead' => [col1, col2, col3, ...]
485+
'columns' => [col1, col2, col3, ...]
476486
];
477487
Show::table($data, 'a table', $opts);
478488
```

examples/HomeController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public function progressCommand($input)
9999
'signChar' => $input->getOpt('sign-char', '>'),
100100
]);
101101
} else {
102-
$bar = $this->output->progressTxt($total, 'Doing oo oo', 'Done');
102+
$bar = $this->output->progressTxt($total, 'Doing gggg ...', 'Done');
103103
}
104104

105105
$this->write('Progress:');

src/Utils/AnsiCode.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
* Class AnsiCode terminal
1313
* @package Inhere\Console\Utils
1414
*
15-
* \r = \x0D -> 13 回到行首
15+
* \r, \x1B[2K 都是清除本行
16+
* \x0D -> 13 回到行首
1617
* ESC = \x1B -> 27
1718
*/
1819
final class AnsiCode

src/Utils/CommandLineParse.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ final class CommandLineParse
3030
* ```
3131
*
3232
* ```php
33-
* $result = OptArgParse::byArgv($_SERVER['argv']);
33+
* $result = CommandLineParse::byArgv($_SERVER['argv']);
3434
* ```
3535
*
3636
* Supports args:
@@ -55,8 +55,8 @@ public static function byArgv(array $params, array $noValues = [], $mergeOpts =
5555
{
5656
$args = $sOpts = $lOpts = [];
5757

58-
// each() will deprecated at 7.2 so,there use current and next instead it.
59-
// while (list(,$p) = each($params)) {
58+
// each() will deprecated at 7.2. so,there use current and next instead it.
59+
// while (list(,$p) = each($params)) {
6060
while (false !== ($p = current($params))) {
6161
next($params);
6262

@@ -86,7 +86,7 @@ public static function byArgv(array $params, array $noValues = [], $mergeOpts =
8686

8787
// fix: allow empty string ''
8888
if ($value === true && $nxp !== false && (!$nxp || $nxp{0} !== '-') && !in_array($opt, $noValues, true)) {
89-
// list(,$value) = each($params);
89+
// list(,$value) = each($params);
9090
$value = current($params);
9191
next($params);
9292

@@ -128,7 +128,7 @@ public static function byArgv(array $params, array $noValues = [], $mergeOpts =
128128
* parse custom array params
129129
*
130130
* ```php
131-
* $result = OptArgParse::byArray([
131+
* $result = CommandLineParse::byArray([
132132
* 'arg' => 'val',
133133
* '--lp' => 'val2',
134134
* '--s' => 'val3',
@@ -162,7 +162,7 @@ public static function byArray(array $params)
162162
/**
163163
*
164164
* ```php
165-
* $result = OptArgParse::byString('foo --bar="foobar"');
165+
* $result = CommandLineParse::byString('foo --bar="foobar"');
166166
* ```
167167
* @todo ...
168168
* @param string $string

src/Utils/Show.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -798,8 +798,9 @@ public static function progressTxt($total, $msg, $doneMsg = '')
798798
$msg = $doneMsg ?: $msg;
799799
}
800800

801-
// printf("\r%d%% %s", $percent, $msg);
802-
printf("\x0D\x1B[2K%d%% %s", $percent, $msg);
801+
// printf("\r%d%% %s", $percent, $msg);
802+
// printf("\x0D\x1B[2K%d%% %s", $percent, $msg);
803+
printf("\x0D\r%d%% %s", $percent, $msg);
803804

804805
if ($finished) {
805806
echo "\n";
@@ -856,8 +857,13 @@ public static function progressBar($total, array $opts = [])
856857
$finished = true;
857858
}
858859

860+
/**
861+
* \x0D 调到行首
862+
* \r, \x1B[2K 都是清除本行
863+
*/
859864
// printf("\r[%'--100s] %d%% %s",
860-
printf("\x0D\x1B[2K[%'{$waitChar}-100s] %d%% %s",
865+
// printf("\x0D\x1B[2K[%'{$waitChar}-100s] %d%% %s",
866+
printf("\x0D\r[%'{$waitChar}-100s] %d%% %s",
861867
str_repeat($opts['doneChar'], $percent) . ($finished ? '' : $opts['signChar']),
862868
$percent,
863869
$msg

0 commit comments

Comments
 (0)