From 5b6ea6c9934cea832305465a5b555f7b3e1b88ec Mon Sep 17 00:00:00 2001 From: Noortheen Raja Date: Fri, 20 Nov 2020 19:59:14 +0530 Subject: [PATCH] feat: use PyperClipboard if available for ptk closes #3989 --- news/feat-clipboard-support.rst | 27 +++++++++++++++++++++++++++ setup.py | 2 +- xonsh/ptk_shell/shell.py | 13 ++++++++++++- 3 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 news/feat-clipboard-support.rst diff --git a/news/feat-clipboard-support.rst b/news/feat-clipboard-support.rst new file mode 100644 index 000000000..4c7b5be5d --- /dev/null +++ b/news/feat-clipboard-support.rst @@ -0,0 +1,27 @@ +**Added:** + +* support PTK's clipboard integration if pyperclip is installed. +So that some common emacs like +[cut/copy](https://github.com/prompt-toolkit/python-prompt-toolkit/blob/master/examples/prompts/system-clipboard-integration.py) +will work out of the box. + + +**Changed:** + +* + +**Deprecated:** + +* + +**Removed:** + +* + +**Fixed:** + +* + +**Security:** + +* diff --git a/setup.py b/setup.py index 77570ff36..4b6d0964a 100755 --- a/setup.py +++ b/setup.py @@ -342,7 +342,7 @@ def main(): } skw["cmdclass"]["develop"] = xdevelop skw["extras_require"] = { - "ptk": ["prompt-toolkit>=3.0"], + "ptk": ["prompt-toolkit>=3.0", "pyperclip"], "pygments": ["pygments>=2.2"], "mac": ["gnureadline"], "linux": ["distro"], diff --git a/xonsh/ptk_shell/shell.py b/xonsh/ptk_shell/shell.py index f835be652..002ad18e8 100644 --- a/xonsh/ptk_shell/shell.py +++ b/xonsh/ptk_shell/shell.py @@ -35,6 +35,12 @@ from prompt_toolkit.formatted_text import PygmentsTokens, to_formatted_text from prompt_toolkit.styles import merge_styles, Style from prompt_toolkit.styles.pygments import pygments_token_to_classname +try: + from prompt_toolkit.clipboard.pyperclip import PyperclipClipboard + + SYS_CLIPBOARD = True +except ImportError: + SYS_CLIPBOARD = False ANSI_OSC_PATTERN = re.compile("\x1b].*?\007") CAPITAL_PATTERN = re.compile(r"([a-z])([A-Z])") @@ -146,7 +152,12 @@ class PromptToolkitShell(BaseShell): winutils.enable_virtual_terminal_processing() self._first_prompt = True self.history = ThreadedHistory(PromptToolkitHistory()) - self.prompter = PromptSession(history=self.history) + + ptk_args = {"history": self.history} + if SYS_CLIPBOARD: + ptk_args["clipboard"] = PyperclipClipboard() + self.prompter = PromptSession(**ptk_args) + self.prompt_formatter = PTKPromptFormatter(self.prompter) self.pt_completer = PromptToolkitCompleter(self.completer, self.ctx, self) self.key_bindings = load_xonsh_bindings()