Skip to content

Commit

Permalink
Add completes for shells by shtab
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Freed-Wu committed Aug 28, 2022
1 parent 0af5c10 commit df59603
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
38 changes: 33 additions & 5 deletions ptpython/entry_points/run_ptpython.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from textwrap import dedent
from typing import Tuple

import shtab
import appdirs
from prompt_toolkit.formatted_text import HTML
from prompt_toolkit.shortcuts import print_formatted_text
Expand All @@ -41,6 +42,28 @@


__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:*.txt",
}
PREAMBLE = {
"bash": """\
# $1=COMP_WORDS[1]
_shtab_greeter_compgen_py_file() {
compgen -d -- $1 # recurse into subdirs
compgen -f -X '!*?.txt' -- $1
}
""",
"zsh": """\
_script_args() {
_arguments -S -s '(-)1:script_args:_files -g *.py' '*: :_files'
}
""",
"tcsh": "",
}
SCRIPT_ARGS = {"zsh": "_script_args"}


class _Parser(argparse.ArgumentParser):
Expand All @@ -59,6 +82,7 @@ def print_help(self):

def create_parser() -> _Parser:
parser = _Parser(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 +94,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="*", help="Script and arguments"
).complete = SCRIPT_ARGS
return parser


Expand Down
1 change: 1 addition & 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

0 comments on commit df59603

Please sign in to comment.