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

Feature: FileSelector #150

Closed
wants to merge 5 commits into from
Closed

Conversation

macocci7
Copy link
Contributor

@macocci7 macocci7 commented May 28, 2024

This PR adds support for file selector.

Here's a demo video, which you can play with for yourself using php playground/fileselector.php on this branch.

2024-05-29.00-13-46.mp4

Purpose

fileselector supplies auto complete function in interactive mode.
Creating a file selector-like function is possible by using suggest prompt with callback for options.
However, automatically updating options after auto completion is impossible.
And filterling options with file extensions might be vervose.
Using fileselector solves these issues and makes codes more simple.

Usage

use function Laravel\Prompts\fileselector;

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

This function lists the entries in the directory on the local file system that matches the input, as the options for suggest.

You can automatically complete the input like the suggest prompt by pressing TAB on the selected option, then continue to input.
Press Enter to finish input.
Actually this function was created from the copy of the suggest promt.

You can filter the options with the parameter extensions.
If the parameter extensions is specified, the path and directories whose path ends match one of the extensions array elements are returned as options.

Points to concern

  • I don't really like the way of implementation for the option update process after input completion.(FileSelector::autoComplete()) Please let me know if there is a better way.
  • I removed the SHIFT_TAB key binding in order to use TAB for input completion. Is there a problem?
  • I am concerned that I was not able to implement the stub as a file list during testing.

@macocci7 macocci7 marked this pull request as draft May 28, 2024 15:31
@macocci7 macocci7 marked this pull request as ready for review May 29, 2024 01:54
@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If possible, please consider releasing your code as a package so that the community can still take advantage of your contributions!

If you feel absolutely certain that this code corrects a bug in the framework, please "@" mention me in a follow-up comment with further explanation so that GitHub will send me a notification of your response.

@macocci7
Copy link
Contributor Author

macocci7 commented Jun 2, 2024

OK.
Thank you for your suggestion!
I've released the code as my package file-selector-prompt.

@ProjektGopher
Copy link
Contributor

@macocci7 I'd love to add this into my Community Prompts package! I'll open an issue on your package as well, in case this comment doesn't get surfaced.

@macocci7
Copy link
Contributor Author

@ProjektGopher I opened PR #5 on Community Prompts.
Please check it out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants