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

Commit

Permalink
Merge pull request #132 from eli-schwartz/bash-on-demand-completion
Browse files Browse the repository at this point in the history
bash-completion: use modern lazy-load directory
  • Loading branch information
orsinium committed May 29, 2019
2 parents 531ff66 + a813d7f commit 9364bb9
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 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,22 +50,29 @@ 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.
if 'BASH_COMPLETION_USER_DIR' in os.environ:
bashcomp_user_dir = Path(os.environ.get('BASH_COMPLETION_USER_DIR'))
else:
bashcomp_user_dir = Path(os.getenv('XDG_DATA_HOME', '~/.local/share')).expanduser() / 'bash-completion'
path = bashcomp_user_dir / 'completions' / 'dephell'

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
# We cannot reliably assume bash-completion 2.x exists on macOS,
# so inject it into the user's bashrc
if platform().lower() == 'darwin':
for rc_name in ('.bashrc', '.profile', '.bash_profile'):
rc_path = Path.home() / rc_name
if not rc_path.exists():
continue
if 'completions/dephell' 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()
Expand Down

0 comments on commit 9364bb9

Please sign in to comment.