Skip to content

Commit aab5aac

Browse files
committed
Add completes for shells by shtab
ptpython --print-completion bash | sudo tee /usr/share/bash-completion/completions/ptpython ptpython --print-completion zsh | sed 's/compdef ptpython/compdef -P pt(i|)python[0-9.]#/' | sudo tee /usr/share/zsh/site-functions/_ptpython # wait <iterative/shtab#87> ptpython --print-completion tcsh | sudo tee /etc/profile.d/ptpython.completion.csh
1 parent 0af5c10 commit aab5aac

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

ptpython/_shtab.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FILE = None
2+
DIRECTORY = DIR = None
3+
4+
5+
def add_argument_to(parser, *args, **kwargs):
6+
from argparse import Action
7+
Action.complete = None
8+
return parser

ptpython/entry_points/run_ptpython.py

+36-6
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
from textwrap import dedent
2929
from typing import Tuple
3030

31+
try:
32+
import shtab
33+
except ImportError:
34+
from . import _shtab as shtab
3135
import appdirs
3236
from prompt_toolkit.formatted_text import HTML
3337
from prompt_toolkit.shortcuts import print_formatted_text
@@ -41,6 +45,27 @@
4145

4246

4347
__all__ = ["create_parser", "get_config_and_history_file", "run"]
48+
# https://github.com/iterative/shtab/blob/master/examples/customcomplete.py#L11-L22
49+
PY_FILE = {
50+
"bash": "_shtab_greeter_compgen_py_file",
51+
"zsh": "_files -g '*.py'",
52+
"tcsh": "f:*.py",
53+
}
54+
PREAMBLE = {
55+
"bash": """\
56+
# $1=COMP_WORDS[1]
57+
_shtab_greeter_compgen_py_file() {
58+
compgen -d -- $1 # recurse into subdirs
59+
compgen -f -X '!*?.py' -- $1
60+
}
61+
""",
62+
"zsh": """\
63+
_script_args() {
64+
_arguments -S -s '(-)1:script_args:_files -g "*.py"' '*: :_files'
65+
}
66+
""",
67+
}
68+
SCRIPT_ARGS = {"zsh": "_script_args"}
4469

4570

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

5984

6085
def create_parser() -> _Parser:
61-
parser = _Parser(description="ptpython: Interactive Python shell.")
86+
parser = _Parser("ptpython", description="ptpython: Interactive Python shell.")
87+
shtab.add_argument_to(parser, preamble=PREAMBLE)
6288
parser.add_argument("--vi", action="store_true", help="Enable Vi key bindings")
6389
parser.add_argument(
6490
"-i",
@@ -70,23 +96,27 @@ def create_parser() -> _Parser:
7096
"--light-bg",
7197
action="store_true",
7298
help="Run on a light background (use dark colors for text).",
73-
),
99+
)
74100
parser.add_argument(
75101
"--dark-bg",
76102
action="store_true",
77103
help="Run on a dark background (use light colors for text).",
78-
),
104+
)
79105
parser.add_argument(
80106
"--config-file", type=str, help="Location of configuration file."
81-
)
82-
parser.add_argument("--history-file", type=str, help="Location of history file.")
107+
).complete = PY_FILE
108+
parser.add_argument(
109+
"--history-file", type=str, help="Location of history file."
110+
).complete = shtab.FILE
83111
parser.add_argument(
84112
"-V",
85113
"--version",
86114
action="version",
87115
version=metadata.version("ptpython"), # type: ignore
88116
)
89-
parser.add_argument("args", nargs="*", help="Script and arguments")
117+
parser.add_argument(
118+
"args", nargs=argparse.REMAINDER, help="Script and arguments"
119+
).complete = SCRIPT_ARGS
90120
return parser
91121

92122

setup.py

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
packages=find_packages("."),
1919
package_data={"ptpython": ["py.typed"]},
2020
install_requires=[
21+
"shtab",
2122
"appdirs",
2223
"importlib_metadata;python_version<'3.8'",
2324
"jedi>=0.16.0",
@@ -50,5 +51,6 @@
5051
extras_require={
5152
"ptipython": ["ipython"], # For ptipython, we need to have IPython
5253
"all": ["black"], # Black not always possible on PyPy
54+
"completion": ["shtab"],
5355
},
5456
)

0 commit comments

Comments
 (0)