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

Prompt user for single file with bells and whistles



After observing how menu selection looks for zim (https://github.com/zimfw/zimfw), I decided to code a zsh prompt for user to provide a single file and have some desirable features. The question was recently posted on unix stackexchange. Here's the link: https://unix.stackexchange.com/q/735012/456507 which has code listing and GIFs. My first attempt at a single file prompt has multiple deficiencies at the moment for which I would like to overcome. My understanding is that these deficiencies require just a tiny list of options to turn on in zle, though I cannot find it...

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.

Here's the copy of my seed code that needs more watering and nutrients:
Create a file ``foofiles.zsh`` in a temp directory, chmod and run it:
```zsh
#!/usr/bin/env zsh
setopt extendedglob
setopt globcomplete
setopt nomenucomplete

mkdir ./foo 2>/dev/null
cd ./foo
touch abcd ebcf gbch ijkl mnop qrst unov wxyz
cd ..

function foofileselector {
    cd ./foo/
    local curcontext=foofileselector:::
    vared -p "Select file: " -c foofile;
    cd ..
}

function _foofileselector {
    _files -W ./foo/
}

zstyle ':completion:foofileselector:*:' insert-tab false completer _foofileselector

autoload -Uz compinit && compinit

foofileselector
[[ -z $foofile ]] && foofile="NIL"
echo "You selected: $foofile"

rm -rf ./foo/
```


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