Skip to content

Commit

Permalink
Work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeraymonddowning committed Apr 12, 2024
1 parent 4e6582e commit 4925d1f
Showing 1 changed file with 21 additions and 27 deletions.
48 changes: 21 additions & 27 deletions src/FormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Laravel\Prompts;

use BadFunctionCallException;
use Closure;
use Illuminate\Support\Collection;
use Laravel\Prompts\Exceptions\FormRevertedException;
Expand Down Expand Up @@ -81,23 +80,23 @@ public function submit(): array
*/
public function text(string $label, string $placeholder = '', string $default = '', bool|string $required = false, mixed $validate = null, string $hint = '', ?string $name = null): self
{
return $this->runPrompt('\\Laravel\\Prompts\\text', get_defined_vars());
return $this->runPrompt(text(...), get_defined_vars());
}

/**
* Prompt the user for multiline text input.
*/
public function textarea(string $label, string $placeholder = '', string $default = '', bool|string $required = false, ?Closure $validate = null, string $hint = '', int $rows = 5, ?string $name = null): self
{
return $this->runPrompt('\\Laravel\\Prompts\\textarea', get_defined_vars());
return $this->runPrompt(textarea(...), get_defined_vars());
}

/**
* Prompt the user for input, hiding the value.
*/
public function password(string $label, string $placeholder = '', bool|string $required = false, mixed $validate = null, string $hint = '', ?string $name = null): self
{
return $this->runPrompt('\\Laravel\\Prompts\\password', get_defined_vars());
return $this->runPrompt(password(...), get_defined_vars());
}

/**
Expand All @@ -108,7 +107,7 @@ public function password(string $label, string $placeholder = '', bool|string $r
*/
public function select(string $label, array|Collection $options, int|string|null $default = null, int $scroll = 5, mixed $validate = null, string $hint = '', bool|string $required = true, ?string $name = null): self
{
return $this->runPrompt('\\Laravel\\Prompts\\select', get_defined_vars());
return $this->runPrompt(select(...), get_defined_vars());
}

/**
Expand All @@ -119,23 +118,23 @@ public function select(string $label, array|Collection $options, int|string|null
*/
public function multiselect(string $label, array|Collection $options, array|Collection $default = [], int $scroll = 5, bool|string $required = false, mixed $validate = null, string $hint = 'Use the space bar to select options.', ?string $name = null): self
{
return $this->runPrompt('\\Laravel\\Prompts\\multiselect', get_defined_vars());
return $this->runPrompt(multiselect(...), get_defined_vars());
}

/**
* Prompt the user to confirm an action.
*/
public function confirm(string $label, bool $default = true, string $yes = 'Yes', string $no = 'No', bool|string $required = false, mixed $validate = null, string $hint = '', ?string $name = null): self
{
return $this->runPrompt('\\Laravel\\Prompts\\confirm', get_defined_vars());
return $this->runPrompt(confirm(...), get_defined_vars());
}

/**
* Prompt the user to continue or cancel after pausing.
*/
public function pause(string $message = 'Press enter to continue...', ?string $name = null): self
{
return $this->runPrompt('\\Laravel\\Prompts\\pause', get_defined_vars());
return $this->runPrompt(pause(...), get_defined_vars());
}

/**
Expand All @@ -145,7 +144,7 @@ public function pause(string $message = 'Press enter to continue...', ?string $n
*/
public function suggest(string $label, array|Collection|Closure $options, string $placeholder = '', string $default = '', int $scroll = 5, bool|string $required = false, mixed $validate = null, string $hint = '', ?string $name = null): self
{
return $this->runPrompt('\\Laravel\\Prompts\\suggest', get_defined_vars());
return $this->runPrompt(suggest(...), get_defined_vars());
}

/**
Expand All @@ -156,7 +155,7 @@ public function suggest(string $label, array|Collection|Closure $options, string
*/
public function search(string $label, Closure $options, string $placeholder = '', int $scroll = 5, mixed $validate = null, string $hint = '', bool|string $required = true, ?string $name = null): self
{
return $this->runPrompt('\\Laravel\\Prompts\\search', get_defined_vars());
return $this->runPrompt(search(...), get_defined_vars());
}

