mirror of
https://github.com/mistralai/mistral-vibe
synced 2026-04-25 17:14:55 +02:00
Co-authored-by: Quentin Torroba <quentin.torroba@mistral.ai> Co-authored-by: Clément Siriex <clement.sirieix@mistral.ai> Co-authored-by: Kim-Adeline Miguel <kimadeline.miguel@mistral.ai> Co-authored-by: Michel Thomazo <michel.thomazo@mistral.ai> Co-authored-by: Clément Drouin <clement.drouin@mistral.ai>
70 lines
1.8 KiB
Python
70 lines
1.8 KiB
Python
from __future__ import annotations
|
|
|
|
import base64
|
|
import os
|
|
|
|
from textual.app import App
|
|
|
|
_PREVIEW_MAX_LENGTH = 40
|
|
|
|
|
|
def _copy_osc52(text: str) -> None:
|
|
encoded = base64.b64encode(text.encode("utf-8")).decode("ascii")
|
|
osc52_seq = f"\033]52;c;{encoded}\a"
|
|
if os.environ.get("TMUX"):
|
|
osc52_seq = f"\033Ptmux;\033{osc52_seq}\033\\"
|
|
|
|
with open("/dev/tty", "w") as tty:
|
|
tty.write(osc52_seq)
|
|
tty.flush()
|
|
|
|
|
|
def _shorten_preview(texts: list[str]) -> str:
|
|
dense_text = "⏎".join(texts).replace("\n", "⏎")
|
|
if len(dense_text) > _PREVIEW_MAX_LENGTH:
|
|
return f"{dense_text[: _PREVIEW_MAX_LENGTH - 1]}…"
|
|
return dense_text
|
|
|
|
|
|
def copy_selection_to_clipboard(app: App, show_toast: bool = True) -> str | None:
|
|
selected_texts = []
|
|
|
|
for widget in app.query("*"):
|
|
if not hasattr(widget, "text_selection") or not widget.text_selection:
|
|
continue
|
|
|
|
selection = widget.text_selection
|
|
|
|
try:
|
|
result = widget.get_selection(selection)
|
|
except Exception:
|
|
continue
|
|
|
|
if not result:
|
|
continue
|
|
|
|
selected_text, _ = result
|
|
if selected_text.strip():
|
|
selected_texts.append(selected_text)
|
|
|
|
if not selected_texts:
|
|
return None
|
|
|
|
combined_text = "\n".join(selected_texts)
|
|
|
|
try:
|
|
_copy_osc52(combined_text)
|
|
if show_toast:
|
|
app.notify(
|
|
f'"{_shorten_preview(selected_texts)}" copied to clipboard',
|
|
severity="information",
|
|
timeout=2,
|
|
markup=False,
|
|
)
|
|
return combined_text
|
|
except Exception:
|
|
app.notify(
|
|
"Failed to copy - clipboard not available", severity="warning", timeout=3
|
|
)
|
|
return None
|