- Notifications
You must be signed in to change notification settings - Fork16
Cross-platform clipboard API (text | image | rich text | html | files | monitoring changes) | 跨平台剪贴板 API(文本|图片|富文本|html|文件|监听变化) Windows,MacOS,Linux
License
ChurchTao/clipboard-rs
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
clipboard-rs is a cross-platform library written in Rust for getting and setting the system-level clipboard content. It supports Linux, Windows, and MacOS.
- Plain text
- Html
- Rich text
- Image (In
PNG
format) - File (In
file-uri-list
format) - Any type (by specifying the type identifier) can be obtained through the
available_formats
method
Type | Windows | macOS | Linux(X11) | iOS(Beta) | Android(WIP) |
---|---|---|---|---|---|
Plain Text | ✅ | ✅ | ✅ | ✅ | 🚧 |
HTML | ✅ | ✅ | ✅ | ✅ | 🚧 |
RTF | ✅ | ✅ | ✅ | ✅ | 🚧 |
Image | PNG(preferred)/DIBV5 | PNG(preferred)/TIFF | PNG | PNG | 🚧 |
File List | ✅ | ✅ | ✅ | ❌ | 🚧 |
Custom Type | ✅ | ✅ | ✅ | ✅ | 🚧 |
Watch Changes | ✅ | ✅ | ✅ | ✅ | 🚧 |
- MacOS Support
- Linux Support (x11)
- Windows Support
- iOS Support (Beta)
- Android Support (🚧)
Add the following content to yourCargo.toml
:
[dependencies]clipboard-rs ="0.3.0"
use clipboard_rs::{Clipboard,ClipboardContext,ContentFormat};fnmain(){let ctx =ClipboardContext::new().unwrap();let types = ctx.available_formats().unwrap();println!("{:?}", types);let has_rtf = ctx.has(ContentFormat::Rtf);println!("has_rtf={}", has_rtf);let rtf = ctx.get_rich_text().unwrap_or("".to_string());println!("rtf={}", rtf);let has_html = ctx.has(ContentFormat::Html);println!("has_html={}", has_html);let html = ctx.get_html().unwrap_or("".to_string());println!("html={}", html);let content = ctx.get_text().unwrap_or("".to_string());println!("txt={}", content);}
use clipboard_rs::{common::RustImage,Clipboard,ClipboardContext};constTMP_PATH:&str ="/tmp/";fnmain(){let ctx =ClipboardContext::new().unwrap();let types = ctx.available_formats().unwrap();println!("{:?}", types);let img = ctx.get_image();match img{Ok(img) =>{img.save_to_path(format!("{}test.png",TMP_PATH).as_str()).unwrap();let resize_img = img.thumbnail(300,300).unwrap();resize_img.save_to_path(format!("{}test_thumbnail.png",TMP_PATH).as_str()).unwrap();}Err(err) =>{println!("err={}", err);}}}
use clipboard_rs::{Clipboard,ClipboardContext};fnmain(){let ctx =ClipboardContext::new().unwrap();let types = ctx.available_formats().unwrap();println!("{:?}", types);let buffer = ctx.get_buffer("public.html").unwrap();let string =String::from_utf8(buffer).unwrap();println!("{}", string);}
use clipboard_rs::{Clipboard,ClipboardContext,ClipboardHandler,ClipboardWatcher,ClipboardWatcherContext,};use std::{thread, time::Duration};structManager{ctx:ClipboardContext,}implManager{pubfnnew() ->Self{let ctx =ClipboardContext::new().unwrap();Manager{ ctx}}}implClipboardHandlerforManager{fnon_clipboard_change(&mutself){println!("on_clipboard_change, txt = {}",self.ctx.get_text().unwrap());}}fnmain(){let manager =Manager::new();letmut watcher =ClipboardWatcherContext::new().unwrap();let watcher_shutdown = watcher.add_handler(manager).get_shutdown_channel();thread::spawn(move ||{thread::sleep(Duration::from_secs(5));println!("stop watch!");watcher_shutdown.stop();});println!("start watch!");watcher.start_watch();}
By default, in X11 clipboard-rs implements a read timeout of 500 ms. You can override or disable this timeout by creatingClipboardContext usingnew_with_options
:
#[cfg(unix)]fnsetup_clipboard() ->ClipboardContext{ClipboardContext::new_with_options(ClipboardContextX11Options{read_timeout:None}).unwrap()}#[cfg(not(unix))]fnsetup_clipboard(ctx:&mutClipboardContext) ->ClipboardContext{ClipboardContext::new().unwrap()}
You are welcome to submit PRs and issues and contribute your code or ideas to the project. Due to my limited level, the library may also have bugs. You are welcome to point them out and I will modify them as soon as possible.
- API design is inspired byelectron
- Linux part of the project code is referenced fromx11-clipboard
if you have any questions, you can contact me by email:swkzymlyy@gmail.com
Chinese users can also contact me by wechatNo:uniq_idx_church_lynn
This project is licensed under the MIT License. See theLICENSE file for details.
About
Cross-platform clipboard API (text | image | rich text | html | files | monitoring changes) | 跨平台剪贴板 API(文本|图片|富文本|html|文件|监听变化) Windows,MacOS,Linux
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Contributors6
Uh oh!
There was an error while loading.Please reload this page.