Skip to content
This repository has been archived by the owner on Jan 12, 2021. It is now read-only.

bash-completion: use modern lazy-load directory #132

Merged
merged 1 commit into from May 29, 2019

Conversation

eli-schwartz
Copy link
Contributor

The legacy bash_completion.d directory requires every script be sourced at shell startup, which is slow. Since bash-completion 2.x (released in 2012), completions are loaded on-demand -- a minimal completion stub is defined as the default command completer, which will search for a script in the completionsdir named the same as the command, and source this script in order to update the command completion.

Make use of this lazy-loaded completion. This entails:

  • moving the installation location
  • getting rid of the legacy modification of .bashrc which is no longer needed

@eli-schwartz
Copy link
Contributor Author

I'm not sure how to handle zsh completions. As I'm not a zsh user, I cannot speak authoritatively, but I don't know of a comparable user directory (the zsh $fpath seems to be totally up to the user).

@eli-schwartz
Copy link
Contributor Author

As a side note: for system packaging, I'm using the following:

    # make shell completions
    python -c 'from dephell.actions._autocomplete import make_bash_autocomplete as comp; print(comp())' | \
        install -Dm644 /dev/stdin "${pkgdir}"/usr/share/bash-completion/completions/dephell

    # rewrite zsh completion to support autoloading
    {   printf '#compdef dephell\n'
        python -c 'from dephell.actions._autocomplete import make_zsh_autocomplete as comp; print(comp())' | \
            sed 's/^compdef _dephell dephell$/_dephell/'
    } | install -Dm644 /dev/stdin "${pkgdir}"/usr/share/zsh/site-functions/_dephell

I've traced the imports and executed the autocomplete functions by hand for each shell, since I need to install all of them and to /usr/share instead of $HOME... the zsh script needs a bit of tweaking to support lazy-loaded completion from the system $fpath

I would be interested to see a way to install these via dephell itself, but I'm not sure how it would be done -- furthermore, it would need to know how to select between the user directory and the system directory.

@orsinium
Copy link
Member

I'm not sure how to handle zsh completions

I discovered this topic. You have to modify .zshrc anyway. Autocomplete can be added in $fpath, plugins or be directly loaded by source.

@orsinium
Copy link
Member

We had here hack for Mac OS. I'm afraid that these changes will not work on it. Maybe, we can check .exists() for this path and fallback on old logic if this lazy-load dir doesn't exists?

@eli-schwartz
Copy link
Contributor Author

eli-schwartz commented May 28, 2019

macOS should be fine as long as the user has installed bash-completion@2 from homebrew and not the legacy bash-completion version 1. And I strongly encourage macOS users to do exactly that; later versions of bash-completion are far superior.

It is possible to check which version is installed using pkg-config --exists 'bash-completion >= 2', assuming the user has brew install'ed pkg-config.

@orsinium
Copy link
Member

And I strongly encourage macOS users to do exactly that; later versions of bash-completion are far superior.

I'm not Mac OS user. And I don't want to force them to do any additional manipulations to make DepHell work. It would be cool to support everything that we can.

@eli-schwartz
Copy link
Contributor Author

Okay -- let's use the modern bash-completion directory, but on macOS also add it to the bashrc just in case. This should work fine regardless of bash-completion version and even work if there is no bash-completion at all.

On linux bash-completion is pretty standard and up to date -- and most distros will even install it automatically for you.

@orsinium
Copy link
Member

Perfect, thank you :) I'll check and merge it tomorrow.

@orsinium
Copy link
Member

It writes ~/.local/share/bash-completion/completions file and doesn't work on my system: GNU bash, version 4.4.19(1)-release (x86_64-pc-linux-gnu)

@orsinium
Copy link
Member

Oh, I see, the path must be ~/.local/share/bash-completion/completions/dephell

The legacy bash_completion.d directory requires every script be sourced
at shell startup, which is slow. Since bash-completion 2.x (released in
2012), completions are loaded on-demand -- a minimal completion stub is
defined as the default command completer, which will search for a script
in the completionsdir named the same as the command, and source this
script in order to update the command completion.

Make use of this lazy-loaded completion. This entails:
- moving the installation location
- getting rid of the legacy modification of .bashrc which is no longer
  needed
@orsinium orsinium merged commit 9364bb9 into dephell:master May 29, 2019
@orsinium
Copy link
Member

Thank you :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants