Skip to content
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

Catch possible pyperclip exception if no clipboard mechanism installed #98

Merged
merged 1 commit into from
Sep 4, 2024
Merged
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
38 changes: 25 additions & 13 deletions src/posting/widgets/text_area.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
from dataclasses import dataclass
import os
import shlex
import subprocess
import tempfile
from typing_extensions import Literal
from dataclasses import dataclass

from textual import on
from textual.app import ComposeResult
from textual.binding import Binding
from textual.containers import Horizontal, Vertical
from textual.message import Message
from textual.reactive import reactive, Reactive
from textual.widgets import TextArea, Label, Select, Checkbox
from textual.reactive import Reactive, reactive
from textual.widgets import Checkbox, Label, Select, TextArea
from textual.widgets.text_area import Selection, TextAreaTheme
from typing_extensions import Literal

from posting.config import SETTINGS
from posting.themes import SyntaxTheme, Theme

from posting.widgets.select import PostingSelect


Expand All @@ -29,15 +30,15 @@ class TextAreaFooter(Horizontal):
dock: bottom;
height: 1;
width: 1fr;

&:focus-within {
background: $primary 55%;
}

&:disabled {
background: transparent;
}

& Select {
width: 8;
margin-left: 1;
Expand All @@ -48,7 +49,7 @@ class TextAreaFooter(Horizontal):
width: 16;
}
}

& Checkbox {
margin: 0 1;
height: 1;
Expand Down Expand Up @@ -451,15 +452,26 @@ def action_copy_to_clipboard(self) -> None:
text_to_copy = self.selected_text
if text_to_copy:
message = f"Copied {len(text_to_copy)} characters."
self.notify(message, title="Selection copied")
title = "Selection copied"
else:
text_to_copy = self.text
message = f"Copied ({len(text_to_copy)} characters)."
self.notify(message, title="Response copied")

import pyperclip
title = "Response 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:
self.notify(message, title=title)

pyperclip.copy(text_to_copy)
self.visual_mode = False

def action_cursor_top(self) -> None:
Expand Down
Loading