Skip to content

Commit e5f2ef7

Browse files
committed
update, some bug fixed. new add some color style, add progressTxt method
1 parent 42a46bf commit e5f2ef7

File tree

10 files changed

+211
-133
lines changed

10 files changed

+211
-133
lines changed

examples/HomeController.php

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use inhere\console\io\Input;
77
use inhere\console\utils\AnsiCode;
88
use inhere\console\utils\Download;
9+
use inhere\console\utils\Helper;
910
use inhere\console\utils\Show;
1011
use inhere\console\utils\Interact;
1112

@@ -60,10 +61,9 @@ public function colorCommand()
6061
return 0;
6162
}
6263

64+
$this->write('color text output:');
6365
$styles = $this->output->getStyle()->getStyleNames();
64-
$this->write('normal text output');
6566

66-
$this->write('color text output');
6767
foreach ($styles as $style) {
6868
$this->output->write("<$style>$style style text</$style>");
6969
}
@@ -90,24 +90,30 @@ public function blockMsgCommand()
9090
* a progress bar example show
9191
*
9292
* @options
93+
* --type the progress type, allow: bar,txt. default <cyan>txt</cyan>
9394
* --done-char the done show char. default <info>=</info>
9495
* --wait-char the waiting show char. default <info>-</info>
9596
* --sign-char the sign char show. default <info>></info>
9697
* @example
9798
* {script} home/progress
9899
* {script} home/progress --done-char '#' --wait-char ' '
100+
* @param Input $input
99101
* @return int
100102
*/
101103
public function progressCommand($input)
102104
{
103105
$i = 0;
104106
$total = 120;
105-
$bar = $this->output->progressBar($total, [
106-
'msg' => 'Msg Text',
107-
'doneChar' => $input->getOpt('done-char', '='),
108-
'waitChar' => $input->getOpt('wait-char', '-'),
109-
'signChar' => $input->getOpt('sign-char', '>'),
110-
]);
107+
if ($input->getOpt('type') === 'bar') {
108+
$bar = $this->output->progressBar($total, [
109+
'msg' => 'Msg Text',
110+
'doneChar' => $input->getOpt('done-char', '='),
111+
'waitChar' => $input->getOpt('wait-char', '-'),
112+
'signChar' => $input->getOpt('sign-char', '>'),
113+
]);
114+
} else {
115+
$bar = $this->output->progressTxt($total, 'Doing df df', 'Done');
116+
}
111117

112118
$this->write('Progress:');
113119

@@ -194,16 +200,17 @@ public function fmtMsgCommand()
194200
/**
195201
* a example for use arguments on command
196202
* @usage home/useArg [arg1=val1 arg2=arg2] [options]
197-
* @example home/useArg status=2 name=john arg0 -s=test --page=23 -d -rf --debug --test=false
198-
* home/useArg status=2 name=john name=tom name=jack arg0 -s=test --page=23 --id=23 --id=154 --id=456 -d -rf --debug --test=false
203+
* @example
204+
* home/useArg status=2 name=john arg0 -s=test --page=23 -d -rf --debug --test=false
205+
* home/useArg status=2 name=john name=tom name=jack arg0 -s=test --page=23 --id=23 --id=154 --id=456 -d -rf --debug --test=false
199206
*/
200207
public function useArgCommand()
201208
{
202209
$this->write('input arguments:');
203-
var_dump($this->input->getArgs());
210+
echo Helper::dumpVar($this->input->getArgs());
204211

205212
$this->write('input options:');
206-
var_dump($this->input->getOpts());
213+
echo Helper::dumpVar($this->input->getOpts());
207214

208215
// $this->write('the Input object:');
209216
// var_dump($this->input);
@@ -277,7 +284,7 @@ public function downCommand()
277284

278285
$d = Download::down($url, $saveAs, $type);
279286

280-
var_dump($d);
287+
echo Helper::dumpVar($d);
281288

282289
return 0;
283290
}

src/AbstractApp.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ abstract class AbstractApp
4141
'debug' => false,
4242
'name' => 'My Console',
4343
'version' => '0.5.1',
44+
'publishAt' => '2017.03.24',
4445
'charset' => 'UTF-8',
4546
'timeZone' => 'Asia/Shanghai',
4647
];

src/AbstractCommand.php

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,21 @@ abstract class AbstractCommand
2424
use InputOutputTrait;
2525
use UserInteractTrait;
2626

27-
// command description message
28-
// please use the const setting current controller/command description
29-
const DESCRIPTION = '';
30-
3127
// name -> {$name}
3228
const ANNOTATION_VAR = '{%s}'; // '{$%s}';
3329

3430
/**
35-
* TODO ...
36-
* command description message
37-
* please use the property setting current controller/command description
31+
* command name e.g 'test' 'test:one'
3832
* @var string
3933
*/
40-
protected static $description = '';
34+
protected static $name = '';
4135

