Skip to content

Add OSC52 clipboard support #83

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 41 additions & 7 deletions elia_chat/widgets/chatbox.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from __future__ import annotations
import bisect
from dataclasses import dataclass
import base64
import bisect
import os
import sys

import pyperclip

from rich.cells import cell_len
from rich.console import RenderableType
Expand All @@ -21,6 +26,39 @@
from elia_chat.models import ChatMessage


class Helpers():

class Clipboard():

@staticmethod
def copy_to_clipboard(text):
"""Copies text to clipboard using the best available method."""
if Helpers.Clipboard._use_osc52():
Helpers.Clipboard._copy_to_clipboard_osc52(text)
else:
pyperclip.copy(text)

@staticmethod
def _use_osc52():
"""Determines if OSC52 should be used."""
x11_display = os.getenv("DISPLAY", "")
term = os.getenv("TERM", "")
tty_available = os.path.exists("/dev/tty")
return (x11_display == "") and tty_available and ("screen" in term or "xterm" in term or "tmux" in term)

@staticmethod
def _copy_to_clipboard_osc52(text):
"""Copies text to clipboard using OSC52."""
b64_text = base64.b64encode(text.encode()).decode()
osc52_sequence = f"\033]52;c;{b64_text}\a"
# Does not work because Textual seems to capture stdout:
#sys.stdout.write(osc52_sequence)
#sys.stdout.flush()
with open("/dev/tty", "w") as ttyd:
ttyd.write(osc52_sequence)
ttyd.flush()


class SelectionTextArea(TextArea):
class LeaveSelectionMode(Message):
"""Broadcast that the user wants to leave selection mode."""
Expand Down Expand Up @@ -144,9 +182,7 @@ def action_copy_to_clipboard(self) -> None:
title = "Message copied"

try:
import pyperclip

pyperclip.copy(text_to_copy)
Helpers.Clipboard.copy_to_clipboard(text_to_copy)
except pyperclip.PyperclipException as exc:
self.notify(
str(exc),
Expand Down Expand Up @@ -285,9 +321,7 @@ def action_copy_to_clipboard(self) -> None:
text_to_copy = self.message.message.get("content")
if isinstance(text_to_copy, str):
try:
import pyperclip

pyperclip.copy(text_to_copy)
Helpers.Clipboard.copy_to_clipboard(text_to_copy)
except pyperclip.PyperclipException as exc:
self.notify(
str(exc),
Expand Down