Skip to content
Open
21 changes: 0 additions & 21 deletions cmd2/clipboard.py

This file was deleted.

10 changes: 4 additions & 6 deletions cmd2/cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
)
from prompt_toolkit.application import create_app_session, get_app
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
from prompt_toolkit.clipboard.pyperclip import PyperclipClipboard
from prompt_toolkit.completion import Completer, DummyCompleter
from prompt_toolkit.formatted_text import ANSI, AnyFormattedText
from prompt_toolkit.history import InMemoryHistory
Expand Down Expand Up @@ -117,10 +118,6 @@
SubcommandRecord,
SubcommandSpec,
)
from .clipboard import (
get_paste_buffer,
write_to_paste_buffer,
)
from .command_set import CommandSet
from .completion import (
Choices,
Expand Down Expand Up @@ -793,6 +790,7 @@ def _(event: Any) -> None: # pragma: no cover
"refresh_interval": refresh_interval,
"rprompt": self.get_rprompt if enable_rprompt else None,
"style": DynamicStyle(get_pt_theme),
"clipboard": PyperclipClipboard(),
Comment thread
neoniobium marked this conversation as resolved.
Outdated
}

if self.stdin.isatty() and self.stdout.isatty():
Expand Down Expand Up @@ -3332,7 +3330,7 @@ def _redirect_output(self, statement: Statement) -> utils.RedirectionSavedState:
# if it's not gonna work. That way we throw the exception before we go
Comment thread
neoniobium marked this conversation as resolved.
Outdated
# run the command and queue up all the output. if this is going to fail,
# no point opening up the temporary file
current_paste_buffer = get_paste_buffer()
current_paste_buffer = self.main_session.clipboard.get_data().text
# create a temporary file to store output
new_stdout = cast(TextIO, tempfile.TemporaryFile(mode="w+")) # noqa: SIM115
redir_saved_state.redirecting = True
Expand Down Expand Up @@ -3362,7 +3360,7 @@ def _restore_output(self, statement: Statement, saved_redir_state: utils.Redirec
and not statement.redirect_to
):
self.stdout.seek(0)
write_to_paste_buffer(self.stdout.read())
self.main_session.clipboard.set_text(self.stdout.read())

with contextlib.suppress(BrokenPipeError):
# Close the file or pipe that stdout was redirected to
Expand Down
3 changes: 0 additions & 3 deletions docs/api/clipboard.md

This file was deleted.

1 change: 0 additions & 1 deletion docs/api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ incremented according to the [Semantic Version Specification](https://semver.org
signatures
- [cmd2.argparse_completer](./argparse_completer.md) - classes for `argparse`-based tab completion
- [cmd2.argparse_utils](./argparse_utils.md) - classes and functions for extending `argparse`
- [cmd2.clipboard](./clipboard.md) - functions to copy from and paste to the clipboard/pastebuffer
- [cmd2.colors](./colors.md) - StrEnum of all color names supported by the Rich library
- [cmd2.command_set](./command_set.md) - supports the definition of commands in separate classes to
be composed into cmd2.Cmd
Expand Down
9 changes: 4 additions & 5 deletions docs/features/clipboard.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ Nearly every operating system has some notion of a short-term storage area which
any program. Usually this is called the :clipboard: clipboard, but sometimes people refer to it as
the paste buffer.

`cmd2` integrates with the operating system clipboard using the
[pyperclip](https://github.com/asweigart/pyperclip) module. Command output can be sent to the
clipboard by ending the command with a greater than symbol:
`cmd2` integrates with the operating system clipboard using the clipboard integration of
[prompt_toolkit](https://github.com/jonathanslenders/prompt_toolkit) in conjunction with the
[pyperclip](https://github.com/prompt-toolkit/python-prompt-toolkit) module. Command output can be
sent to the clipboard by ending the command with a greater than symbol:

```text
mycommand args >
Expand Down Expand Up @@ -36,5 +37,3 @@ you can change it at any time from within your code.
If you would like your `cmd2` based application to be able to use the clipboard in additional or
alternative ways, you can use the following methods (which work uniformly on Windows, macOS, and
Linux).

::: cmd2.clipboard
17 changes: 13 additions & 4 deletions tests/test_cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
)
from unittest import mock

import pyperclip # type: ignore[import-untyped]
import pytest
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
from prompt_toolkit.completion import DummyCompleter
Expand All @@ -29,7 +30,6 @@
CommandSet,
Completions,
SubcommandRecord,
clipboard,
constants,
exceptions,
plugin,
Expand All @@ -50,6 +50,15 @@
)


def get_paste_buffer() -> str:
Comment thread
neoniobium marked this conversation as resolved.
Outdated
"""
Get the contents of the clipboard / paste buffer. This is just wrapper around
pyperclip paste() that provides the correct type annotation.

"""
return cast(str, pyperclip.paste())


def create_outsim_app():
c = cmd2.Cmd()
c.stdout = utils.StdSim(c.stdout)
Expand Down Expand Up @@ -873,7 +882,7 @@ def test_pipe_to_shell_error(redirection_app) -> None:

try:
# try getting the contents of the clipboard
_ = clipboard.get_paste_buffer()
_ = get_paste_buffer()
# pyperclip raises at least the following types of exceptions
# FileNotFoundError on Windows Subsystem for Linux (WSL) when Windows paths are removed from $PATH
# ValueError for headless Linux systems without Gtk installed
Expand All @@ -894,7 +903,7 @@ def test_send_to_paste_buffer(redirection_app: RedirectionApp, capsys: pytest.Ca
out, _err = capsys.readouterr()
assert out == "print\n"

lines = cmd2.clipboard.get_paste_buffer().splitlines()
lines = get_paste_buffer().splitlines()
assert len(lines) == 1
assert lines[0] == "poutput"

Expand All @@ -904,7 +913,7 @@ def test_send_to_paste_buffer(redirection_app: RedirectionApp, capsys: pytest.Ca
out, _err = capsys.readouterr()
assert out == "print\n"

lines = cmd2.clipboard.get_paste_buffer().splitlines()
lines = get_paste_buffer().splitlines()
assert len(lines) == 2
assert lines[0] == "poutput"
assert lines[1] == "poutput"
Expand Down
Loading