From 78056778a576c35016133984568a8ebd3b81a668 Mon Sep 17 00:00:00 2001 From: Gokul Date: Thu, 13 Apr 2023 17:43:52 +0400 Subject: [PATCH] std buffer manipulations (#10) * rebased * fixed stdin --- Cargo.lock | 25 +++++++++++++++++++++++-- Cargo.toml | 1 + src/lib.rs | 17 +++++++++++++++-- src/main.rs | 3 +-- 4 files changed, 40 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7665f5b..764dc17 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -68,6 +68,17 @@ dependencies = [ "x11rb", ] +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi 0.1.19", + "libc", + "winapi", +] + [[package]] name = "autocfg" version = "1.1.0" @@ -315,6 +326,15 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + [[package]] name = "hermit-abi" version = "0.3.1" @@ -342,7 +362,7 @@ version = "1.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c66c74d2ae7e79a5a8f7ac924adbe38ee42a859c6539ad869eb51f0b52dc220" dependencies = [ - "hermit-abi", + "hermit-abi 0.3.1", "libc", "windows-sys 0.48.0", ] @@ -353,7 +373,7 @@ version = "0.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "adcf93614601c8129ddf72e2d5633df827ba6551541c6d8c59520a371475be1f" dependencies = [ - "hermit-abi", + "hermit-abi 0.3.1", "io-lifetimes", "rustix", "windows-sys 0.48.0", @@ -867,5 +887,6 @@ name = "zp" version = "0.2.0" dependencies = [ "arboard", + "atty", "clap", ] diff --git a/Cargo.toml b/Cargo.toml index 463ff23..659826e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,4 +13,5 @@ exclude = ["HomebrewFormula"] [dependencies] arboard = "3.2.0" +atty = "0.2.14" clap = { version = "4.2.1", features = ["derive"] } diff --git a/src/lib.rs b/src/lib.rs index 93d3e12..427327a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,8 @@ use arboard::Clipboard; use std::fs::File; -use std::io::Read; use std::process; +use std::io::{self, Read}; +use atty::Stream; #[derive(Debug)] pub struct Query { @@ -14,7 +15,19 @@ impl Query { let source = match args.next() { Some(arg) => arg, - None => return Err("No source to copy from"), + None => { + if atty::is(Stream::Stdout) && atty::is(Stream::Stderr) && atty::isnt(Stream::Stdin) { + let mut buffer = io::stdin(); + let mut contents = String::new(); + while let Ok(n) = buffer.read_to_string(&mut contents) { + if n == 0 {break;} + } + cpy(&contents); + process::exit(1); + }else{ + return Err("No source to copy from"); + } + }, }; Ok(Query { source }) diff --git a/src/main.rs b/src/main.rs index ceddc1e..e078a1d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,8 +14,7 @@ const VERSION: &str = env!("CARGO_PKG_VERSION"); )] struct Zp { - ///Name of the file to copy from. - file_name: String, + file_name: Vec, } fn main() {