Skip to content

Commit 3c3d925

Browse files
committed
update, some modify ...
1 parent a1ea50c commit 3c3d925

File tree

7 files changed

+82
-43
lines changed

7 files changed

+82
-43
lines changed

examples/Controllers/HomeController.php

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ protected static function commandAliases()
4040
'af' => 'artFont',
4141
'ml' => 'multiList',
4242
'ms' => 'multiSelect',
43+
'sl' => 'splitLine',
4344
];
4445
}
4546

@@ -75,10 +76,31 @@ public function testCommand()
7576
$this->write('hello, welcome!! this is ' . __METHOD__);
7677
}
7778

79+
/**
80+
* command `defArgCommand` config
81+
* @throws \LogicException
82+
*/
83+
protected function defArgConfigure()
84+
{
85+
$this->createDefinition()
86+
->setDescription('the command arg/opt config use defined configure, it like symfony console: argument define by position')
87+
->addArgument('name', Input::ARG_REQUIRED, "description for the argument 'name'")
88+
->addOption('yes', 'y', Input::OPT_BOOLEAN, "description for the option 'yes'")
89+
->addOption('opt1', null, Input::OPT_REQUIRED, "description for the option 'opt1'");
90+
}
91+
92+
/**
93+
* the command arg/opt config use defined configure, it like symfony console: argument define by position
94+
*/
95+
public function defArgCommand()
96+
{
97+
$this->output->dump($this->input->getArgs(), $this->input->getOpts(), $this->input->getBoolOpt('y'));
98+
}
99+
78100
/**
79101
* a example for highlight code
80102
* @options
81-
* --ln With line number
103+
* --ln Display with line number
82104
* @param Input $in
83105
*/
84106
public function highlightCommand($in)
@@ -210,6 +232,21 @@ public function pendingCommand()
210232
Show::pending('Done', true);
211233
}
212234

235+
/**
236+
* dynamic notice message show: pointing
237+
*/
238+
public function pointingCommand()
239+
{
240+
$total = 100;
241+
242+
while ($total--) {
243+
Show::pointing();
244+
usleep(10000);
245+
}
246+
247+
Show::pointing('Done', true);
248+
}
249+
213250
/**
214251
* a progress bar example show, by Show::progressBar()
215252
* @options
@@ -280,6 +317,20 @@ public function titleCommand()
280317
return 0;
281318
}
282319

320+
/**
321+
* output format message: splitLine
322+
* @options
323+
* -w, --width WIDTH The split line width. default is current screen width.
324+
*/
325+
public function splitLineCommand()
326+
{
327+
$this->output->splitLine('split Line', '-', $this->input->getSameOpt(['w', 'width'], 0));
328+
329+
$this->output->splitLine('split 中文 Line', '-', $this->input->getSameOpt(['w', 'width'], 0));
330+
331+
return 0;
332+
}
333+
283334
/**
284335
* output format message: section
285336
*/
@@ -540,27 +591,6 @@ public function useArgCommand()
540591
// var_dump($this->input);
541592
}
542593

543-
/**
544-
* command `defArgCommand` config
545-
* @throws \LogicException
546-
*/
547-
protected function defArgConfigure()
548-
{
549-
$this->createDefinition()
550-
->setDescription('the command arg/opt config use defined configure, it like symfony console: argument define by position')
551-
->addArgument('name', Input::ARG_REQUIRED, 'description for the argument [name]')
552-
->addOption('yes', 'y', Input::OPT_BOOLEAN, 'description for the option [yes]')
553-
->addOption('opt1', null, Input::OPT_REQUIRED, 'description for the option [opt1]');
554-
}
555-
556-
/**
557-
* the command arg/opt config use defined configure, it like symfony console: argument define by position
558-
*/
559-
public function defArgCommand()
560-
{
561-
$this->output->dump($this->input->getArgs(), $this->input->getOpts(), $this->input->getBoolOpt('y'));
562-
}
563-
564594
/**
565595
* This is a demo for use <magenta>Interact::confirm</magenta> method
566596
*/

src/Base/AbstractCommand.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -190,20 +190,23 @@ protected function afterExecute()
190190
*/
191191
protected function showHelp()
192192
{
193-
// 创建了 InputDefinition , 则使用它的信息。此时不会再解析和使用命令的注释。
194-
if ($def = $this->getDefinition()) {
195-
$cmd = $this->input->getCommand();
196-
$spt = $this->input->getScript();
193+
if (!$def = $this->getDefinition()) {
194+
return false;
195+
}
197196

198-
$info = $def->getSynopsis();
199-
$info['usage'] = "$spt $cmd " . $info['usage'];
197+
// 创建了 InputDefinition , 则使用它的信息。
198+
// 此时不会再解析和使用命令的注释。
199+
$info = $def->getSynopsis();
200+
$info['usage'] = sprintf('%s %s %s',
201+
$this->input->getScript(),
202+
$this->input->getCommand(),
203+
$info['usage']
204+
);
200205

201-
$this->output->mList($info);
206+
$this->output->mList($info, ['sepChar' => ' ']);
202207

203-
return true;
204-
}
208+
return true;
205209

206-
return false;
207210
}
208211

