Skip to content

Commit

Permalink
Merge pull request #1 from artemeon/feat/beautify-cli-prompts
Browse files Browse the repository at this point in the history
✨ Beautify CLI prompts
  • Loading branch information
marcreichel authored Jul 28, 2023
2 parents a9ee0c5 + 7afa9e7 commit 029128d
Show file tree
Hide file tree
Showing 10 changed files with 303 additions and 27 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
}
],
"require": {
"php": ">=8.0",
"php": "^8.1",
"laravel/prompts": "^0.1.0",
"nunomaduro/termwind": "^1.5",
"symfony/console": "^5.0|^6.0"
}
Expand Down
53 changes: 53 additions & 0 deletions example/example
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env php
<?php

declare(strict_types=1);

use Artemeon\Console\Commands\ConfirmExampleCommand;
use Artemeon\Console\Commands\MultiSelectExampleCommand;
use Artemeon\Console\Commands\PasswordExampleCommand;
use Artemeon\Console\Commands\SelectExampleCommand;
use Artemeon\Console\Commands\TextExampleCommand;
use Symfony\Component\Console\Application;

(new class() {
public function main(): void
{
$this->autoload();

$app = new Application();
$app->add(new ConfirmExampleCommand());
$app->add(new MultiSelectExampleCommand());
$app->add(new PasswordExampleCommand());
$app->add(new SelectExampleCommand());
$app->add(new TextExampleCommand());
$app->run();
}

private function autoload(): void
{
if (isset($GLOBALS['_composer_autoload_path'])) {
define('COMPOSER_INSTALL_PATH', $GLOBALS['_composer_autoload_path']);

unset($GLOBALS['_composer_autoload_path']);
} else {
foreach (
[
__DIR__ . '/../../../autoload.php',
__DIR__ . '/../../vendor/autoload.php',
__DIR__ . '/../vendor/autoload.php'
] as $file
) {
if (file_exists($file)) {
define('COMPOSER_INSTALL_PATH', $file);

break;
}
}

unset($file);
}

require COMPOSER_INSTALL_PATH;
}
})->main();
2 changes: 1 addition & 1 deletion src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Artemeon\Console;

use Artemeon\Console\Style\ArtemeonStyle;
use Artemeon\Console\Styles\ArtemeonStyle;
use Symfony\Component\Console\Command\Command as SymfonyCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down
19 changes: 19 additions & 0 deletions src/Commands/ConfirmExampleCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Artemeon\Console\Commands;

use Artemeon\Console\Command;

class ConfirmExampleCommand extends Command
{
protected string $signature = 'confirm';

public function __invoke(): int
{
$this->confirm(label: 'Please confirm, sending me all your money', yes: 'Absolutely', no: 'Absolutely not');

return self::SUCCESS;
}
}
27 changes: 27 additions & 0 deletions src/Commands/MultiSelectExampleCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Artemeon\Console\Commands;

use Artemeon\Console\Command;

class MultiSelectExampleCommand extends Command
{
protected string $signature = 'multiselect';

public function __invoke(): int
{
$this->multiselect(label: 'Please choose your favorite car brands', options: [
'audi' => 'Audi',
'bmw' => 'BMW',
'ferrari' => 'Ferrari',
'koenigsegg' => 'Koenigsegg',
'seat' => 'Seat',
'tesla' => 'Tesla',
'vw' => 'Volkswagen',
]);

return self::SUCCESS;
}
}
19 changes: 19 additions & 0 deletions src/Commands/PasswordExampleCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Artemeon\Console\Commands;

use Artemeon\Console\Command;

class PasswordExampleCommand extends Command
{
protected string $signature = 'password';

public function __invoke(): int
{
$this->password('Choose your password', required: true);

return self::SUCCESS;
}
}
29 changes: 29 additions & 0 deletions src/Commands/SelectExampleCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Artemeon\Console\Commands;

use Artemeon\Console\Command;

use function Laravel\Prompts\spin;

class SelectExampleCommand extends Command
{
protected string $signature = 'select';

public function __invoke(): int
{
$this->select(label: 'Please choose a car brand', options: [
'audi' => 'Audi',
'bmw' => 'BMW',
'ferrari' => 'Ferrari',
'koenigsegg' => 'Koenigsegg',
'seat' => 'Seat',
'tesla' => 'Tesla',
'vw' => 'Volkswagen',
]);

return self::SUCCESS;
}
}
21 changes: 21 additions & 0 deletions src/Commands/TextExampleCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Artemeon\Console\Commands;

use Artemeon\Console\Command;

class TextExampleCommand extends Command
{
protected string $signature = 'text';

public function __invoke(): int
{
$username = $_SERVER['USER'] ?? '';

$this->ask('What is your name?', 'E.g. your username', $username, true);

return self::SUCCESS;
}
}
Loading

0 comments on commit 029128d

Please sign in to comment.