Skip to content

Commit 6280a07

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 6280a07

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

ptpython/entry_points/run_ptpython.py

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

31+
import shtab
3132
import appdirs
3233
from prompt_toolkit.formatted_text import HTML
3334
from prompt_toolkit.shortcuts import print_formatted_text
@@ -41,6 +42,27 @@
4142

4243

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

4567

4668
class _Parser(argparse.ArgumentParser):
@@ -59,6 +81,7 @@ def print_help(self):
5981

6082
def create_parser() -> _Parser:
6183
parser = _Parser(description="ptpython: Interactive Python shell.")
84+
shtab.add_argument_to(parser, preamble=PREAMBLE)
6285
parser.add_argument("--vi", action="store_true", help="Enable Vi key bindings")
6386
parser.add_argument(
6487
"-i",
@@ -70,23 +93,27 @@ def create_parser() -> _Parser:
7093
"--light-bg",
7194
action="store_true",
7295
help="Run on a light background (use dark colors for text).",
73-
),
96+
)
7497
parser.add_argument(
7598
"--dark-bg",
7699
action="store_true",
77100
help="Run on a dark background (use light colors for text).",
78-
),
101+
)
79102
parser.add_argument(
80103
"--config-file", type=str, help="Location of configuration file."
81-
)
82-
parser.add_argument("--history-file", type=str, help="Location of history file.")
104+
).complete = PY_FILE
105+
parser.add_argument(
106+
"--history-file", type=str, help="Location of history file."
107+
).complete = shtab.FILE
83108
parser.add_argument(
84109
"-V",
85110
"--version",
86111
action="version",
87112
version=metadata.version("ptpython"), # type: ignore
88113
)
89-
parser.add_argument("args", nargs="*", help="Script and arguments")
114+
parser.add_argument(
115+
"args", nargs=argparse.REMAINDER, help="Script and arguments"
116+
).complete = SCRIPT_ARGS
90117
return parser
91118

92119

setup.py

+1
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",

0 commit comments

Comments
 (0)