Skip to content

Commit

Permalink
Merge pull request #116 from kodie/kodie/command-logo-support
Browse files Browse the repository at this point in the history
Add logo support for commands (rebased)
  • Loading branch information
adhocore authored Nov 26, 2024
2 parents 86e02bf + 67fd4d1 commit 2ca5a50
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ class InitCommand extends Ahc\Cli\Input\Command
'<bold> init</end> <comment>--apple applet --ball ballon <arggg></end> ## details 1<eol/>' .
// $0 will be interpolated to actual command name
'<bold> $0</end> <comment>-a applet -b ballon <arggg> [arg2]</end> ## details 2<eol/>'
);
)
->logo('Ascii art logo of your command');
}

// This method is auto called before `self::execute()` and receives `Interactor $io` instance
Expand Down
25 changes: 25 additions & 0 deletions src/Input/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class Command extends Parser implements Groupable

protected ?string $_alias = null;

protected string $_logo = '';

protected string $_help = '';

private array $_events = [];
Expand Down Expand Up @@ -154,6 +156,24 @@ public function bind(?App $app = null): self
return $this;
}

/**
* Sets or gets the ASCII art logo.
*
* @param string|null $logo
*
* @return string|self
*/
public function logo(?string $logo = null)
{
if (func_num_args() === 0) {
return $this->_logo;
}

$this->_logo = $logo;

return $this;
}

/**
* Registers argument definitions (all at once). Only last one can be variadic.
*/
Expand Down Expand Up @@ -332,6 +352,11 @@ public function showDefaultHelp(): mixed
{
$io = $this->io();
$helper = new OutputHelper($io->writer());
$app = $this->app();

if (($logo = $this->logo()) || ($app && ($logo = $app->logo()) && $app->getDefaultCommand() === $this->_name)) {
$io->logo($logo, true);
}

$io->help_header("Command {$this->_name}, version {$this->_version}", true)->eol();
$io->help_summary($this->_desc, true)->eol();
Expand Down
15 changes: 15 additions & 0 deletions tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,21 @@ public function test_logo()
$this->assertSame($logo, $a->logo());
}

public function test_logo_command()
{
$a = $this->newApp('test', '0.0.2');
$c = $a->command('cmd');

$this->assertSame($c, $c->logo($logo = '
| |_ ___ ___| |_
| __/ _ \/ __| __|
| || __/\__ \ |_
\__\___||___/\__|
'));

$this->assertSame($logo, $c->logo());
}

public function test_add()
{
$a = $this->newApp('test', '0.0.1-test');
Expand Down

0 comments on commit 2ca5a50

Please sign in to comment.