From caa71fb6371a04f57c7d54cfc227b7774c22fb5a Mon Sep 17 00:00:00 2001 From: Spenser Black Date: Wed, 10 Jul 2024 09:40:03 -0400 Subject: [PATCH] Only print trailing newline when output is terminal This makes it so the output does *not* have a trailing newline when the user is piping to another command. --- Cargo.lock | 1 + Cargo.toml | 1 + src/main.rs | 4 +++- 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 631a1df..c688981 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -229,6 +229,7 @@ name = "xcolor" version = "0.5.1" dependencies = [ "anyhow", + "atty", "clap", "lazy_static", "nix", diff --git a/Cargo.toml b/Cargo.toml index d01fbdb..e7f1064 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,7 @@ edition = "2018" [dependencies] anyhow = "1" +atty = "0.2" nom = "7" clap = "2" nix = "0.22" diff --git a/src/main.rs b/src/main.rs index 9d2ac51..d498e57 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,7 @@ mod selection; mod util; use anyhow::{anyhow, Result}; +use atty::Stream; use clap::{value_t, ArgMatches, ErrorKind}; use nix::unistd::ForkResult; use xcb::base::Connection; @@ -86,7 +87,8 @@ fn run(args: &ArgMatches) -> Result<()> { set_selection(&conn, root, &selection.unwrap(), &output)?; } } else { - println!("{}", output); + let trailing_newline = atty::is(Stream::Stdout).then_some("\n").unwrap_or(""); + print!("{}{}", output, trailing_newline); } } }