Skip to content

Commit

Permalink
Add logo support for commands
Browse files Browse the repository at this point in the history
  • Loading branch information
kodie committed Nov 26, 2024
1 parent 86e02bf commit 6b7459f
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 6b7459f

Please sign in to comment.