Skip to content

Add support for OSC52 clipboard codes for the REPL .copy command. #1280

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

Merged
merged 4 commits into from
May 9, 2025
Merged

Add support for OSC52 clipboard codes for the REPL .copy command. #1280

merged 4 commits into from
May 9, 2025

Conversation

gcv
Copy link
Contributor

@gcv gcv commented May 3, 2025

The .copy REPL command does not work when aichat runs on a headless server. It appears that the arboard crate only works with graphical Linux interfaces, X11 and Wayland. When these are not available, it fails. This PR adds a fallback to use an OSC52 terminal codes to access the clipboard through the terminal emulator. This works on pretty much all modern emulators. Seems to work on my testing, and seems to work correctly under X11 (I don’t have a Wayland system to test with).

Thanks for this excellent package! aichat is the best CLI/TUI LLM interface I found, and I use it every day.

}

#[cfg(any(target_os = "android", target_os = "emscripten"))]
pub fn set_text(_text: &str) -> anyhow::Result<()> {
Err(anyhow::anyhow!("No clipboard available").context("Failed to copy"))
pub fn set_text(text: &str) -> Result<()> {
Copy link
Owner

Choose a reason for hiding this comment

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

  1. This function must return anyhow::Result, otherwise it will cause a compilation error.
-pub fn set_text(text: &str) -> Result<()> {
+pub fn set_text(text: &str) -> anyhow::Result<()> {
  1. Even in Android or WASM environments, OSC52 clipboard support is unlikely. I suggest leaving the function as it is.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense. Fixed.

@sigoden
Copy link
Owner

sigoden commented May 5, 2025

@gcv Thank you for your contribution.

OSC52 clipboard code support is definitely necessary.

However, there are a few minor issues; would you have time to fix them?

@gcv
Copy link
Contributor Author

gcv commented May 5, 2025

Yes, I’ll fix those. I’m surprised I didn’t get compilation errors with that namespace problem. Edit: Probably because it’s conditionally compiled.

@gcv
Copy link
Contributor Author

gcv commented May 5, 2025

I reversed the changes to the function handling unsupported platforms. Thanks for the code review!

@sigoden
Copy link
Owner

sigoden commented May 6, 2025

@gcv
Copy link
Contributor Author

gcv commented May 7, 2025

Clippy warnings fixed in the latest commit.

@sigoden
Copy link
Owner

sigoden commented May 7, 2025

@gcv
Copy link
Contributor Author

gcv commented May 8, 2025

Formatting fixed.

@sigoden sigoden merged commit e3e6388 into sigoden:main May 9, 2025
3 checks passed
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