macocci7/file-selector-prompt

为您的 Laravel/Prompts 应用程序提供的额外文件选择提示。

v0.1.2 2024-08-21 01:04 UTC

This package is auto-updated.

Last update: 2024-09-21 01:52:35 UTC


README

为您的Laravel/Prompts应用程序提供的额外文件选择提示。

特性

文件选择提示在交互模式下帮助您快速、准确地输入文件路径。

2024-05-29.00-13-46.mp4

使用fileselector(),您可以快速、准确地输入文件路径。

安装

composer require macocci7/file-selector-prompt

可用函数

文件选择器

fileselector函数可以用于提供可能的选项的自动完成。

此函数将列出本地文件系统中与输入匹配的条目,作为建议选项。

用户仍然可以提供任何答案,无论是否有自动完成提示。

use function Macocci7\FileSelectorPrompt\fileselector;

$path = fileselector('Select a file to import.');

您还可以包含占位文本、默认值、必填值和信息性提示。

$path = fileselector(
    label: 'Select a file to import.',
    placeholder: 'E.g. ./vendor/autoload.php',
    default: '',
    required: 'The file path is required.',
    hint: 'Input the file path.',
);

如果您想执行额外的验证逻辑,可以将闭包传递给validate参数。

$path = fileselector(
    label: 'Select a file to import.',
    placeholder: 'E.g. ./vendor/autoload.php',
    hint: 'Input the file path.',
    validate: fn (string $value) => match (true) {
        !is_readable($value) => 'Cannot read the file.',
        default => null,
    },
);

此外,您可以通过传递闭包给transform参数,在验证之前更改输入。

$path = fileselector(
    label: 'Select a file to import.',
    placeholder: 'E.g. ./vendor/autoload.php',
    hint: 'Input the file path.',
    validate: fn (string $value) => match (true) {
        !is_readable($value) => 'Cannot read the file.',
        default => null,
    },
    transform: fn ($value) => realpath($value),
);

最后,如果您想根据文件扩展名过滤选择中的文件,可以将数组传递给extensions参数。

$path = fileselector(
    label: 'Select a file to import.',
    placeholder: 'E.g. ./vendor/autoload.php',
    hint: 'Input the file path.',
    validate: fn (string $value) => match (true) {
        !is_readable($value) => 'Cannot read the file.',
        default => null,
    },
    extensions: [
        '.json',
        '.php',
    ],
);

表单

Macocci7\FileSelectorPrompt\form函数包装了Laravel\Prompt\form并支持fileselector

use function Macocci7\FileSelectorPrompt\form;

$responses = form()
    ->fileselector(
        label: 'Select a file to import.',
        placeholder: 'E.g. ./vendor/autoload.php',
        hint: 'Input the file path.',
        validate: fn (string $value) => match (true) {
            !is_readable($value) => 'Cannot read the file.',
            default => null,
        },
        extensions: [
            '.json',
            '.php',
        ],
    )->submit();

许可证

MIT许可证