Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add completes for shells by shtab #509

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions ptpython/_shtab.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FILE = None
DIRECTORY = DIR = None


def add_argument_to(parser, *args, **kwargs):
from argparse import Action
Action.complete = None
return parser
42 changes: 36 additions & 6 deletions ptpython/entry_points/run_ptpython.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
from textwrap import dedent
from typing import Tuple

try:
import shtab
except ImportError:
from .. import _shtab as shtab
import appdirs
from prompt_toolkit.formatted_text import HTML
from prompt_toolkit.shortcuts import print_formatted_text
Expand All @@ -41,6 +45,27 @@


__all__ = ["create_parser", "get_config_and_history_file", "run"]
# https://github.com/iterative/shtab/blob/master/examples/customcomplete.py#L11-L22
PY_FILE = {
"bash": "_shtab_greeter_compgen_py_file",
"zsh": "_files -g '*.py'",
"tcsh": "f:*.py",
}
PREAMBLE = {
"bash": """\
# $1=COMP_WORDS[1]
_shtab_greeter_compgen_py_file() {
compgen -d -- $1 # recurse into subdirs
compgen -f -X '!*?.py' -- $1
}
""",
"zsh": """\
_script_args() {
_arguments -S -s '(-)1:script_args:_files -g "*.py"' '*: :_files'
}
""",
}
SCRIPT_ARGS = {"zsh": "_script_args"}


class _Parser(argparse.ArgumentParser):
Expand All @@ -58,7 +83,8 @@ def print_help(self):


def create_parser() -> _Parser:
parser = _Parser(description="ptpython: Interactive Python shell.")
parser = _Parser("ptpython", description="ptpython: Interactive Python shell.")
shtab.add_argument_to(parser, preamble=PREAMBLE)
parser.add_argument("--vi", action="store_true", help="Enable Vi key bindings")
parser.add_argument(
"-i",
Expand All @@ -70,23 +96,27 @@ def create_parser() -> _Parser:
"--light-bg",
action="store_true",
help="Run on a light background (use dark colors for text).",
),
)
parser.add_argument(
"--dark-bg",
action="store_true",
help="Run on a dark background (use light colors for text).",
),
)
parser.add_argument(
"--config-file", type=str, help="Location of configuration file."
)
parser.add_argument("--history-file", type=str, help="Location of history file.")
).complete = PY_FILE
parser.add_argument(
"--history-file", type=str, help="Location of history file."
).complete = shtab.FILE
parser.add_argument(
"-V",
"--version",
action="version",
version=metadata.version("ptpython"), # type: ignore
)
parser.add_argument("args", nargs="*", help="Script and arguments")
parser.add_argument(
"args", nargs=argparse.REMAINDER, help="Script and arguments"
).complete = SCRIPT_ARGS
return parser


Expand Down
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
packages=find_packages("."),
package_data={"ptpython": ["py.typed"]},
install_requires=[
"shtab",
"appdirs",
"importlib_metadata;python_version<'3.8'",
"jedi>=0.16.0",
Expand Down Expand Up @@ -50,5 +51,6 @@
extras_require={
"ptipython": ["ipython"], # For ptipython, we need to have IPython
"all": ["black"], # Black not always possible on PyPy
"completion": ["shtab"],
},
)