Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat) Add P class with static methods #9

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions playground/confirm.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

use function Laravel\Prompts\confirm;
use Laravel\Prompts\P;

require __DIR__.'/../vendor/autoload.php';

$confirmed = confirm(
$confirmed = P::confirm(
label: 'Would you like to install dependencies?',
validate: fn ($value) => match ($value) {
false => 'You must install dependencies.',
Expand Down
40 changes: 14 additions & 26 deletions playground/index.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
<?php

use function Laravel\Prompts\alert;
use function Laravel\Prompts\confirm;
use function Laravel\Prompts\error;
use function Laravel\Prompts\intro;
use function Laravel\Prompts\multiselect;
use function Laravel\Prompts\note;
use function Laravel\Prompts\outro;
use function Laravel\Prompts\password;
use function Laravel\Prompts\select;
use function Laravel\Prompts\spin;
use function Laravel\Prompts\suggest;
use function Laravel\Prompts\text;
use function Laravel\Prompts\warning;
use Laravel\Prompts\P;

require __DIR__.'/../vendor/autoload.php';

intro('Welcome to Laravel');
P::intro('Welcome to Laravel');

$name = suggest(
$name = P::suggest(
label: 'What is your name?',
placeholder: 'E.g. Taylor Otwell',
options: [
Expand All @@ -38,7 +26,7 @@
},
);

$path = text(
$path = P::text(
label: 'Where should we create your project?',
placeholder: 'E.g. ./laravel',
validate: fn ($value) => match (true) {
Expand All @@ -48,7 +36,7 @@
},
);

$password = password(
$password = P::password(
label: 'Provide a password',
validate: fn ($value) => match (true) {
! $value => 'Please enter a password.',
Expand All @@ -57,7 +45,7 @@
},
);

$type = select(
$type = P::select(
label: 'Pick a project type',
default: 'ts',
options: [
Expand All @@ -66,7 +54,7 @@
],
);

$tools = multiselect(
$tools = P::multiselect(
label: 'Select additional tools.',
default: ['pint', 'eslint'],
options: [
Expand All @@ -81,19 +69,19 @@
}
);

$install = confirm(
$install = P::confirm(
label: 'Install dependencies?',
);

if ($install) {
spin(fn () => sleep(3), 'Installing dependencies...');
P::spin(fn () => sleep(3), 'Installing dependencies...');
}

error('Error');
warning('Warning');
alert('Alert');
P::error('Error');
P::warning('Warning');
P::alert('Alert');

note(<<<EOT
P::note(<<<EOT
Installation complete!

To get started, run:
Expand All @@ -102,6 +90,6 @@
php artisan serve
EOT);

outro('Happy coding!');
P::outro('Happy coding!');

var_dump(compact('name', 'path', 'password', 'type', 'tools', 'install'));
4 changes: 2 additions & 2 deletions playground/multiselect.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

use function Laravel\Prompts\multiselect;
use Laravel\Prompts\P;

require __DIR__.'/../vendor/autoload.php';

$permissions = multiselect(
$permissions = P::multiselect(
label: 'What permissions should the user have?',
options: [
'view' => 'View',
Expand Down
19 changes: 7 additions & 12 deletions playground/notes.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
<?php

use function Laravel\Prompts\alert;
use function Laravel\Prompts\error;
use function Laravel\Prompts\intro;
use function Laravel\Prompts\note;
use function Laravel\Prompts\outro;
use function Laravel\Prompts\warning;
use Laravel\Prompts\P;

require __DIR__.'/../vendor/autoload.php';

intro('Intro');
note('Note');
warning('Warning');
error('Error');
alert('Alert');
outro('Outro');
P::intro('Intro');
P::note('Note');
P::warning('Warning');
P::error('Error');
P::alert('Alert');
P::outro('Outro');
4 changes: 2 additions & 2 deletions playground/password.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

use function Laravel\Prompts\password;
use Laravel\Prompts\P;

require __DIR__.'/../vendor/autoload.php';

$password = password(
$password = P::password(
label: 'Please provide a password',
placeholder: 'Min 8 characters',
required: true,
Expand Down
4 changes: 2 additions & 2 deletions playground/search.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

use function Laravel\Prompts\search;
use Laravel\Prompts\P;

require __DIR__.'/../vendor/autoload.php';

$model = search(
$model = P::search(
label: 'Which user should receive the email?',
placeholder: 'Search...',
options: function ($value) {
Expand Down
4 changes: 2 additions & 2 deletions playground/select.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

use function Laravel\Prompts\select;
use Laravel\Prompts\P;

require __DIR__.'/../vendor/autoload.php';

$role = select(
$role = P::select(
label: 'What role should the user have?',
options: [
'read-only' => 'Read only',
Expand Down
4 changes: 2 additions & 2 deletions playground/spin.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

use function Laravel\Prompts\spin;
use Laravel\Prompts\P;

require __DIR__.'/../vendor/autoload.php';

$result = spin(
$result = P::spin(
function () {
sleep(4);

Expand Down
4 changes: 2 additions & 2 deletions playground/suggest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

use function Laravel\Prompts\suggest;
use Laravel\Prompts\P;

require __DIR__.'/../vendor/autoload.php';

$model = suggest(
$model = P::suggest(
label: 'What model should the policy apply to?',
placeholder: 'E.g. User',
options: [
Expand Down
4 changes: 2 additions & 2 deletions playground/text.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

use function Laravel\Prompts\text;
use Laravel\Prompts\P;

require __DIR__.'/../vendor/autoload.php';

$email = text(
$email = P::text(
label: 'What is your email address',
placeholder: 'E.g. [email protected]',
validate: fn ($value) => match (true) {
Expand Down
171 changes: 171 additions & 0 deletions src/P.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
<?php

namespace Laravel\Prompts;

use Closure;
use Illuminate\Support\Collection;

class P
{
public static function text(
string $label,
string $placeholder = '',
string $default = '',
bool|string $required = false,
Closure $validate = null
): string {
return (new TextPrompt($label, $placeholder, $default, $required, $validate))->prompt();
}

/**
* Prompt the user for input, hiding the value.
*/
public static function password(
string $label,
string $placeholder = '',
bool|string $required = false,
Closure $validate = null
): string {
return (new PasswordPrompt($label, $placeholder, $required, $validate))->prompt();
}

/**
* Prompt the user to select an option.
*
* @param array<int|string, string>|Collection<int|string, string> $options
*/
public static function select(
string $label,
array|Collection $options,
int|string $default = null,
int $scroll = 5,
Closure $validate = null
): int|string {
return (new SelectPrompt($label, $options, $default, $scroll, $validate))->prompt();
}

/**
* Prompt the user to select multiple options.
*
* @param array<int|string, string>|Collection<int|string, string> $options
* @param array<int|string>|Collection<int, int|string> $default
* @return array<int|string>
*/
public static function multiselect(
string $label,
array|Collection $options,
array|Collection $default = [],
int $scroll = 5,
bool|string $required = false,
Closure $validate = null
): array {
return (new MultiSelectPrompt($label, $options, $default, $scroll, $required, $validate))->prompt();
}

/**
* Prompt the user to confirm an action.
*/
public static function confirm(
string $label,
bool $default = true,
string $yes = 'Yes',
string $no = 'No',
bool|string $required = false,
Closure $validate = null
): bool {
return (new ConfirmPrompt($label, $default, $yes, $no, $required, $validate))->prompt();
}

/**
* Prompt the user for text input with auto-completion.
*
* @param array<string>|Collection<int, string>|Closure(string): array<string> $options
*/
public static function suggest(
string $label,
array|Collection|Closure $options,
string $placeholder = '',
string $default = '',
int $scroll = 5,
bool|string $required = false,
Closure $validate = null
): string {
return (new SuggestPrompt($label, $options, $placeholder, $default, $scroll, $required, $validate))->prompt();
}

/**
* Allow the user to search for an option.
*
* @param Closure(string): array<int|string, string> $options
*/
public static function search(
string $label,
Closure $options,
string $placeholder = '',
int $scroll = 5,
Closure $validate = null
): int|string {
return (new SearchPrompt($label, $options, $placeholder, $scroll, $validate))->prompt();
}

/**
* Render a spinner while the given callback is executing.
*
* @template TReturn of mixed
*
* @param \Closure(): TReturn $callback
* @return TReturn
*/
public static function spin(Closure $callback, string $message = ''): mixed
{
return (new Spinner($message))->spin($callback);
}

/**
* Display a note.
*/
public static function note(string $message, string $type = null): void
{
(new Note($message, $type))->display();
}

/**
* Display an error.
*/
public static function error(string $message): void
{
(new Note($message, 'error'))->display();
}

/**
* Display a warning.
*/
public static function warning(string $message): void
{
(new Note($message, 'warning'))->display();
}

/**
* Display an alert.
*/
public static function alert(string $message): void
{
(new Note($message, 'alert'))->display();
}

/**
* Display an introduction.
*/
public static function intro(string $message): void
{
(new Note($message, 'intro'))->display();
}

/**
* Display a closing message.
*/
public static function outro(string $message): void
{
(new Note($message, 'outro'))->display();
}
}
Loading