From 5855921dbc8eaa8a46c4a05ce9eb913755342239 Mon Sep 17 00:00:00 2001 From: Kaiyin Zhong Date: Tue, 13 Nov 2018 14:00:18 +0100 Subject: [PATCH] user bash completion path by default --- src/infi/docopt_completion/bash.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/infi/docopt_completion/bash.py b/src/infi/docopt_completion/bash.py index c91831b..d7fdc2d 100644 --- a/src/infi/docopt_completion/bash.py +++ b/src/infi/docopt_completion/bash.py @@ -1,6 +1,7 @@ from .common import CompletionGenerator import os import string +from pathlib import Path FILE_TEMPLATE = """{0}\ncomplete -o bashdefault -o default -o filenames -F _{1} {2}""" @@ -27,12 +28,13 @@ _{1}_{0} ;;""" + class BashCompletion(CompletionGenerator): def get_name(self): return "BASH with bash-completion" def get_completion_path(self): - return "/etc/bash_completion.d" + return os.path.join(Path.home(), ".bash_completion.d") def get_completion_filepath(self, cmd): completion_path = self.get_completion_path() @@ -41,7 +43,8 @@ def get_completion_filepath(self, cmd): def create_subcommand_switch(self, cmd_name, level_num, subcommands, opts): if len(subcommands) == 0: return "" - subcommand_cases = '\n'.join(CASE_TEMPLATE.format(subcommand, cmd_name) for subcommand in subcommands) + subcommand_cases = '\n'.join(CASE_TEMPLATE.format( + subcommand, cmd_name) for subcommand in subcommands) return SUBCOMMAND_SWITCH_TEMPLATE.format(level_num=level_num, subcommand_cases=subcommand_cases) def create_compreply(self, param_tree): @@ -49,16 +52,19 @@ def create_compreply(self, param_tree): # in this case there are (usually) no subcommands to suggest, and only flags, so it's ok to suggest files, # and if the user types "-" first, then only the flags will be suggested flag = '-fW' if len(param_tree.arguments) > 0 else '-W' - word_list = " ".join(param_tree.options) + " " + " ".join(param_tree.subcommands.keys()) + word_list = " ".join(param_tree.options) + " " + \ + " ".join(param_tree.subcommands.keys()) return "{} '{}'".format(flag, word_list) def create_section(self, cmd_name, param_tree, option_help, level_num): subcommands = param_tree.subcommands opts = param_tree.options - subcommand_switch = self.create_subcommand_switch(cmd_name, level_num, subcommands, opts) + subcommand_switch = self.create_subcommand_switch( + cmd_name, level_num, subcommands, opts) res = SECTION_TEMPLATE.format(cmd_name=cmd_name, level_num=level_num, - compreply=self.create_compreply(param_tree), + compreply=self.create_compreply( + param_tree), subcommand_switch=subcommand_switch, op="eq" if len(subcommands) > 0 else 'ge') for subcommand_name, subcommand_tree in subcommands.items(): @@ -74,9 +80,11 @@ def sanitize_name(self, name): return "".join([char for char in name if char in valid_chars]) def get_completion_file_content(self, cmd, param_tree, option_help): - completion_file_inner_content = self.create_section(self.sanitize_name(cmd), param_tree, option_help, 1) + completion_file_inner_content = self.create_section( + self.sanitize_name(cmd), param_tree, option_help, 1) return FILE_TEMPLATE.format(completion_file_inner_content, self.sanitize_name(cmd), cmd) + class ManualBashCompletion(BashCompletion): def get_completion_path(self): return "."