Skip to content

Commit

Permalink
Add extension filter
Browse files Browse the repository at this point in the history
  • Loading branch information
macocci7 committed May 28, 2024
1 parent 7d79b7b commit f87c77c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
5 changes: 5 additions & 0 deletions playground/fileselector.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
default => null,
},
hint: 'The model name should be singular.',
extensions: [
'.md',
'.json',
'.php',
],
);

var_dump($model);
Expand Down
20 changes: 20 additions & 0 deletions src/FileSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class FileSelector extends Prompt

/**
* Create a new SuggestPrompt instance.
*
* @param string[] $extensions
*/
public function __construct(
public string $label,
Expand All @@ -36,6 +38,7 @@ public function __construct(
public bool|string $required = false,
public mixed $validate = null,
public string $hint = '',
public array $extensions = [],
) {
$this->options = fn (string $value) => $this->entries($value);

Expand Down Expand Up @@ -170,6 +173,7 @@ protected function entries(string $path)
return collect(iterator_to_array($this->glob($path)))
->reject(fn (string $entry) => match (true) {
str_ends_with($entry, '/.'), str_ends_with($entry, '/..') => true,
$this->isRejectable($entry) => true,
default => false,
})
->map(
Expand All @@ -181,6 +185,22 @@ protected function entries(string $path)
->all();
}

private function isRejectable(string $entry)
{
if (is_dir($entry)) {
return false;
}
if (count($this->extensions) === 0) {
return false;
}
foreach ($this->extensions as $extension) {
if (str_ends_with($entry, $extension)) {
return false;
}
}
return true;
}

/**
* automatically complete the text input
*/
Expand Down
4 changes: 3 additions & 1 deletion src/FormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,10 @@ public function progress(string $label, iterable|int $steps, ?Closure $callback

/**
* Prompt the user for text input with auto-completion of filepath.
*
* @param array<string> $extensions
*/
public function fileselector(string $label, string $placeholder = '', string $default = '', int $scroll = 5, bool|string $required = false, mixed $validate = null, string $hint = '', ?string $name = null): self
public function fileselector(string $label, string $placeholder = '', string $default = '', int $scroll = 5, bool|string $required = false, mixed $validate = null, string $hint = '', ?string $name = null, array $extensions = []): self
{
return $this->runPrompt(fileselector(...), get_defined_vars());
}
Expand Down
4 changes: 3 additions & 1 deletion src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,10 @@ function form(): FormBuilder
if (! function_exists('\Laravel\Prompts\fileselector')) {
/**
* Prompt the user for text input with auto-completion of filepath.
*
* @param array<string> $extensions
*/
function fileselector(string $label, string $placeholder = '', string $default = '', int $scroll = 5, bool|string $required = false, mixed $validate = null, string $hint = ''): string
function fileselector(string $label, string $placeholder = '', string $default = '', int $scroll = 5, bool|string $required = false, mixed $validate = null, string $hint = '', array $extensions = []): string
{
return (new FileSelector(...func_get_args()))->prompt();
}
Expand Down

0 comments on commit f87c77c

Please sign in to comment.