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

Add recipes to generated completion scripts #573

Open
4 of 5 tasks
casey opened this issue Jan 15, 2020 · 14 comments
Open
4 of 5 tasks

Add recipes to generated completion scripts #573

casey opened this issue Jan 15, 2020 · 14 comments

Comments

@casey
Copy link
Owner

casey commented Jan 15, 2020

In #572, I'll be landing a --completions flag to make just output completion scripts for various shells. However, these completion scripts should also allow completing recipe names (like build or test). I'll need to add this functionality separately.

See #223 for completion scripts that others have written.

Add recipe completions to:

  • Bash
  • Zsh
  • Fish
  • PowerShell
  • Elvish
@casey casey added this to the soon milestone Jan 15, 2020
@casey casey modified the milestones: soon, eventually Jan 15, 2020
@gotcha
Copy link

gotcha commented Jan 15, 2020

@casey If the just.bash file (see https://github.com/casey/just/pull/572/files#diff-1dcc75cc227d1a56d11d839f20f6a50c) is generated with clap, how do you plan to maintain it
if I fiddle to merge the recipe completion I have in https://github.com/gotcha/just-bash-completion ?

@casey
Copy link
Owner Author

casey commented Jan 15, 2020

@gotcha Good question!

I was thinking that I would first write the completion script to a string while generating it in config.rs, and then modifying it before printing it. (Either by appending more stuff to the end, or doing a search and replace if it's more involved than that.)

If you can show me what should be added to the completion script (either in a gist, or a PR), then I can make the changes in rust to add it when it's generated too.

@gergelyk
Copy link

In case of bash, I do this dirty trick to replace <ARGUMENTS>... with (fine tuned) output of just --list.

source <(OPTS=$(just --list | tail -n +2 | sed 's/    / /g' | tr -d '\n'); just --completions bash | sed "s/  <ARGUMENTS>... /$OPTS/g")

Do you think it would make sense to include similar solution in default script generated by just --completions bash? Probably some dedicated variant of just --list would be applicable here.

@casey
Copy link
Owner Author

casey commented Feb 12, 2021

That's an awesome trick! I'd be worried about adding it into the default script, just because it adds dependencies on tail, sed, and tr. But if you can modify the bash completion script to include recipe names without using those additional commands, I can modify the scripts when they're generated, here: https://github.com/Insomniak47/just/blob/master/src/subcommand.rs

@gergelyk
Copy link

I agree. tail, sed, tr is probably what we wand to avoid. As an alternative I would suggest extending functionality of --list. E.g. --list-inline would give the same output as --list but in a single line, without header line and space-separated. This way completion script could be modified so that instead of <ARGUMENTS>... we have $(just --list-inline).

@casey
Copy link
Owner Author

casey commented Feb 12, 2021

Ah, okay, I misunderstood. Actually, that's already available if you do just --summary, it'll print a space-separated list of recipe names on a single line.

@gergelyk
Copy link

Great! I confirm, the following does the trick:

source <(just --completions bash | sed 's/ <ARGUMENTS>... /$(just --summary)/g')

We will appreciate making it default in just --completions bash.

@casey
Copy link
Owner Author

casey commented Feb 12, 2021

Actually, can you check to see if you're using the latest version of Just? I think recipe completions may have already been added to the latest version of the bash completions script:

local recipes=$(just --summary --color never 2> /dev/null)

@gergelyk
Copy link

gergelyk commented Feb 14, 2021

You are correct. just of mine wasn't up to date. After upgrading to v0.8.4 I got it working. The only remark I can make is that it works only for the first task that I try to execute. For instance here only foo is completed and bar, baz aren't:

just foo bar baz

@casey
Copy link
Owner Author

casey commented Feb 15, 2021

After upgrading to v5.4 I got it working.

Ah, great. I don't use bash myself, so I forgot that it already had completions.

The only remark I can make is that it works only for the first task that I try to execute.

That's definitely an unfortunate limitation of the completion script. I opened #756 to track it.

@8tm
Copy link

8tm commented Feb 15, 2021

This solution does not support aliases which are sometimes made up of several characters

just --summary

To support aliases and other section we can use this command:

just --list --list-heading "" | awk '{print $1}'

You can also add checking for justfile to expand completions only when file exists:

if [ -f justfile ]; then
    options=$(just --list --list-heading "" | awk '{print $1}')
fi

Is it possible to add autocomplete that would match only sections and aliases from justfile?
Without auto-completion for just?
In case when we would like use only auto-completion for our project we could simplify bash completions to:

_just()
{
    COMPREPLY=()
    options=()

    if [ -f justfile ]; then
        options=$(just --list --list-heading "" | awk '{print $1}')
    fi

    COMPREPLY=( $(compgen -W "${options}" -- ${COMP_WORDS[COMP_CWORD]}) )
}

complete -F _just j

j is simple wrapper function for just:

function j()
{
    if [ $# -eq 0 ]
    then
        just --list
    else
        just "$@"
    fi
}

In this case I would like to request new command something like --project-only or something like that.

And it resolving limitation of the completion script so You can press 2 xTAB, use option, use again 2 x TAB, use next option etc etc. So it helps with auto-completion for:

just foo bar baz

Like:

obraz

It also works dynamically for each of project, so You will have other auto-completions for each of justfile (without changing anything). Like:

obraz

@casey
Copy link
Owner Author

casey commented Feb 15, 2021

This solution does not support aliases which are sometimes made up of several characters.

I wasn't sure whether completion scripts should complete aliases, as well as full recipe names. My thought was completions scripts should only complete full recipe names, since aliases are often made to be shorter to type, but might clutter completion results. I'm curious how others feel about this though.

You can also add checking for justfile to expand completions only when file exists:

if [ -f justfile ]; then
    options=$(just --list --list-heading "" | awk '{print $1}')
fi

Unfortunately this only works if the justfile is in the current directory, but not if it's in a parent directory.

Is it possible to add autocomplete that would match only sections and aliases from justfile? Without auto-completion for just?

Do you mean auto-complete that only completes recipe names and aliases? Is this because it's simpler, or because it's undesirable to complete flags and options?

@gergelyk
Copy link

I wasn't sure whether completion scripts should complete aliases, as well as full recipe names.

I don't have sharp opinion, but probably yes, I would include aliases too.

@8tm
Copy link

8tm commented Feb 15, 2021

Solution for Bash with parent directories (fixed):

#!/usr/bin/env bash

_just()
{
    COMPREPLY=()
    options=""
    nearest_existing_justfile_path=""
    path=""
    DEFAULT_IFS="${IFS}"
    IFS="/";


    for current_folder_name in $(pwd);
    do
        IFS="${DEFAULT_IFS}"
        path="${path}/${current_folder_name}"

        if [ -f "${path}/justfile" ]; then
            nearest_existing_justfile_path="${path}/justfile"
        fi;

        IFS="/"
    done;

    IFS="${DEFAULT_IFS}"

    if [ -f "${nearest_existing_justfile_path}" ]; then
        options=$(just --list --list-heading "" --justfile "${nearest_existing_justfile_path}" | awk '{print $1}')
    fi

    COMPREPLY=($(compgen -W "${options}" -- "${COMP_WORDS[COMP_CWORD]}"))
}

complete -F _just j

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

No branches or pull requests

4 participants