56 lines
1.9 KiB
Python
56 lines
1.9 KiB
Python
#!/usr/bin/env python3
|
|
"""Exercise one generated completion through an interactive shell."""
|
|
|
|
import os
|
|
import shlex
|
|
import shutil
|
|
import sys
|
|
|
|
import pexpect
|
|
|
|
shell, script, line = sys.argv[1:]
|
|
executable = shutil.which(shell)
|
|
if executable is None:
|
|
raise SystemExit(f"{shell} is not installed")
|
|
quoted_script = shlex.quote(script)
|
|
if shell == "bash":
|
|
args = ["--noprofile", "--norc", "-i"]
|
|
setup = 'PS1="PROMPT> "; source /usr/share/bash-completion/bash_completion; source ' + quoted_script
|
|
elif shell == "zsh":
|
|
args = ["-f"]
|
|
setup = 'PROMPT="PROMPT> "; '
|
|
if os.environ.get("COMPLETION_ZSH_MODULE_PATH"):
|
|
setup += "module_path=(" + shlex.quote(os.environ["COMPLETION_ZSH_MODULE_PATH"]) + "); "
|
|
if os.environ.get("COMPLETION_ZSH_FUNCTION_ROOT"):
|
|
setup += "fpath=(" + shlex.quote(os.environ["COMPLETION_ZSH_FUNCTION_ROOT"]) + "/**/*(N-/) $fpath); "
|
|
setup += "autoload -Uz compinit; compinit -D; source " + quoted_script
|
|
elif shell == "fish":
|
|
args = ["--no-config", "--interactive"]
|
|
setup = "function fish_prompt; printf '\\120\\122\\117\\115\\120\\124\\076\\040'; end; source " + quoted_script
|
|
else:
|
|
raise SystemExit(f"unsupported shell: {shell}")
|
|
|
|
child = pexpect.spawn(executable, args, env=os.environ.copy(), cwd=os.environ["PROBE_DIR"],
|
|
encoding="utf-8", timeout=15, dimensions=(24, 120))
|
|
child.delaybeforesend = 0.05
|
|
try:
|
|
child.setecho(False)
|
|
child.sendline(setup)
|
|
child.expect_exact("PROMPT> ")
|
|
child.send(line)
|
|
child.send("\t")
|
|
if os.environ.get("PROBE_LIST") == "1":
|
|
child.send("\t")
|
|
remaining = ["Readable text", "YAML output"]
|
|
captured = []
|
|
while remaining:
|
|
index = child.expect_exact(remaining)
|
|
captured.append(child.before + child.after)
|
|
remaining.pop(index)
|
|
print("".join(captured))
|
|
else:
|
|
child.sendline("")
|
|
child.expect_exact("PROMPT> ")
|
|
finally:
|
|
child.close(force=True)
|