/**
Expand All @@ -166,7 +165,7 @@ public function search(string $label, Closure $options, string $placeholder = ''
*/
public function multisearch(string $label, Closure $options, string $placeholder = '', int $scroll = 5, bool|string $required = false, mixed $validate = null, string $hint = 'Use the space bar to select options.', ?string $name = null): self
{
return $this->runPrompt('\\Laravel\\Prompts\\multisearch', get_defined_vars());
return $this->runPrompt(multisearch(...), get_defined_vars());
}

/**
Expand All @@ -176,63 +175,63 @@ public function multisearch(string $label, Closure $options, string $placeholder
*/
public function spin(Closure $callback, string $message = '', ?string $name = null): self
{
return $this->runPrompt('\\Laravel\\Prompts\\spin', get_defined_vars(), true);
return $this->runPrompt(spin(...), get_defined_vars(), true);
}

/**
* Display a note.
*/
public function note(string $message, ?string $type = null, ?string $name = null): self
{
return $this->runPrompt('\\Laravel\\Prompts\\note', get_defined_vars(), true);
return $this->runPrompt(note(...), get_defined_vars(), true);
}

/**
* Display an error.
*/
public function error(string $message, ?string $name = null): self
{
return $this->runPrompt('\\Laravel\\Prompts\\error', get_defined_vars(), true);
return $this->runPrompt(error(...), get_defined_vars(), true);
}

/**
* Display a warning.
*/
public function warning(string $message, ?string $name = null): self
{
return $this->runPrompt('\\Laravel\\Prompts\\warning', get_defined_vars(), true);
return $this->runPrompt(warning(...), get_defined_vars(), true);
}

/**
* Display an alert.
*/
public function alert(string $message, ?string $name = null): self
{
return $this->runPrompt('\\Laravel\\Prompts\\alert', get_defined_vars(), true);
return $this->runPrompt(alert(...), get_defined_vars(), true);
}

/**
* Display an informational message.
*/
public function info(string $message, ?string $name = null): self
{
return $this->runPrompt('\\Laravel\\Prompts\\info', get_defined_vars(), true);
return $this->runPrompt(info(...), get_defined_vars(), true);
}

/**
* Display an introduction.
*/
public function intro(string $message, ?string $name = null): self
{
return $this->runPrompt('\\Laravel\\Prompts\\intro', get_defined_vars(), true);
return $this->runPrompt(intro(...), get_defined_vars(), true);
}

/**
* Display a closing message.
*/
public function outro(string $message, ?string $name = null): self
{
return $this->runPrompt('\\Laravel\\Prompts\\outro', get_defined_vars(), true);
return $this->runPrompt(outro(...), get_defined_vars(), true);
}

/**
Expand All @@ -243,7 +242,7 @@ public function outro(string $message, ?string $name = null): self
*/
public function table(array|Collection $headers = [], array|Collection|null $rows = null, ?string $name = null): self
{
return $this->runPrompt('\\Laravel\\Prompts\\table', get_defined_vars(), true);
return $this->runPrompt(table(...), get_defined_vars(), true);
}

/**
Expand All @@ -257,21 +256,16 @@ public function table(array|Collection $headers = [], array|Collection|null $row
*/
public function progress(string $label, iterable|int $steps, ?Closure $callback = null, string $hint = '', ?string $name = null): self
{
return $this->runPrompt('\\Laravel\\Prompts\\progress', get_defined_vars(), true);
return $this->runPrompt(progress(...), get_defined_vars(), true);
}

/**
* Execute the given prompt passing the given arguments.
*
* @param callable-string $prompt
* @param array<mixed> $arguments
*/
protected function runPrompt(string $prompt, array $arguments, bool $ignoreWhenReverting = false): self
protected function runPrompt(callable $prompt, array $arguments, bool $ignoreWhenReverting = false): self
{
if (! function_exists($prompt)) {
throw new BadFunctionCallException("The given prompt {$prompt} does not exist.");
}

return $this->add(function (array $responses, mixed $previousResponse) use ($prompt, $arguments) {
unset($arguments['name']);

Expand Down

0 comments on commit 4925d1f

Please sign in to comment.