Misterio77 / flavours

🎨💧 An easy to use base16 scheme manager that integrates with any workflow.
MIT License
506 stars 28 forks source link

Trouble getting zsh completion to work #31

Closed RichardBronosky closed 3 years ago

RichardBronosky commented 3 years ago

By reading the source code, I found the undocumented feature of flavours —completions zsh. But eval-ing it doesn’t work, and redirecting it to a file and sourcing it doesn’t work.

❯ flavours --completions zsh > /tmp/flavours.completions.zsh

❯ eval "$(flavours --completions zsh)"
_arguments:comparguments:325: can only be called from completion function

❯ source /tmp/flavours.completions.zsh
_arguments:comparguments:325: can only be called from completion function

Any help would be appreciated. (I have used bash since 1995 and just switched to zsh a few months ago, so I'm out of my element here.)

Misterio77 commented 3 years ago

Hey!

Normally you don't eval completions, if i'm not mistaken. I think (assuming you've already compinited somewhere on your .zshrc) there might be a user-wide directory for zsh completions, but i'm not really sure where it is. I do know you can place system-wide completions on /usr/share/zsh/site-functions.

About the --completions option, i didn't document it because it is not really as featureful as i wanted (i manually tweak them, so flavours apply can manually complete scheme names, for example). You can find the correct completions already generated on the completions folder on the repo.

Not sure which distro you use, but the AUR package does exactly that. It takes the zsh completion (on the folder i mentioned) and copies it to a file named _flavours on the /usr/share/zsh/site-functions/ directory. So just doing that should do the trick for you too!

Let me know if that gets you going.

Misterio77 commented 3 years ago

Actually here's an issue related: https://github.com/Misterio77/flavours/issues/4

RichardBronosky commented 3 years ago

Hey, thanks for the reply. You were correct about needing to add a symlink to a dir in the $fpath. That did get it working for me. I waited to reply because I wanted to get a solution together for others to benefit from.

Here is how I recommend bootstrapping macOS:

#!/usr/bin/env bash

## Recommended ENV vars and paths
CARGO_HOME="$HOME/.local/share/cargo"
FLAVOURS_CONFIG_FILE="$HOME/.config/flavours/config.toml"
FLAVOURS_DATA_DIRECTORY="$HOME/.local/share/flavours"

## May vary by user
SRC_DIRECTORY="$HOME/src/"
COMPLETIONS_DIRECTORY="$HOME/.local/share/zsh/site-functions"
SHELL_PROFILE="$HOME/.config/shell/profile"

## Ensure presence of ENV vars
for var_name in "CARGO_HOME" "FLAVOURS_DATA_DIRECTORY" "FLAVOURS_CONFIG_FILE"; do
    if ! grep -q $var_name "$SHELL_PROFILE"; then
        # SEE: https://mywiki.wooledge.org/BashFAQ/006#Evaluating_indirect.2Freference_variables
        if [[ -n "${ZSH_VERSION:-}" ]]; then
            var_value="${(P)var_value}"
        elif [[ -n "${BASH_VERSION:-}" ]]; then
            var_value="${!var_value}"
        fi
        # or if you are not opposed to eval...
        #   eval "var_value=\"\${$var_value}\""
        var_value="${var_value/$HOME/\$HOME}"
        echo "var_name=\"${var_value}\"" >> "$SHELL_PROFILE"
    fi
done

## Ensure presence of directories
for dir in \
        "$(dirname $FLAVOURS_CONFIG_FILE)" "$FLAVOURS_DATA_DIRECTORY" \
        "$SRC_DIRECTORY" "$COMPLETIONS_DIRECTORY" "$CARGO_HOME"; do
    [[ -d "$dir" ]] || mkdir -p "$dir"
done

brew install rust

cargo install flavours

## Add cargo bin dir to PATH
ln -s ../share/cargo/bin/ ~/.local/bin/cargo
## Assumes...
## Make reloading idempotent
# [[ -z "$PATH_ORIGINAL" ]] && export PATH_ORIGINAL=$PATH || export PATH=$PATH_ORIGINAL
## Adds `~/.local/bin` (and all subdirs) to $PATH
# export PATH="${$(find -L ~/.local/bin -type d -print0 | xargs -0n1 -I {} zsh -c 'printf %q: {}')%%:}:$PATH"

flavours update all

## Add zsh completions
[[ -d $COMPLETIONS_DIRECTORY ]] || COMPLETIONS_DIRECTORY="${fpath[1]}"
git clone https://github.com/Misterio77/flavours "$SRC_DIRECTORY/flavours"
ln -s "$SRC_DIRECTORY/flavours/completions/zsh" "$COMPLETIONS_DIRECTORY/_flavours"