209212
/**

src/IO/InputDefinition.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,8 @@ public function addOptions(array $options = [])
249249
*/
250250
public function addOption($name, $shortcut = null, $mode = null, $description = '', $default = null)
251251
{
252-
if (0 === strpos($name, '--')) {
253-
$name = substr($name, 2);
252+
if (0 === strpos($name, '-')) {
253+
$name = trim($name, '-');
254254
}
255255

256256
if (empty($name)) {
@@ -267,7 +267,9 @@ public function addOption($name, $shortcut = null, $mode = null, $description =
267267
throw new \InvalidArgumentException(sprintf('Option mode "%s" is not valid.', $mode));
268268
}
269269

270-
if (($isArray = $mode === Input::OPT_IS_ARRAY) && !$this->optionIsAcceptValue($mode)) {
270+
$isArray = $mode === Input::OPT_IS_ARRAY;
271+
272+
if ($isArray && !$this->optionIsAcceptValue($mode)) {
271273
throw new \InvalidArgumentException('Impossible to have an option mode VALUE_IS_ARRAY if the option does not accept a value.');
272274
}
273275

@@ -277,7 +279,7 @@ public function addOption($name, $shortcut = null, $mode = null, $description =
277279

278280
// set default value
279281
if (Input::OPT_BOOLEAN === (Input::OPT_BOOLEAN & $mode) && null !== $default) {
280-
throw new \LogicException('Cannot set a default value when using Input::OPT_BOOLEAN mode.');
282+
throw new \LogicException('Cannot set a default value when using OPT_BOOLEAN mode.');
281283
}
282284

283285
if ($isArray) {
@@ -392,7 +394,7 @@ private function shortcutToName($shortcut)
392394
*/
393395
private function mergeArgOptConfig(array $map)
394396
{
395-
return array_merge(self::$defaultArgOptConfig, $map);
397+
return $map ? array_merge(self::$defaultArgOptConfig, $map) : self::$defaultArgOptConfig;
396398
}
397399

398400
/**

src/Style/Highlighter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function highlight(string $source, $withLn = false)
7373
* @return string
7474
* @throws \InvalidArgumentException
7575
*/
76-
public function getCodeSnippet($source, $lineNumber, $linesBefore = 2, $linesAfter = 2)
76+
public function highlightSnippet($source, $lineNumber, $linesBefore = 2, $linesAfter = 2)
7777
{
7878
$tokenLines = $this->getHighlightedLines($source);
7979

src/Traits/FormatOutputAwareTrait.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@
3838
*
3939
* @method padding(array $data, string $title = null, array $opts = [])
4040
*
41+
* @method splitLine(string $title, string $char = '-', int $width = 0)
4142
* @method spinner($msg = '', $ended = false)
4243
* @method loading($msg = 'Loading ', $ended = false)
4344
* @method pending($msg = 'Pending ', $ended = false)
45+
* @method pointing($msg = 'handling ', $ended = false)
4446
*
4547
* @method \Generator counterTxt($msg = 'Pending ', $ended = false)
4648
*/

src/Utils/Helper.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ public static function stripAnsiCode($string)
200200
*/
201201
public static function strUtf8Len($string)
202202
{
203+
// strlen: one chinese is 3 char.
204+
// mb_strlen: one chinese is 1 char.
205+
// mb_strwidth: one chinese is 2 char.
203206
return mb_strlen($string, 'utf-8');
204207
}
205208

src/Utils/Show.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,10 @@ public static function splitLine(string $title, string $char = '-', int $width =
195195
list($width,) = CliUtil::getScreenSize();
196196
}
197197

198-
$length = $width - Helper::strLen($title) + 2;
198+
$strLen = ceil(($width - Helper::strLen($title) + 2) / 2);
199+
$padStr = $strLen > 0 ? str_repeat($char, $strLen) : '';
199200

200-
self::write(str_pad('' . ucwords($title) . ' ', $length, $char, STR_PAD_BOTH));
201+
self::write($padStr . ' ' . ucwords($title) . ' ' . $padStr);
201202
}
202203

203204
/**
@@ -988,7 +989,7 @@ public static function pointing($msg = 'handling ', $ended = false)
988989
static $counter = 0;
989990

990991
if ($ended) {
991-
return printf(' %s %d', $msg ?: 'Total', $counter);
992+
return self::write(sprintf(' (%s %d)', $msg ?: 'Total', $counter));
992993
}
993994

994995
if ($counter === 0 && $msg) {
@@ -1006,13 +1007,11 @@ public static function pointing($msg = 'handling ', $ended = false)
10061007
* $total = 120;
10071008
* $ctt = Show::counterTxt('handling ...', 'handled.');
10081009
* $this->write('Counter:');
1009-
*
10101010
* while ($total - 1) {
10111011
* $ctt->send(1);
10121012
* usleep(30000);
10131013
* $total--;
10141014
* }
1015-
*
10161015
* // end of the counter.
10171016
* $ctt->send(-1);
10181017
* ```

0 commit comments

Comments
 (0)