terminal-clipboard-0.4.1/.cargo_vcs_info.json0000644000000001360000000000100146060ustar { "git": { "sha1": "f00647c1189c44a2510818ca31108a143b49c173" }, "path_in_vcs": "" }terminal-clipboard-0.4.1/.github/workflows/deny.yml000064400000000000000000000013261046102023000204570ustar 00000000000000on: [push, pull_request] name: cargo-deny jobs: deny: name: deny runs-on: ubuntu-latest strategy: matrix: checks: - advisories - bans licenses sources # Prevent sudden announcement of a new advisory from failing ci: continue-on-error: ${{ matrix.checks == 'advisories' }} steps: - name: Checkout sources uses: actions/checkout@v4.1.1 - name: Install toolchain uses: dtolnay/rust-toolchain@master with: toolchain: 1.73.0 - name: Cache uses: Swatinem/rust-cache@v2 - name: cargo-deny uses: EmbarkStudios/cargo-deny-action@v1 with: command: check ${{ matrix.checks }} terminal-clipboard-0.4.1/.gitignore000064400000000000000000000000701046102023000153630ustar 00000000000000/build /target Cargo.lock /compile-example.sh deploy.sh terminal-clipboard-0.4.1/Cargo.lock0000644000000133140000000000100125630ustar # This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 3 [[package]] name = "autocfg" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" [[package]] name = "bitflags" version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "block" version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0d8c1fef690941d3e7788d328517591fecc684c084084702d6ff1641e993699a" [[package]] name = "cfg-if" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "clipboard-win" version = "4.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5123c6b97286809fea9e38d2c9bf530edbcb9fc0d8f8272c28b0c95f067fa92d" dependencies = [ "error-code", "str-buf", "winapi", ] [[package]] name = "clipboard_macos" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "145a7f9e9b89453bc0a5e32d166456405d389cea5b578f57f1274b1397588a95" dependencies = [ "objc", "objc-foundation", "objc_id", ] [[package]] name = "error-code" version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "56551f6cdadfbb3279fbb8d17693591e49c599dacf22b40088ef321571ae53ee" dependencies = [ "libc", "str-buf", ] [[package]] name = "gethostname" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bb65d4ba3173c56a500b555b532f72c42e8d1fe64962b518897f8959fae2c177" dependencies = [ "libc", "winapi", ] [[package]] name = "libc" version = "0.2.149" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a08173bc88b7955d1b3145aa561539096c421ac8debde8cbc3612ec635fee29b" [[package]] name = "malloc_buf" version = "0.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "62bb907fe88d54d8d9ce32a3cceab4218ed2f6b7d35617cafe9adf84e43919cb" dependencies = [ "libc", ] [[package]] name = "memoffset" version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5de893c32cde5f383baa4c04c5d6dbdd735cfd4a794b0debdb2bb1b421da5ff4" dependencies = [ "autocfg", ] [[package]] name = "nix" version = "0.26.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "598beaf3cc6fdd9a5dfb1630c2800c7acd31df7aaf0f565796fba2b53ca1af1b" dependencies = [ "bitflags", "cfg-if", "libc", "memoffset", ] [[package]] name = "objc" version = "0.2.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "915b1b472bc21c53464d6c8461c9d3af805ba1ef837e1cac254428f4a77177b1" dependencies = [ "malloc_buf", ] [[package]] name = "objc-foundation" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1add1b659e36c9607c7aab864a76c7a4c2760cd0cd2e120f3fb8b952c7e22bf9" dependencies = [ "block", "objc", "objc_id", ] [[package]] name = "objc_id" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c92d4ddb4bd7b50d730c215ff871754d0da6b2178849f8a2a2ab69712d0c073b" dependencies = [ "objc", ] [[package]] name = "once_cell" version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "692fcb63b64b1758029e0a96ee63e049ce8c5948587f2f7208df04625e5f6b56" [[package]] name = "str-buf" version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cec4d70d0ed9214ae8d642a13bafe4fdfe45a4c611d27a9fb4d4c1a3b02a12e3" [[package]] name = "terminal-clipboard" version = "0.4.1" dependencies = [ "clipboard-win", "clipboard_macos", "once_cell", "termux-clipboard", "x11-clipboard", ] [[package]] name = "termux-clipboard" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9f6aff13ca3293315b94f6dbd9c69e1c958fe421c294681e2ffda80c9858e36f" [[package]] name = "winapi" version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" dependencies = [ "winapi-i686-pc-windows-gnu", "winapi-x86_64-pc-windows-gnu", ] [[package]] name = "winapi-i686-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" [[package]] name = "winapi-wsapoll" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "44c17110f57155602a80dca10be03852116403c9ff3cd25b079d666f2aa3df6e" dependencies = [ "winapi", ] [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" [[package]] name = "x11-clipboard" version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b41aca1115b1f195f21c541c5efb423470848d48143127d0f07f8b90c27440df" dependencies = [ "x11rb", ] [[package]] name = "x11rb" version = "0.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b1641b26d4dec61337c35a1b1aaf9e3cba8f46f0b43636c609ab0291a648040a" dependencies = [ "gethostname", "nix", "winapi", "winapi-wsapoll", "x11rb-protocol", ] [[package]] name = "x11rb-protocol" version = "0.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "82d6c3f9a0fb6701fab8f6cea9b0c0bd5d6876f1f89f7fada07e558077c344bc" dependencies = [ "nix", ] terminal-clipboard-0.4.1/Cargo.toml0000644000000023320000000000100126040ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2021" name = "terminal-clipboard" version = "0.4.1" authors = ["Denys Seguret "] description = "a minimal cross-platform clipboard" readme = "README.md" keywords = [ "clipboard", "terminal", ] license = "MIT" repository = "https://github.com/Canop/terminal-clipboard" [dependencies.once_cell] version = "1.8" [features] default = [] [target."cfg(not(any(target_os=\"windows\",target_os=\"android\",target_os=\"macos\")))".dependencies.x11-clipboard] version = "0.8.1" [target."cfg(target_os=\"android\")".dependencies.termux-clipboard] version = "0.1" [target."cfg(target_os=\"macos\")".dependencies.clipboard_macos] version = "0.1" [target."cfg(target_os=\"windows\")".dependencies.clipboard-win] version = "4.0.3" terminal-clipboard-0.4.1/Cargo.toml.orig000064400000000000000000000013101046102023000162600ustar 00000000000000[package] name = "terminal-clipboard" version = "0.4.1" authors = ["Denys Seguret "] description = "a minimal cross-platform clipboard" repository = "https://github.com/Canop/terminal-clipboard" keywords = ["clipboard", "terminal"] readme = "README.md" edition = "2021" license = "MIT" [dependencies] once_cell = "1.8" [features] default = [] [target.'cfg(target_os="android")'.dependencies] termux-clipboard = "0.1" [target.'cfg(target_os="windows")'.dependencies] clipboard-win = "4.0.3" [target.'cfg(target_os="macos")'.dependencies] clipboard_macos = "0.1" [target.'cfg(not(any(target_os="windows",target_os="android",target_os="macos")))'.dependencies] x11-clipboard = "0.8.1" terminal-clipboard-0.4.1/README.md000064400000000000000000000047521046102023000146650ustar 00000000000000# terminal-clipboard [![CI][s3]][l3] [![MIT][s2]][l2] [![Latest Version][s1]][l1] [![Chat on Miaou][s3]][l3] [s1]: https://img.shields.io/crates/v/terminal-clipboard.svg [l1]: https://crates.io/crates/terminal-clipboard [s2]: https://img.shields.io/badge/license-MIT-blue.svg [l2]: LICENSE [s3]: https://miaou.dystroy.org/static/shields/room.svg [l3]: https://miaou.dystroy.org/3490?terminal-clipboard **terminal-clipboard** is a cross-platform clipboard library focused on strings copying and pasting for terminal applications: * it's tested on linux, windows and Android (Termux) * it doesn't handle other types of objects than strings * it doesn't handle non UTF8 strings # Usage ``` terminal_clipboard::set_string("test").unwrap(); assert_eq!("test", terminal_clipboard::get_string().unwrap()); ``` # Supported platforms The implementation is currently chosen from the "target_os" part of the compilation target. ## Android (Termux) The current implementation will defer to Termux API facilities to access the Android clipboard, and won't work if the Termux API isn't available at runtime. If you know of solutions to access the Android clipboard without Termux, please open an issue. ## Linux If a unix-like target is detected and the "termux" feature isn't enabled, terminal-clipboard uses the [x11-clipboard](https://crates.io/crates/x11-clipboard) crate. You'll need to have `libxcb` to compile. On Debian and Ubuntu you can install them with ``` sudo apt install libxcb1-dev libxcb-render0-dev libxcb-shape0-dev libxcb-xfixes0-dev -y ``` Fedora, Centos, Red Hat ``` sudo dnf install libxcb -y ``` openSUSE ``` sudo zypper --non-interactive install xorg-x11-util-devel libxcb-composite0 libxcb-render0 libxcb-shape0 libxcb-xfixes0 ``` Arch Linux ``` sudo pacman -Syu --noconfirm libxcb ``` Alpine is not supported. For alpine you have to use [`musl`](https://wiki.musl-libc.org/functional-differences-from-glibc.html) instead of `gnu` and have to provide alternative behaviour. ``` #[cfg(not(target_env = "musl"))] { terminal_clipboard::set_string(answer_text).unwrap(); assert_eq!(*answer_text, terminal_clipboard::get_string().unwrap()); println!("Text '{answer_text}' was copied to your clipboard") } #[cfg(target_env = "musl")] { println!("{}", answer_text); } ``` ## Windows If the compilation target is "windows", terminal-clipboard uses the [clipboard-win](https://crates.io/crates/clipboard-win) crate. If you're only interested in this platform, you should use this crate directly. terminal-clipboard-0.4.1/bacon.toml000064400000000000000000000015711046102023000153610ustar 00000000000000# This is a configuration file for the bacon tool # More info at https://github.com/Canop/bacon default_job = "check" [jobs] [jobs.check] command = ["cargo", "check", "--color", "always"] need_stdout = false [jobs.check-win] command = ["cross", "build", "--target", "x86_64-pc-windows-gnu", "--color", "always"] need_stdout = false [jobs.check-android] command = ["cross", "build", "--target", "aarch64-linux-android", "--color", "always"] need_stdout = false [jobs.check-all] command = ["cargo", "check", "--all-targets", "--color", "always"] need_stdout = false watch = ["tests", "benches", "examples"] [jobs.clippy] command = ["cargo", "clippy", "--color", "always"] need_stdout = false [jobs.test] command = ["cargo", "test", "--color", "always"] need_stdout = true watch = ["tests"] [jobs.doc] command = ["cargo", "doc", "--color", "always", "--no-deps"] need_stdout = false terminal-clipboard-0.4.1/deny.toml000064400000000000000000000045241046102023000152370ustar 00000000000000[licenses] # The lint level for crates which do not have a detectable license unlicensed = "deny" # List of explicitly allowed licenses # See https://spdx.org/licenses/ for list of possible licenses # [possible values: any SPDX 3.7 short identifier (+ optional exception)]. allow = [] # List of explicitly disallowed licenses # See https://spdx.org/licenses/ for list of possible licenses # [possible values: any SPDX 3.7 short identifier (+ optional exception)]. deny = [] # The lint level for licenses considered copyleft copyleft = "deny" # Blanket approval or denial for OSI-approved or FSF Free/Libre licenses # * both - The license will only be approved if it is both OSI-approved *AND* FSF/Free # * either - The license will be approved if it is either OSI-approved *OR* FSF/Free # * osi-only - The license will be approved if is OSI-approved *AND NOT* FSF/Free # * fsf-only - The license will be approved if is FSF/Free *AND NOT* OSI-approved # * neither - The license will be denied if is FSF/Free *OR* OSI-approved allow-osi-fsf-free = "either" # The confidence threshold for detecting a license from license text. # The higher the value, the more closely the license text must be to the # canonical license text of a valid SPDX license file. # [possible values: any between 0.0 and 1.0]. confidence-threshold = 0.8 exceptions = [ ] [bans] # Lint level for when multiple versions of the same crate are detected multiple-versions = "warn" # The graph highlighting used when creating dotgraphs for crates # with multiple versions # * lowest-version - The path to the lowest versioned duplicate is highlighted # * simplest-path - The path to the version with the fewest edges is highlighted # * all - Both lowest-version and simplest-path are used highlight = "all" # List of crates that are allowed. Use with care! allow = [ ] # List of crates to deny deny = [ # Each entry the name of a crate and a version range. If version is # not specified, all versions will be matched. ] # Certain crates/versions that will be skipped when doing duplicate detection. skip = [ ] # Similarly to `skip` allows you to skip certain crates during duplicate detection, # unlike skip, it also includes the entire tree of transitive dependencies starting at # the specified crate, up to a certain depth, which is by default infinite skip-tree = [ ] [advisories] ignore = [ ] terminal-clipboard-0.4.1/examples/write_read.rs000064400000000000000000000007671046102023000177210ustar 00000000000000//! run with //! cargo run --example write_read fn main() { println!("Clipboard backend type: {:?}", terminal_clipboard::get_type()); println!( "Initial content of the clipboard: {:?}", terminal_clipboard::get_string().unwrap(), ); let s = "TEST"; println!("Writing {:?} in the clipboard", s); terminal_clipboard::set_string(s).unwrap(); println!( "New content of the clipboard: {:?}", terminal_clipboard::get_string().unwrap(), ); } terminal-clipboard-0.4.1/src/clipboard.rs000064400000000000000000000003611046102023000164720ustar 00000000000000use { crate::errors::ClipboardError, }; pub trait Clipboard { fn get_type(&self) -> &'static str; fn get_string(&self) -> Result; fn set_string(&mut self, s: &str) -> Result<(), ClipboardError>; } terminal-clipboard-0.4.1/src/errors.rs000064400000000000000000000012651046102023000160530ustar 00000000000000use std::{ fmt, string::FromUtf8Error, }; #[derive(Debug, Clone)] pub struct ClipboardError { message: String, } impl From for ClipboardError { fn from(message: String) -> Self { Self { message } } } impl From<&str> for ClipboardError { fn from(message: &str) -> Self { Self { message: message.to_owned() } } } impl fmt::Display for ClipboardError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "clipboard error: {}", self.message) } } impl From for ClipboardError { fn from(_: FromUtf8Error) -> Self { ClipboardError::from("error interpreting as UTF8".to_string()) } } terminal-clipboard-0.4.1/src/lib.rs000064400000000000000000000110711046102023000153010ustar 00000000000000/*! **terminal-clipboard** is a cross-platform clipboard library focused on strings copying and pasting for terminal applications: * it's tested on linux, windows and Android (Termux) * it doesn't handle other types of objects than strings * it doesn't handle non UTF8 strings # Usage ``` terminal_clipboard::set_string("test").unwrap(); assert_eq!("test", terminal_clipboard::get_string().unwrap()); ``` # Supported platforms The implementation is currently chosen from the "target_os" part of the compilation target. ## Android (Termux) The current implementation will defer to Termux API facilities to access the Android clipboard, and won't work if the Termux API isn't available at runtime. If you know of solutions to access the Android clipboard without Termux, please open an issue. ## Linux If a unix-like target is detected and the "termux" feature isn't enabled, terminal-clipboard uses the [x11-clipboard](https://crates.io/crates/x11-clipboard) crate. You'll need to have `xorg-dev` and `libxcb-composite0-dev` to compile. On Debian and Ubuntu you can install them with ```bash sudo apt install xorg-dev libxcb-composite0-dev ``` ## Windows If the compilation target is "windows", terminal-clipboard uses the [clipboard-win](https://crates.io/crates/clipboard-win) crate. If you're only interested in this platform, you should use this crate directly. ``` use terminal_clipboard; terminal_clipboard::set_string("test").unwrap(); assert_eq!("test", terminal_clipboard::get_string().unwrap()); ``` */ mod clipboard; mod errors; mod local; pub use { clipboard::Clipboard, errors::ClipboardError, local::LocalClipboard, }; #[cfg(target_os = "macos")] mod macos; #[cfg(target_os = "macos")] pub use macos::MacClipboard; #[cfg(target_os = "windows")] mod win; #[cfg(target_os = "windows")] pub use win::WinClipboard; #[cfg(target_os = "android")] mod termux; #[cfg(target_os = "android")] pub use termux::TermuxClipboard; #[cfg(not(any(target_os="windows",target_os="android",target_os="macos")))] mod x11; #[cfg(not(any(target_os="windows",target_os="android",target_os="macos")))] pub use x11::X11Clipboard; use { once_cell::sync::Lazy, std::sync::Mutex, }; static CLIPBOARD: Lazy>> = Lazy::new(|| Mutex::new(new_clipboard())); /// Build a new clipboard. /// /// It's recommended to not use this directly but the get_string /// and set_string global functions instead, as those functions /// ensure the lazy static initialization of the clipboard. pub fn new_clipboard() -> Box { #[cfg(target_os = "windows")] { // To my knowledge return Box::new(WinClipboard::new()); } #[cfg(target_os = "macos")] { // only use MacClipboard after it is verified. if let Ok(clipboard) = MacClipboard::verified() { return Box::new(clipboard); } } #[cfg(target_os = "android")] { // we'll use the Termux clipboard, but only after having // checked it works. It fails for example when the // Termux API isn't available on the device if let Ok(clipboard) =TermuxClipboard::verified() { return Box::new(clipboard); } } #[cfg(not(any(target_os="windows",target_os="android",target_os="macos")))] { // we'll use the X11 clipboard, but only after having // checked it works. As nobody understants X11 anyway, // I won't try to pretend I know when it works and when // it doesn't if let Ok(clipboard) = X11Clipboard::verified() { return Box::new(clipboard); } } // when everything else failed, use a local clipboard #[allow(unreachable_code)] Box::new(LocalClipboard::new()) } /// Return the type of the Clipboard, for example /// "X11", "Windows", "Local", or "Termux" pub fn get_type() -> &'static str { CLIPBOARD.lock().unwrap().get_type() } /// Return the content of the clipboard pub fn get_string() -> Result { CLIPBOARD.lock().unwrap().get_string() } /// Fill the clipboard with the given string pub fn set_string>(s: S) -> Result<(), ClipboardError> { CLIPBOARD.lock().unwrap().set_string(s.as_ref()) } // Those tests are the same than doc tests but they must be // kept separate because cargo-cross doesn't run doc tests // (see https://github.com/rust-embedded/cross/issues/225) #[cfg(test)] mod clipboard_tests { use super::*; #[test] fn write_read() { let test = "TEST"; set_string(test).unwrap(); assert_eq!(test, get_string().unwrap()); } } terminal-clipboard-0.4.1/src/local.rs000064400000000000000000000020241046102023000156230ustar 00000000000000use { crate::{ Clipboard, errors::ClipboardError, }, }; /// A clipboard with no access to outside the application. #[derive(Default)] pub struct LocalClipboard { content: String, } impl LocalClipboard { pub fn new() -> LocalClipboard { Self { content: String::new(), } } } impl Clipboard for LocalClipboard { fn get_type(&self) -> &'static str { "Local" } fn get_string(&self) -> Result { let mut copy = String::with_capacity(self.content.len()); copy.push_str(&self.content); Ok(copy) } fn set_string(&mut self, s: &str) -> Result<(), ClipboardError> { self.content.clear(); self.content.push_str(s); Ok(()) } } #[test] fn test_local_clipboard() { let mut clipboard = LocalClipboard::new(); assert_eq!(clipboard.get_type(), "Local"); clipboard.set_string("test local").unwrap(); assert_eq!(clipboard.get_string().unwrap(), "test local".to_string()); } terminal-clipboard-0.4.1/src/macos.rs000064400000000000000000000026021046102023000156350ustar 00000000000000use crate::{ errors::ClipboardError, Clipboard, }; pub struct MacClipboard { backend: clipboard_macos::Clipboard, } impl MacClipboard { pub fn new() -> Result { clipboard_macos::Clipboard::new() .map(|backend| Self { backend }) .map_err(|e| ClipboardError::from(format!("macOS clipboard error : {}", e))) } pub fn verified() -> Result { let mut clipboard = Self::new()?; let previous = clipboard.get_string()?; // saving the old value let test = "test macOS"; clipboard.set_string(test)?; let res = clipboard.get_string()?; clipboard.set_string(&previous)?; if res == test.to_string() { Ok(clipboard) } else { Err(ClipboardError::from("non compliand round trip")) } } } impl Clipboard for MacClipboard { fn get_type(&self) -> &'static str { "macOS" } fn get_string(&self) -> Result { self.backend .read() .map_err(|e| ClipboardError::from(format!("macOS clipboard error : {}", e,))) } fn set_string(&mut self, s: &str) -> Result<(), ClipboardError> { self.backend .write(s.to_string()) .map_err(|e| ClipboardError::from(format!("macOS clipboard error : {}", e,))) } } terminal-clipboard-0.4.1/src/termux.rs000064400000000000000000000021621046102023000160600ustar 00000000000000use { crate::{ Clipboard, errors::ClipboardError, }, termux_clipboard::TermuxClipboardError, }; impl From for ClipboardError { fn from(termux_error: TermuxClipboardError) -> Self { ClipboardError::from(termux_error.to_string()) } } pub struct TermuxClipboard {} impl TermuxClipboard { pub fn new() -> TermuxClipboard { Self {} } pub fn verified() -> Result { let mut clipboard = Self {}; let test = "test Termux"; clipboard.set_string(test)?; let res = clipboard.get_string()?; if res == test.to_string() { Ok(clipboard) } else { Err(ClipboardError::from("non compliand round trip")) } } } impl Clipboard for TermuxClipboard { fn get_type(&self) -> &'static str { "Termux" } fn get_string(&self) -> Result { Ok(termux_clipboard::get_string()?) } fn set_string(&mut self, s: &str) -> Result<(), ClipboardError> { Ok(termux_clipboard::set_string(s)?) } } terminal-clipboard-0.4.1/src/win.rs000064400000000000000000000013121046102023000153250ustar 00000000000000use { crate::{ Clipboard, errors::ClipboardError, }, }; pub struct WinClipboard {} impl WinClipboard { pub fn new() -> WinClipboard { Self {} } } impl Clipboard for WinClipboard { fn get_type(&self) -> &'static str { "Windows" } fn get_string(&self) -> Result { clipboard_win::get_clipboard_string() .map_err(|e| ClipboardError::from(format!("Windows clipboard error : {}", e,))) } fn set_string(&mut self, s: &str) -> Result<(), ClipboardError> { clipboard_win::set_clipboard_string(s) .map_err(|e| ClipboardError::from(format!("Windows clipboard error : {}", e,))) } } terminal-clipboard-0.4.1/src/x11.rs000064400000000000000000000034601046102023000151470ustar 00000000000000use { crate::{ Clipboard, errors::ClipboardError, }, std::time::Duration, }; impl From for ClipboardError { fn from(x11_error: x11_clipboard::error::Error) -> Self { ClipboardError::from(format!("X11 clipboard error : {}", x11_error)) } } pub struct X11Clipboard { backend: x11_clipboard::Clipboard, } impl X11Clipboard { pub fn new() -> Result { let backend = x11_clipboard::Clipboard::new()?; Ok(Self { backend }) } /// return a X11 clipboard but only if it has been verified to /// be correctly working pub fn verified() -> Result { let backend = x11_clipboard::Clipboard::new()?; let mut clipboard = Self { backend }; let previous = clipboard.get_string()?; // saving the old value let test = "test X11"; clipboard.set_string(test)?; let res = clipboard.get_string()?; clipboard.set_string(&previous)?; if res == *test { Ok(clipboard) } else { Err(ClipboardError::from("non compliand round trip")) } } } impl Clipboard for X11Clipboard { fn get_type(&self) -> &'static str { "X11" } fn get_string(&self) -> Result { Ok(String::from_utf8(self.backend.load( self.backend.getter.atoms.clipboard, self.backend.getter.atoms.utf8_string, self.backend.getter.atoms.property, Duration::from_secs(2), )?)?) } fn set_string(&mut self, s: &str) -> Result<(), ClipboardError> { Ok(self.backend.store( self.backend.setter.atoms.clipboard, self.backend.setter.atoms.utf8_string, s, )?) } }