Skip to content

[Cmd] use prompt-toolkit native clipboard integration#1721

Open
neoniobium wants to merge 13 commits into
python-cmd2:mainfrom
neoniobium:clipboard
Open

[Cmd] use prompt-toolkit native clipboard integration#1721
neoniobium wants to merge 13 commits into
python-cmd2:mainfrom
neoniobium:clipboard

Conversation

@neoniobium

Copy link
Copy Markdown
Contributor

Addresses #1720

@codecov

codecov Bot commented Jul 17, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.61%. Comparing base (9ab5858) to head (60f6f5f).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1721   +/-   ##
=======================================
  Coverage   99.61%   99.61%           
=======================================
  Files          23       22    -1     
  Lines        5915     5930   +15     
=======================================
+ Hits         5892     5907   +15     
  Misses         23       23           
Flag Coverage Δ
unittests 99.61% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

Comment thread tests/test_cmd2.py Outdated
Comment thread cmd2/cmd2.py Outdated
Comment thread cmd2/cmd2.py Outdated
@tleonhardt

Copy link
Copy Markdown
Member

@neoniobium You'll need to merge master into your branch.

Btw, you should now be able to create branches in the main repo, so you don't need to use a Fork anymore unless you want to. Using branches in main repo, makes it easier for others to help collaborate.

@neoniobium

Copy link
Copy Markdown
Contributor Author

@neoniobium You'll need to merge master into your branch.

Btw, you should now be able to create branches in the main repo, so you don't need to use a Fork anymore unless you want to. Using branches in main repo, makes it easier for others to help collaborate.

Ok, next time I will create a branch instead of using my frok.

Comment thread cmd2/cmd2.py Outdated
Comment thread cmd2/cmd2.py Outdated
Comment thread cmd2/cmd2.py Outdated
Comment thread cmd2/cmd2.py Outdated
Comment thread tests/test_cmd2.py Outdated
@neoniobium

Copy link
Copy Markdown
Contributor Author

Thanks again for the review.

I was also thinking whether the clipboard should be shared will each session, or example read_secret. I think good arguments for either case. I would like to know your opinion on that point.

Furthermore I was wondering allow_clipboard is still a good variable name since is now allow a clipboard available.

@tleonhardt

Copy link
Copy Markdown
Member

@kmvanbrunt Do you have any thoughts on this PR?

Comment thread cmd2/cmd2.py
Comment thread cmd2/cmd2.py

# Only enable PyperclipClipboard if the system clipboard is accessible to Pyperclip.
try:
cb = PyperclipClipboard()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PyperclipClipboard is instantiated even if allow_clipboard is False.

The docstring states that setting allow_clipboard to False disables clipboard interactions. However, PyperclipClipboard is currently added to the PromptSession regardless of this flag (if the system clipboard is accessible). This natively enables system clipboard pasting inside the prompt, which contradicts the intention of disabling all clipboard interactions. If allow_clipboard is meant to disable all system clipboard access, you should conditionally check self.allow_clipboard. (Note: If you apply this fix, you will also need to update test_init_with_no_clipboard_allowed).

Recommend wrapping code ath initializes cb inside a check:

if self.allow_clipboard:
    try:
        cb = PyperclipClipboard()
        ...

Comment thread cmd2/exceptions.py
"""Custom exception class for when redirecting or piping output fails."""


class ClipboardError(Exception):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ClipboardError will cause a generic stack trace since it is not caught in onecmd_plus_hooks.

ClipboardError inherits directly from Exception. Because there is no explicit except ClipboardError: block in onecmd_plus_hooks (where commands are executed), raising this error during output redirection will trigger the generic except Exception: handler, which prints a full stack trace via self.pexcept(ex).

You should either catch ClipboardError gracefully in onecmd_plus_hooks alongside RedirectionError, or simply have this class inherit from RedirectionError so it gets handled cleanly by the existing logic, i.e.:

class ClipboardError(RedirectionError):
    """Custom exception class for when clipboard operations fail."""

@tleonhardt

Copy link
Copy Markdown
Member

@neoniobium I pushed an update to your branch that merged in changes from main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants