Skip to content

Commit 842d7db

Browse files
committed
update:
add simple art font support. add annotation var support in others tag: options, arguments...
1 parent bdca268 commit 842d7db

File tree

18 files changed

+383
-27
lines changed

18 files changed

+383
-27
lines changed

examples/Commands/DemoCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ protected function configure()
2929
{
3030
$this->createDefinition()
3131
->setDescription(self::getDescription())
32-
->setExample($this->handleAnnotationVars('{script} {command} john male 43 --opt1 value1'))
32+
->setExample($this->parseAnnotationVars('{script} {command} john male 43 --opt1 value1'))
3333
->addArgument('name', Input::ARG_REQUIRED, 'description for the argument [name], is required')
3434
->addArgument('sex', Input::ARG_OPTIONAL, 'description for the argument [sex], is optional')
3535
->addArgument('age', Input::ARG_OPTIONAL, 'description for the argument [age], is optional')

examples/Controllers/HomeController.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Inhere\Console\Examples\Controllers;
44

55
use Inhere\Console\Components\AnsiCode;
6+
use Inhere\Console\Components\ArtFont;
67
use Inhere\Console\Components\Download;
78
use Inhere\Console\Controller;
89
use Inhere\Console\IO\Input;
@@ -31,9 +32,17 @@ protected static function commandAliases()
3132
'l' => 'list',
3233
'h' => 'helpPanel',
3334
'hp' => 'helpPanel',
35+
'af' => 'artFont',
3436
];
3537
}
3638

39+
protected function init()
40+
{
41+
parent::init();
42+
43+
$this->addAnnotationVar('internalFonts', implode(',', ArtFont::getInternalFonts()));
44+
}
45+
3746
/**
3847
* this is a command's description message
3948
* the second line text
@@ -89,6 +98,22 @@ public function blockMsgCommand()
8998
return 0;
9099
}
91100

101+
/**
102+
* output art font text
103+
* @options
104+
* --font Set the art font name(allow: {internalFonts}).
105+
* --style Set the art font style.
106+
* @return int
107+
*/
108+
public function artFontCommand()
109+
{
110+
ArtFont::create()->show('404', ArtFont::INTERNAL_GROUP,[
111+
'style' => $this->input->getOpt('style')
112+
]);
113+
114+
return 0;
115+
}
116+
92117
/**
93118
* a counter example show. It is like progress txt, but no max value.
94119
* @example

src/Base/AbstractCommand.php

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ abstract class AbstractCommand implements BaseCommandInterface
6363
/** @var string */
6464
private $processTitle;
6565

66+
/** @var array */
67+
private $annotationVars;
68+
6669
/**
6770
* Command constructor.
6871
* @param Input $input
@@ -73,6 +76,7 @@ public function __construct(Input $input, Output $output, InputDefinition $defin
7376
{
7477
$this->input = $input;
7578
$this->output = $output;
79+
$this->annotationVars = $this->annotationVars();
7680

7781
if ($definition) {
7882
$this->definition = $definition;
@@ -307,17 +311,54 @@ public function validateInput()
307311
**************************************************************************/
308312

309313
/**
310-
* 为命令注解提供可解析解析变量. 可以在命令的注释中使用
314+
* @param string $name
315+
* @param string $value
316+
*/
317+
protected function addAnnotationVar(string $name, $value)
318+
{
319+
if (!isset($this->annotationVars[$name])) {
320+
$this->annotationVars[$name] = (string)$value;
321+
}
322+
}
323+
324+
/**
325+
* @param array $map
326+
*/
327+
protected function addAnnotationVars(array $map)
328+
{
329+
foreach ($map as $name => $value) {
330+
$this->addAnnotationVar($name, $value);
331+
}
332+
}
333+
334+
/**
335+
* @param string $name
336+
* @param string $value
337+
*/
338+
protected function setAnnotationVar(string $name, $value)
339+
{
340+
$this->annotationVars[$name] = (string)$value;
341+
}
342+
343+
/**
344+
* 替换注解中的变量为对应的值
311345
* @param string $str
312346
* @return string
313347
*/
314-
protected function handleAnnotationVars($str)
348+
protected function parseAnnotationVars($str)
315349
{
316-
$map = [];
350+
static $map;
317351

318-
foreach ($this->annotationVars() as $key => $value) {
319-
$key = sprintf(self::ANNOTATION_VAR, $key);
320-
$map[$key] = $value;
352+
if ($map === null) {
353+
foreach ($this->annotationVars as $key => $value) {
354+
$key = sprintf(self::ANNOTATION_VAR, $key);
355+
$map[$key] = $value;
356+
}
357+
}
358+
359+
// not use vars
360+
if (false === strpos($str, '{')) {
361+
return $str;
321362
}
322363

323364
return $map ? strtr($str, $map) : $str;
@@ -349,7 +390,7 @@ protected function showHelpByMethodAnnotation($method, $action = null, array $al
349390
}
350391

351392
$doc = $ref->getMethod($method)->getDocComment();
352-
$tags = Annotation::getTags($this->handleAnnotationVars($doc));
393+
$tags = Annotation::getTags($this->parseAnnotationVars($doc));
353394
$comments = [];
354395

355396
if ($aliases) {
@@ -362,7 +403,7 @@ protected function showHelpByMethodAnnotation($method, $action = null, array $al
362403
}
363404

364405
if (isset(self::$annotationTags[$tag])) {
365-
$msg = trim($msg);
406+
$msg = $this->parseAnnotationVars(trim($msg));
366407

367408
// need multi align
368409
// if (self::$annotationTags[$tag]) {
@@ -453,6 +494,14 @@ public function setDefinition(InputDefinition $definition)
453494
$this->definition = $definition;
454495
}
455496

497+
/**
498+
* @return array
499+
*/
500+
public function getAnnotationVars(): array
501+
{
502+
return $this->annotationVars;
503+
}
504+
456505
/**
457506
* @return ApplicationInterface
458507
*/

src/BuiltIn/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# something
2+
3+
4+
## art font
5+
6+
- sublime plugin: `Figlet Big ASCII Text`
7+
- php implement: [povils/figlet](https://github.com/povils/figlet)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
__ __ ____ __ __
2+
/ // / / __ \/ // /
3+
/ // /_/ / / / // /_
4+
/__ __/ /_/ /__ __/
5+
/_/ \____/ /_/
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
__________ ____
2+
/ ____/ __ \/ __ \
3+
/___ \/ / / / / / /
4+
____/ / /_/ / /_/ /
5+
/_____/\____/\____/
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
______
2+
| ____|
3+
| |__ _ __ _ __ ___ _ __
4+
| __| | '__| '__/ _ \| '__|
5+
| |____| | | | | (_) | |
6+
|______|_| |_| \___/|_|
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
______
2+
/ ____/_____________ _____
3+
/ __/ / ___/ ___/ __ \/ ___/
4+
/ /___/ / / / / /_/ / /
5+
/_____/_/ /_/ \____/_/
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
_ _
2+
| \ | |
3+
| \| | ___
4+
| . ` |/ _ \
5+
| |\ | (_) |
6+
|_| \_|\___/
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
_ __
2+
/ | / /___
3+
/ |/ / __ \
4+
/ /| / /_/ /
5+
/_/ |_/\____/

0 commit comments

Comments
 (0)