Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Prompt user for single file with bells and whistles



On Fri, Feb 10, 2023 at 10:37 PM OG Code Poet <ogcodepoet@xxxxxxxxx> wrote:
>
> 1. Highlight: current menu selection item should be highlighted with reverse-video.
> 2. Order: the possible file completions should be shown below the prompt.
> 3. Prompt stepping: Prompt shouldn't step below after printing possible options above. This is related to 2.
> 4. Only one: Tab complete only first item, as we are asking for only 1 file name from user, not multiple. (This is what I mean by "functionally correct")
> 5. Size: If there are more possible completions than can fit below the prompt, that case is handled elegantly. Assume alternate screen, and no screen movement or scrolling available.
> 6. Bonus: If you could think of yet another nice feature to have for a single file selector, feel free to add. Though it would be nice to have it in a separate listing or with comments to identify how to remove your that from solution for 1-5 above.
>
> Note: The answer would be self-contained zsh code. It wouldn't ask user to edit .zshrc or install some zsh extension/plugin/framework/library.

How about this?

    #!/usr/bin/env -S zsh -fi

    zstyle ':completion:*' completer _files
    zstyle ':completion:*' menu yes select
    autoload -Uz compinit
    compinit
    vared -p "Select file: " -c file
    print -r -- "You selected: ${(q-)file}"

Roman.




Messages sorted by: Reverse Date, Date, Thread, Author