4236
/**
43-
* command name e.g 'test' 'test:one'
37+
* command/controller description message
38+
* please use the property setting current controller/command description
4439
* @var string
4540
*/
46-
protected static $name = '';
41+
protected static $description = '';
4742

4843
/**
4944
* Allow display message tags in the command annotation
@@ -180,7 +175,8 @@ protected function annotationVars()
180175

181176
/**
182177
* 为命令注解提供可解析解析变量. 可以在命令的注释中使用
183-
* @return array
178+
* @param string $str
179+
* @return string
184180
*/
185181
protected function replaceAnnotationVars($str)
186182
{
@@ -191,11 +187,7 @@ protected function replaceAnnotationVars($str)
191187
$map[$key] = $value;
192188
}
193189

194-
if ($map) {
195-
return strtr($str, $map);
196-
}
197-
198-
return $str;
190+
return $map ? strtr($str, $map) : $str;
199191
}
200192

201193
/**
@@ -264,41 +256,42 @@ public static function setName(string $name)
264256
/**
265257
* @return string
266258
*/
267-
public static function getName(): string
259+
final public static function getName(): string
268260
{
269261
return static::$name;
270262
}
271263

272264
/**
273-
* @return array
265+
* @return string
274266
*/
275-
public static function getAllowTags(): array
267+
final public static function getDescription(): string
276268
{
277-
return self::$allowTags;
269+
return static::$description;
278270
}
279271

280272
/**
281-
* @param array $allowTags
273+
* @param string $description
282274
*/
283-
public static function setAllowTags(array $allowTags)
275+
public static function setDescription(string $description)
284276
{
285-
self::$allowTags = $allowTags;
277+
static::$description = $description;
286278
}
287279

288280
/**
289-
* @return string
281+
* @return array
290282
*/
291-
final public static function getDescription(): string
283+
public static function getAllowTags(): array
292284
{
293-
return static::$description;
285+
return self::$allowTags;
294286
}
295287

296288
/**
297-
* @param string $description
289+
* @param array $allowTags
290+
* @param bool $replace
298291
*/
299-
public static function setDescription(string $description)
292+
public static function setAllowTags(array $allowTags, $replace = false)
300293
{
301-
static::$description = $description;
294+
self::$allowTags = $replace ? $allowTags : array_merge(self::$allowTags, $allowTags);
302295
}
303296

304297
/**

src/App.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,8 @@ public function dispatch()
7474
return $this->runAction($name, $action, true);
7575
}
7676

77-
if (false !== self::fire(self::ON_NOT_FOUND, [$this])) {
78-
// not match, output error message
79-
$this->output->error("Console Controller or Command [$command] not exists!");
77+
if (true !== self::fire(self::ON_NOT_FOUND, [$this])) {
78+
$this->output->error("Console controller or command [$command] not exists!");
8079
$this->showCommandList(false);
8180
}
8281

@@ -214,17 +213,19 @@ public function showHelpInfo($quit = true)
214213
*/
215214
public function showVersionInfo($quit = true)
216215
{
216+
$date = date('Y-m-d');
217217
$version = $this->config('version', 'Unknown');
218+
$publishAt = $this->config['publishAt'];
218219
$phpVersion = PHP_VERSION;
219220
$os = PHP_OS;
220221

221-
$message = <<<EOF
222-
Console App Version <comment>$version</comment>
222+
$this->output->aList([
223+
"Console Application <info>{$this->config['name']}</info> Version <comment>$version</comment>(publish at $publishAt)",
224+
'System' => "PHP version <info>$phpVersion</info>, on OS <info>$os</info>, current Date $date",
225+
], null, [
226+
'leftChar' => ''
227+
]);
223228

224-
<comment>System:</comment>
225-
PHP <info>$phpVersion</info>, on OS <info>$os</info>
226-
EOF;
227-
$this->output->write($message);
228229
$quit && $this->stop();
229230
}
230231

@@ -270,7 +271,8 @@ public function showCommandList($quit = true)
270271

271272
$this->output->write('There are all console controllers and independent commands.');
272273
$this->output->mList([
273-
'Group Commands:(by controller)' => $controllerArr ?: '... No register any group command',
274+
//'There are all console controllers and independent commands.',
275+
'Group Commands:(by controller)' => $controllerArr ?: '... No register any group command(controller)',
274276
'Independent Commands:' => $commandArr ?: '... No register any independent command',
275277
'Internal Commands:' => $internalCommands
276278
]);

src/style/LiteStyle.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ class LiteStyle
3030

3131
// extra Foreground color
3232

33-
const DARK_GRAY = 90;
34-
const LIGHT_RED = 91;
35-
const LIGHT_GREEN = 92;
36-
const LIGHT_YELLOW = 93;
37-
const LIGHT_BLUE = 94;
38-
const LIGHT_MAGENTA = 95;
39-
const LIGHT_CYAN = 96;
40-
const WHITE = 97;
33+
const FG_DARK_GRAY = 90;
34+
const FG_LIGHT_RED = 91;
35+
const FG_LIGHT_GREEN = 92;
36+
const FG_LIGHT_YELLOW = 93;
37+
const FG_LIGHT_BLUE = 94;
38+
const FG_LIGHT_MAGENTA = 95;
39+
const FG_LIGHT_CYAN = 96;
40+
const FG_WHITE_W = 97;
4141

4242
// Background color
4343
const BG_BLACK = 40;
@@ -57,7 +57,7 @@ class LiteStyle
5757
const BG_LIGHT_BLUE = 104;
5858
const BG_LIGHT_MAGENTA = 105;
5959
const BG_LIGHT_CYAN = 106;
60-
const BG_WHITE = 107;
60+
const BG_WHITE_W = 107;
6161

6262
// color option
6363
const BOLD = 1; // 加粗
@@ -97,7 +97,7 @@ class LiteStyle
9797
* @param string|int|array $style
9898
* @return string
9999
*/
100-
public static function add($text, $style = self::NORMAL)
100+
public static function color($text, $style = self::NORMAL)
101101
{
102102
return self::render($text, $style);
103103
}

src/style/Style.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
class Style
2121
{
2222
/**
23-
* there are some default styles
23+
* there are some default style tags
2424
*/
2525
const NORMAL = 'normal';
2626
const FAINTLY = 'faintly';
@@ -29,6 +29,7 @@ class Style
2929
const PRIMARY = 'primary';
3030
const SUCCESS = 'success';
3131
const INFO = 'info';
32+
const NOTE = 'note';
3233
const WARNING = 'warning';
3334
const COMMENT = 'comment';
3435
const QUESTION = 'question';
@@ -101,23 +102,27 @@ public function __construct($fg = '', $bg = '', array $options = [])
101102
*/
102103
protected function loadDefaultStyles()
103104
{
104-
$this->add(self::NORMAL, ['fg' => 'normal', 'options' => ['underscore'] ])
105+
$this
106+
->add(self::NORMAL, ['fg' => 'normal'])
105107
// 不明显的 浅灰色的
106108
->add(self::FAINTLY, ['fg' => 'normal', 'options' => ['italic'] ])
107109
->add(self::BOLD, ['options' => ['bold'] ])
108110
->add(self::INFO, [ 'fg' => 'green', ]) //'options' => ['bold']
111+
->add(self::NOTE, [ 'fg' => 'green', 'options' => ['bold'] ]) //'options' => ['bold']
109112
->add(self::PRIMARY, [ 'fg' => 'blue', ]) //'options' => ['bold']
110113
->add(self::SUCCESS, ['fg' => 'green', 'options' => ['bold'] ])
111114
->add(self::NOTICE, ['options' => ['bold', 'underscore'], ])
112115
->add(self::WARNING, ['fg' => 'black', 'bg' => 'yellow', ]) //'options' => ['bold']
113116
->add(self::COMMENT, [ 'fg' => 'yellow', ]) //'options' => ['bold']
114117
->add(self::QUESTION, ['fg' => 'black', 'bg' => 'cyan'])
115118
->add(self::DANGER, ['fg' => 'red', ]) // 'bg' => 'magenta', 'options' => ['bold']
116-
->add(self::ERROR, ['fg' => 'white', 'bg' => 'red']);
119+
->add(self::ERROR, ['fg' => 'black', 'bg' => 'red'])
120+
->add('underline', ['fg' => 'normal', 'options' => ['underscore'] ])
121+
->add('cyan', ['fg' => 'cyan'])
122+
->add('magenta', ['fg' => 'magenta'])
123+
->add('yellow', ['fg' => 'yellow']);
117124
}
118125

119-
//////////////////////////////////////////// Text Color handle ////////////////////////////////////////////
120-
121126
/**
122127
* Process a string.
123128
* @param $text
@@ -250,6 +255,14 @@ public function getStyleNames(): array
250255
return array_keys($this->styles);
251256
}
252257

258+
/**
259+
* @return array
260+
*/
261+
public function getNames(): array
262+
{
263+
return array_keys($this->styles);
264+
}
265+
253266
/**
254267
* @return array
255268
*/

src/traits/FormatOutputTrait.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function section($title, $body, array $opts = [])
6060
* @inheritdoc
6161
* @see Show::aList()
6262
*/
63-
public function aList($data, $title, array $opts = [])
63+
public function aList($data, $title = null, array $opts = [])
6464
{
6565
Show::aList($data, $title, $opts);
6666
}
@@ -111,6 +111,15 @@ public function table(array $data, $title = 'Info List', $showBorder = true)
111111
Show::table($data, $title, $showBorder);
112112
}
113113

114+
/**
115+
* @inheritdoc
116+
* @see Show::progressBar()
117+
*/
118+
public function progressTxt($total, $msg, $doneMsg = '')
119+
{
120+
return Show::progressTxt($total, $msg, $doneMsg);
121+
}
122+
114123
/**
115124
* @inheritdoc
116125
* @see Show::progressBar()

0 commit comments

Comments
 (0)