diff --git a/.rustfmt.toml b/.rustfmt.toml new file mode 100644 index 0000000..218e203 --- /dev/null +++ b/.rustfmt.toml @@ -0,0 +1 @@ +hard_tabs = true diff --git a/Cargo.lock b/Cargo.lock index 2c1c95e..fe2b1c3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,6 +8,12 @@ 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 = "bitflags" version = "2.4.2" @@ -24,41 +30,9 @@ checksum = "0d8c1fef690941d3e7788d328517591fecc684c084084702d6ff1641e993699a" name = "bp" version = "0.1.4" dependencies = [ - "copypasta", -] - -[[package]] -name = "calloop" -version = "0.12.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fba7adb4dd5aa98e5553510223000e7148f621165ec5f9acd7113f6ca4995298" -dependencies = [ - "bitflags", - "log", - "polling", - "rustix", - "slab", - "thiserror", -] - -[[package]] -name = "calloop-wayland-source" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0f0ea9b9476c7fad82841a8dbb380e2eae480c21910feba80725b46931ed8f02" -dependencies = [ - "calloop", - "rustix", - "wayland-backend", - "wayland-client", + "copypasta-ext", ] -[[package]] -name = "cc" -version = "1.0.86" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f9fa1897e4325be0d68d48df6aa1a71ac2ed4d27723887e7754192705350730" - [[package]] name = "cfg-if" version = "1.0.0" @@ -75,20 +49,11 @@ dependencies = [ "winapi", ] -[[package]] -name = "concurrent-queue" -version = "2.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d16048cd947b08fa32c24458a22f5dc5e835264f689f4f5653210c69fd107363" -dependencies = [ - "crossbeam-utils", -] - [[package]] name = "copypasta" -version = "0.10.1" +version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "deb85422867ca93da58b7f95fb5c0c10f6183ed6e1ef8841568968a896d3a858" +checksum = "133fc8675ee3a4ec9aa513584deda9aa0faeda3586b87f7f0f2ba082c66fb172" dependencies = [ "clipboard-win", "objc", @@ -99,16 +64,16 @@ dependencies = [ ] [[package]] -name = "crossbeam-utils" -version = "0.8.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "248e3bacc7dc6baa3b21e405ee045c3047101a49145e7e9eca583ab4c2ca5345" - -[[package]] -name = "cursor-icon" -version = "1.1.0" +name = "copypasta-ext" +version = "0.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "96a6ac251f4a2aca6b3f91340350eab87ae57c3f127ffeb585e92bd336717991" +checksum = "9455f470ea0c7d50c3fe3d22389c3a482f38a9f5fbab1c8ee368121356c56718" +dependencies = [ + "copypasta", + "libc", + "which", + "x11-clipboard", +] [[package]] name = "dlib" @@ -125,6 +90,12 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ea835d29036a4087793836fa931b08837ad5e957da9e23886b29586fb9b6650" +[[package]] +name = "either" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11157ac094ffbdde99aa67b23417ebdd801842852b500e395a45a9c0aac03e4a" + [[package]] name = "errno" version = "0.3.8" @@ -137,12 +108,21 @@ dependencies = [ [[package]] name = "gethostname" -version = "0.4.3" +version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0176e0459c2e4a1fe232f984bca6890e681076abb9934f6cea7c326f3fc47818" +checksum = "c1ebd34e35c46e00bb73e81363248d627782724609fe1b6396f553f68fe3862e" dependencies = [ "libc", - "windows-targets 0.48.5", + "winapi", +] + +[[package]] +name = "home" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3d1354bf6b7235cb4a0576c2619fd4ed18183f689b12b006a0ee7329eeff9a5" +dependencies = [ + "windows-sys 0.52.0", ] [[package]] @@ -151,6 +131,12 @@ version = "5.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "10257499f089cd156ad82d0a9cd57d9501fa2c989068992a97eb3c27836f206b" +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + [[package]] name = "libc" version = "0.2.153" @@ -189,18 +175,33 @@ dependencies = [ ] [[package]] -name = "memchr" -version = "2.7.1" +name = "memmap2" +version = "0.5.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83faa42c0a078c393f6b29d5db232d8be22776a891f8f56e5284faee4a20b327" +dependencies = [ + "libc", +] + +[[package]] +name = "memoffset" +version = "0.6.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149" +checksum = "5aa361d4faea93603064a027415f07bd8e1d5c88c9fbf68bf56a285428fd79ce" +dependencies = [ + "autocfg", +] [[package]] -name = "memmap2" -version = "0.9.4" +name = "nix" +version = "0.24.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fe751422e4a8caa417e13c3ea66452215d7d63e19e604f4980461212f3ae1322" +checksum = "fa52e972a9a719cecb6864fb88568781eb706bac2cd1d4f04a648542dbf78069" dependencies = [ + "bitflags 1.3.2", + "cfg-if", "libc", + "memoffset", ] [[package]] @@ -238,32 +239,12 @@ version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" -[[package]] -name = "pin-project-lite" -version = "0.2.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58" - [[package]] name = "pkg-config" version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec" -[[package]] -name = "polling" -version = "3.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "24f040dee2588b4963afb4e420540439d126f73fdacf4a9c486a96d840bac3c9" -dependencies = [ - "cfg-if", - "concurrent-queue", - "pin-project-lite", - "rustix", - "tracing", - "windows-sys 0.52.0", -] - [[package]] name = "proc-macro2" version = "1.0.78" @@ -273,15 +254,6 @@ dependencies = [ "unicode-ident", ] -[[package]] -name = "quick-xml" -version = "0.31.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1004a344b30a54e2ee58d66a71b32d2db2feb0a31f9a2d302bf0536f15de2a33" -dependencies = [ - "memchr", -] - [[package]] name = "quote" version = "1.0.35" @@ -297,7 +269,7 @@ version = "0.38.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6ea3e1a662af26cd7a3ba09c0297a31af215563ecf42817c98df621387f4e949" dependencies = [ - "bitflags", + "bitflags 2.4.2", "errno", "libc", "linux-raw-sys", @@ -310,15 +282,6 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294" -[[package]] -name = "slab" -version = "0.4.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67" -dependencies = [ - "autocfg", -] - [[package]] name = "smallvec" version = "1.13.1" @@ -327,87 +290,32 @@ checksum = "e6ecd384b10a64542d77071bd64bd7b231f4ed5940fba55e98c3de13824cf3d7" [[package]] name = "smithay-client-toolkit" -version = "0.18.1" +version = "0.16.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "922fd3eeab3bd820d76537ce8f582b1cf951eceb5475c28500c7457d9d17f53a" +checksum = "870427e30b8f2cbe64bf43ec4b86e88fe39b0a84b3f15efd9c9c2d020bc86eb9" dependencies = [ - "bitflags", - "calloop", - "calloop-wayland-source", - "cursor-icon", - "libc", + "bitflags 1.3.2", + "dlib", + "lazy_static", "log", "memmap2", - "rustix", - "thiserror", - "wayland-backend", + "nix", + "pkg-config", "wayland-client", - "wayland-csd-frame", "wayland-cursor", "wayland-protocols", - "wayland-protocols-wlr", - "wayland-scanner", - "xkeysym", ] [[package]] name = "smithay-clipboard" -version = "0.7.1" +version = "0.6.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c091e7354ea8059d6ad99eace06dd13ddeedbb0ac72d40a9a6e7ff790525882d" +checksum = "0a345c870a1fae0b1b779085e81b51e614767c239e93503588e54c5b17f4b0e8" dependencies = [ - "libc", "smithay-client-toolkit", - "wayland-backend", -] - -[[package]] -name = "syn" -version = "2.0.50" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "74f1bdc9872430ce9b75da68329d1c1746faf50ffac5f19e02b71e37ff881ffb" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - -[[package]] -name = "thiserror" -version = "1.0.57" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e45bcbe8ed29775f228095caf2cd67af7a4ccf756ebff23a306bf3e8b47b24b" -dependencies = [ - "thiserror-impl", -] - -[[package]] -name = "thiserror-impl" -version = "1.0.57" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a953cb265bef375dae3de6663da4d3804eee9682ea80d8e2542529b73c531c81" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "tracing" -version = "0.1.40" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" -dependencies = [ - "pin-project-lite", - "tracing-core", + "wayland-client", ] -[[package]] -name = "tracing-core" -version = "0.1.32" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" - [[package]] name = "unicode-ident" version = "1.0.12" @@ -415,101 +323,90 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" [[package]] -name = "wayland-backend" -version = "0.3.3" +name = "wayland-client" +version = "0.29.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d50fa61ce90d76474c87f5fc002828d81b32677340112b4ef08079a9d459a40" +checksum = "3f3b068c05a039c9f755f881dc50f01732214f5685e379829759088967c46715" dependencies = [ - "cc", + "bitflags 1.3.2", "downcast-rs", - "rustix", + "libc", + "nix", "scoped-tls", - "smallvec", - "wayland-sys", -] - -[[package]] -name = "wayland-client" -version = "0.31.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "82fb96ee935c2cea6668ccb470fb7771f6215d1691746c2d896b447a00ad3f1f" -dependencies = [ - "bitflags", - "rustix", - "wayland-backend", + "wayland-commons", "wayland-scanner", + "wayland-sys", ] [[package]] -name = "wayland-csd-frame" -version = "0.3.0" +name = "wayland-commons" +version = "0.29.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "625c5029dbd43d25e6aa9615e88b829a5cad13b2819c4ae129fdbb7c31ab4c7e" +checksum = "8691f134d584a33a6606d9d717b95c4fa20065605f798a3f350d78dced02a902" dependencies = [ - "bitflags", - "cursor-icon", - "wayland-backend", + "nix", + "once_cell", + "smallvec", + "wayland-sys", ] [[package]] name = "wayland-cursor" -version = "0.31.1" +version = "0.29.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "71ce5fa868dd13d11a0d04c5e2e65726d0897be8de247c0c5a65886e283231ba" +checksum = "6865c6b66f13d6257bef1cd40cbfe8ef2f150fb8ebbdb1e8e873455931377661" dependencies = [ - "rustix", + "nix", "wayland-client", "xcursor", ] [[package]] name = "wayland-protocols" -version = "0.31.2" +version = "0.29.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f81f365b8b4a97f422ac0e8737c438024b5951734506b0e1d775c73030561f4" +checksum = "b950621f9354b322ee817a23474e479b34be96c2e909c14f7bc0100e9a970bc6" dependencies = [ - "bitflags", - "wayland-backend", + "bitflags 1.3.2", "wayland-client", - "wayland-scanner", -] - -[[package]] -name = "wayland-protocols-wlr" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad1f61b76b6c2d8742e10f9ba5c3737f6530b4c243132c2a2ccc8aa96fe25cd6" -dependencies = [ - "bitflags", - "wayland-backend", - "wayland-client", - "wayland-protocols", + "wayland-commons", "wayland-scanner", ] [[package]] name = "wayland-scanner" -version = "0.31.1" +version = "0.29.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "63b3a62929287001986fb58c789dce9b67604a397c15c611ad9f747300b6c283" +checksum = "8f4303d8fa22ab852f789e75a967f0a2cdc430a607751c0499bada3e451cbd53" dependencies = [ "proc-macro2", - "quick-xml", "quote", + "xml-rs", ] [[package]] name = "wayland-sys" -version = "0.31.1" +version = "0.29.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "15a0c8eaff5216d07f226cb7a549159267f3467b289d9a2e52fd3ef5aae2b7af" +checksum = "be12ce1a3c39ec7dba25594b97b42cb3195d54953ddb9d3d95a7c3902bc6e9d4" dependencies = [ "dlib", - "log", - "once_cell", + "lazy_static", "pkg-config", ] +[[package]] +name = "which" +version = "4.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87ba24419a2078cd2b0f2ede2691b6c66d8e47836da3b6db8265ebad47afbfc7" +dependencies = [ + "either", + "home", + "once_cell", + "rustix", +] + [[package]] name = "winapi" version = "0.3.9" @@ -526,6 +423,15 @@ 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" @@ -666,30 +572,34 @@ checksum = "0770833d60a970638e989b3fa9fd2bb1aaadcf88963d1659fd7d9990196ed2d6" [[package]] name = "x11-clipboard" -version = "0.9.1" +version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "613c2be3e772af2bbb57c5a94413675f5ec668bac00a71ada2ced28c420ef087" +checksum = "980b9aa9226c3b7de8e2adb11bf20124327c054e0e5812d2aac0b5b5a87e7464" dependencies = [ - "libc", "x11rb", ] [[package]] name = "x11rb" -version = "0.13.0" +version = "0.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8f25ead8c7e4cba123243a6367da5d3990e0d3affa708ea19dce96356bd9f1a" +checksum = "592b4883219f345e712b3209c62654ebda0bb50887f330cbd018d0f654bfd507" dependencies = [ "gethostname", - "rustix", + "nix", + "winapi", + "winapi-wsapoll", "x11rb-protocol", ] [[package]] name = "x11rb-protocol" -version = "0.13.0" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e63e71c4b8bd9ffec2c963173a4dc4cbde9ee96961d4fcb4429db9929b606c34" +checksum = "56b245751c0ac9db0e006dc812031482784e434630205a93c73cfefcaabeac67" +dependencies = [ + "nix", +] [[package]] name = "xcursor" @@ -698,7 +608,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6a0ccd7b4a5345edfcd0c3535718a4e9ff7798ffc536bb5b5a0e26ff84732911" [[package]] -name = "xkeysym" -version = "0.2.0" +name = "xml-rs" +version = "0.8.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "054a8e68b76250b253f671d1268cb7f1ae089ec35e195b2efb2a4e9a836d0621" +checksum = "0fcb9cbac069e033553e8bb871be2fbdffcab578eb25bd0f7c508cedc6dcd75a" diff --git a/Cargo.toml b/Cargo.toml index ef2eb50..bd1ae54 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,4 +12,4 @@ keywords = ["clipboard", "cli", "tool"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -copypasta = "0.10.1" +copypasta-ext = "0.4.4" diff --git a/src/clipboard.rs b/src/clipboard.rs index c690fa4..386c3f6 100644 --- a/src/clipboard.rs +++ b/src/clipboard.rs @@ -1,26 +1,27 @@ use super::Error; use std::io; -use copypasta::{ClipboardContext, ClipboardProvider}; +use copypasta_ext::prelude::*; +use copypasta_ext::x11_fork::ClipboardContext; pub fn copy(mut file: impl io::Read, strip_whitespace: bool) -> Result, Error> { - let mut ctx = ClipboardContext::new()?; - let mut contents = Vec::with_capacity(32); - file.read_to_end(&mut contents)?; + let mut ctx = ClipboardContext::new()?; + let mut contents = Vec::with_capacity(32); + file.read_to_end(&mut contents)?; - let mut contents = String::from_utf8(contents)?; - if strip_whitespace { - contents = contents.trim().to_string(); - } - ctx.set_contents(contents.clone())?; - Ok(contents.into()) + let mut contents = String::from_utf8(contents)?; + if strip_whitespace { + contents = contents.trim().to_string(); + } + ctx.set_contents(contents.clone())?; + Ok(contents.into()) } pub fn paste(strip_whitespace: bool) -> Result, Error> { - let mut ctx = ClipboardContext::new()?; - let mut contents = ctx.get_contents()?; - if strip_whitespace { - contents = contents.trim().to_string(); - } - Ok(contents.into_bytes()) + let mut ctx = ClipboardContext::new()?; + let mut contents = ctx.get_contents()?; + if strip_whitespace { + contents = contents.trim().to_string(); + } + Ok(contents.into_bytes()) } diff --git a/src/main.rs b/src/main.rs index ac03f59..7fa05fb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ use std::{ - env, error, ffi, fmt, fs, - io::{self, Write}, - process, + env, error, ffi, fmt, fs, + io::{self, Write}, + process, }; mod clipboard; @@ -9,94 +9,94 @@ mod clipboard; pub type Error = Box; fn stdin_tty() -> bool { - io::IsTerminal::is_terminal(&io::stdin()) + io::IsTerminal::is_terminal(&io::stdin()) } fn stdout_tty() -> bool { - io::IsTerminal::is_terminal(&io::stdout()) + io::IsTerminal::is_terminal(&io::stdout()) } #[derive(Debug)] struct UnknownArgumentError(ffi::OsString); impl fmt::Display for UnknownArgumentError { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "unknown argument `{}`", self.0.to_string_lossy()) - } + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "unknown argument `{}`", self.0.to_string_lossy()) + } } impl error::Error for UnknownArgumentError {} fn inner_main() -> Result<(), Error> { - let mut input_file = None; - let mut force_stdout = false; - let mut output_to_stderr = false; - let mut strip_whitespace = false; - for arg in env::args_os().skip(1) { - if arg == "-h" || arg == "--help" { - print_usage(io::stdout())?; - return Ok(()); - } else if arg == "--version" || arg == "-V" || arg == "-v" { - print_version()?; - return Ok(()); - } else if arg == "-O" || arg == "--stdout" { - force_stdout = true; - } else if arg == "-E" || arg == "--stderr" { - output_to_stderr = true; - } else if arg == "-s" || arg == "--strip" { - strip_whitespace = true; - } else if arg - .as_os_str() - .to_str() - .map_or(false, |s| s.starts_with('-')) - { - return Err(UnknownArgumentError(arg))?; - } else if input_file.is_none() { - input_file = Some(arg); - } else { - return Err(UnknownArgumentError(arg))?; - } - } - - let mut tty_paste = false; - - let contents = if let Some(input_file) = input_file { - let file = fs::File::open(input_file)?; - clipboard::copy(file, strip_whitespace)? - } else if stdin_tty() { - if !output_to_stderr { - tty_paste = true; - } - clipboard::paste(strip_whitespace)? - } else { - clipboard::copy(io::stdin(), strip_whitespace)? - }; - - if force_stdout || tty_paste || !stdout_tty() { - io::stdout().write_all(&contents)?; - if tty_paste - && !contents.ends_with(&[b'\n']) - && !contents.is_empty() - && stdout_tty() - && !strip_whitespace - { - println!(); - } - } - - if output_to_stderr { - io::stderr().write_all(&contents)?; - } - - Ok(()) + let mut input_file = None; + let mut force_stdout = false; + let mut output_to_stderr = false; + let mut strip_whitespace = false; + for arg in env::args_os().skip(1) { + if arg == "-h" || arg == "--help" { + print_usage(io::stdout())?; + return Ok(()); + } else if arg == "--version" || arg == "-V" || arg == "-v" { + print_version()?; + return Ok(()); + } else if arg == "-O" || arg == "--stdout" { + force_stdout = true; + } else if arg == "-E" || arg == "--stderr" { + output_to_stderr = true; + } else if arg == "-s" || arg == "--strip" { + strip_whitespace = true; + } else if arg + .as_os_str() + .to_str() + .map_or(false, |s| s.starts_with('-')) + { + return Err(UnknownArgumentError(arg))?; + } else if input_file.is_none() { + input_file = Some(arg); + } else { + return Err(UnknownArgumentError(arg))?; + } + } + + let mut tty_paste = false; + + let contents = if let Some(input_file) = input_file { + let file = fs::File::open(input_file)?; + clipboard::copy(file, strip_whitespace)? + } else if stdin_tty() { + if !output_to_stderr { + tty_paste = true; + } + clipboard::paste(strip_whitespace)? + } else { + clipboard::copy(io::stdin(), strip_whitespace)? + }; + + if force_stdout || tty_paste || !stdout_tty() { + io::stdout().write_all(&contents)?; + if tty_paste + && !contents.ends_with(&[b'\n']) + && !contents.is_empty() + && stdout_tty() + && !strip_whitespace + { + println!(); + } + } + + if output_to_stderr { + io::stderr().write_all(&contents)?; + } + + Ok(()) } fn get_version_number() -> &'static str { - env!("CARGO_PKG_VERSION") + env!("CARGO_PKG_VERSION") } fn print_usage(mut f: impl io::Write) -> io::Result<()> { - write!( - f, - "Usage: bp [FLAGS] [file] + write!( + f, + "Usage: bp [FLAGS] [file] Version {version} @@ -106,26 +106,26 @@ Flags: -O --stdout print clipboard contents to stdout -E --stderr print clipboard contents to stderr -s --strip strip whitespace from the content\n", - version = get_version_number() - )?; - Ok(()) + version = get_version_number() + )?; + Ok(()) } fn print_version() -> io::Result<()> { - writeln!(io::stdout(), "bp {}", get_version_number())?; - Ok(()) + writeln!(io::stdout(), "bp {}", get_version_number())?; + Ok(()) } fn main() { - match inner_main() { - Ok(()) => (), - Err(e) => { - eprint!("Error: {}", e); - while let Some(e) = e.source() { - eprint!(": {}", e); - } - eprintln!(); - process::exit(1); - } - } + match inner_main() { + Ok(()) => (), + Err(e) => { + eprint!("Error: {}", e); + while let Some(e) = e.source() { + eprint!(": {}", e); + } + eprintln!(); + process::exit(1); + } + } }