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

Commit

Permalink
bash-completion: use modern lazy-load directory
Browse files Browse the repository at this point in the history
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
  • Loading branch information
eli-schwartz committed May 28, 2019
1 parent f3e07b4 commit 2e20f13
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions dephell/commands/autocomplete.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from argparse import ArgumentParser
from pathlib import Path
from platform import platform
import os

# external
from appdirs import user_data_dir
Expand Down Expand Up @@ -49,23 +50,16 @@ def __call__(self):
def _bash(self):
script = make_bash_autocomplete()

# https://github.com/dephell/dephell/pull/62
path = Path.home() / '.local' / 'etc' / 'bash_completion.d' / 'dephell.bash-completion'
if platform().lower() == 'darwin':
# ref. https://itnext.io/programmable-completion-for-bash-on-macos-f81a0103080b
path = Path('/') / 'usr' / 'local' / 'etc' / 'bash_completion.d' / 'dephell.bash-completion'
# Install completions to the correct location for modern bash-completion.
# This will be sourced on-demand by bash-completion as soon as dephell is
# completed for the first time.
xdg_data_home = Path(os.getenv('XDG_DATA_HOME', '~/.local/share')).expanduser()
bashcomp_user_dir = Path(os.getenv('BASH_COMPLETION_USER_DIR', xdg_data_home / 'bash-completion'))
path = bashcomp_user_dir / 'completions'

path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(script)

for rc_name in ('.bashrc', '.profile', '.bash_profile'):
rc_path = Path.home() / rc_name
if not rc_path.exists():
continue
if 'bash_completion.d' not in rc_path.read_text():
with rc_path.open('a') as stream:
stream.write('\n\nsource "{}"\n'.format(str(path)))
break

def _zsh(self):
script = make_zsh_autocomplete()
path = Path(user_data_dir('dephell')) / '_dephell_zsh_autocomplete'
Expand Down

0 comments on commit 2e20f13

Please sign in to comment.