black icon indicating copy to clipboard operation
black copied to clipboard

please add bash-completion (working version included)

Open eighthave opened this issue 5 years ago • 2 comments

black does not have bash-completion, hitting tab brings up nothing. I wrote up a quick bash-completion script that can be included under the same license as black uses. There are dynamic completion methods, where the bash-completion is generated from the Python code, but many Python devs find the required changes weird.

To test this, since this into either /etc/bash_completion.d/black or __:

# Copyright 2020, Hans-Christoph Steiner
_have black &&
_black()
{
    local cur prev

    COMPREPLY=()
    cur=${COMP_WORDS[COMP_CWORD]}
    prev=${COMP_WORDS[COMP_CWORD-1]}

    case $prev in
	-l|--line-length|--include|--exclude)
	    return 0;;
    esac
    if [[ "$cur" == -* ]]; then
	opts='-l -S -N -q -v -h'
	lopts='
	    --check
	    --config
	    --diff
	    --exclude
	    --fast
	    --include
	    --line-length
	    --quiet
	    --safe
	    --skip-numeric-underscore-normalization
	    --skip-string-normalization
	    --verbose
	    --version
	'	 
	COMPREPLY=( $(compgen -W "${opts[*]} ${lopts[*]}" -- $cur) )
    else
	_filedir
    fi
}
complete -F _black $filenames black

eighthave avatar Aug 07 '20 10:08 eighthave

Black use click as its command line parser library. click has a related library named click-completion to generate bash/zsh/fish completions. It is no need to write shell completion manually, just need someone who know click-completion to add this feature.

Freed-Wu avatar Aug 26 '22 13:08 Freed-Wu

This is method: https://click.palletsprojects.com/en/8.1.x/shell-completion/

Should contact the packager of linux distribution to do this job for black and blackd.

And this issue can be closed. :smile:

BTW, click's completion seems to have space to improve.

Expected behaviour:

❯ black --help <TAB>
no more arguments

Actual:

❯ black --help --<TAB>
unsorted
--code                        Format the code passed in as a string.
--line-length                 How many characters per line to allow.
--target-version              Python versions that should be supported by Black's output. [default: per-file auto-detection]
--pyi                         Format all input files like typing stubs regardless of file extension (useful when piping source on standard input).

Freed-Wu avatar Aug 26 '22 18:08 Freed-Wu