Skip to content

Commit

Permalink
Merge pull request #74 from seapagan/fix-no-clipboard-error
Browse files Browse the repository at this point in the history
Cleanly handle exception if no clipboard mechanism is installed
  • Loading branch information
darrenburns authored Sep 8, 2024
2 parents d265aa3 + 173a220 commit c8ade45
Showing 1 changed file with 30 additions and 9 deletions.
39 changes: 30 additions & 9 deletions elia_chat/widgets/chatbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,29 @@ def action_cursor_word_right(self, select: bool = False) -> None:

def action_copy_to_clipboard(self) -> None:
text_to_copy = self.selected_text

if text_to_copy:
message = f"Copied {len(text_to_copy)} selected characters to clipboard."
self.notify(message, title="Selection copied")
title = "Selection copied"
else:
text_to_copy = self.text
message = f"Copied message ({len(text_to_copy)} characters)."
self.notify(message, title="Message copied")
title = "Message copied"

import pyperclip
try:
import pyperclip

pyperclip.copy(text_to_copy)
except pyperclip.PyperclipException as exc:
self.notify(
str(exc),
title="Clipboard error",
severity="error",
timeout=10,
)
else:
self.notify(message, title=title)

pyperclip.copy(text_to_copy)
self.visual_mode = False

def action_next_code_block(self) -> None:
Expand Down Expand Up @@ -206,11 +218,20 @@ def action_copy_to_clipboard(self) -> None:
if not self.selection_mode:
text_to_copy = self.message.message.get("content")
if isinstance(text_to_copy, str):
import pyperclip

pyperclip.copy(text_to_copy)
message = f"Copied message ({len(text_to_copy)} characters)."
self.notify(message, title="Message copied")
try:
import pyperclip

pyperclip.copy(text_to_copy)
except pyperclip.PyperclipException as exc:
self.notify(
str(exc),
title="Clipboard error",
severity="error",
timeout=10,
)
else:
message = f"Copied message ({len(text_to_copy)} characters)."
self.notify(message, title="Message copied")
else:
message = "Unable to copy message"
self.notify(message, title="Clipboard error", severity="error")
Expand Down

0 comments on commit c8ade45

Please sign in to comment.